Skip to content

Router cycles cause silent runtime hangs #6809

@IlyasShabi

Description

@IlyasShabi

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions