Router cycles cause silent runtime hangs · Issue #6809 · expressjs/express · GitHub
Skip to content

Router cycles cause silent runtime hangs #6809

Open
pillarjs/router
#183

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

Activity

  1. aprici7y commented on Oct 7, 2025

    @aprici7y

    i would like to work on this one

  2. aprici7y commented on Oct 7, 2025

    @aprici7y

    This issue is actually in the router package (pillarjs/router) which Express depends on. I've addressed it with cycle detection that emits a warning when a router is mounted on itself: pillarjs/router#183

  3. ganeshbhagwat123924 commented on Oct 9, 2025

    @ganeshbhagwat123924
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions