expressjs / expressjs/express

Router cycles cause silent runtime hangs

Open
#6,809 3 comments 1 reaction 0 assignees View on GitHub
module:router
Dominant language
JavaScript
Stars
69.5k
Forks
25k
Avg merge
4d 20h
Merged PRs (30d)
9

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).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.