-
-
Notifications
You must be signed in to change notification settings - Fork 21.3k
Description
Express allows a router to be mounted on itself (directly or indirectly). The registration succeeds, but any request that traverses the cycle hangs indefinitely causing a stack overflow. The issue is tricky to spot because nothing happens at startup.
Detecting this setup when app.use
/ router.use
is called and emitting a warning would make the problem much easier to diagnose, while keeping behavior unchanged.
Steps to reproduce
const express = require('express');
const app = express();
// Simple self-loop
const loop = express.Router();
loop.use('/', loop); // mounts router onto itself
app.use('/cycle', loop);
app.get('/', (req, res) => res.send('ok'));
app.listen(3000, () => console.log('listening on 3000'));
Hit GET /cycle
the request never completes and memory usage grows until the process terminates.
The issue is easy to introduce in complex router hierarchies and incredibly hard to debug once it manifests. Emitting a warning when such a circular mount is detected would reduce the time needed to locate the offending setup, and we can consider turning this into a thrown error in the next major version (v6).