Skip to content

fix: resolve AppContext memory leak for direct app.express handlers#117

Merged
sjorobekov merged 1 commit into
mainfrom
fix/appcontext-leak-direct-routes
May 18, 2026
Merged

fix: resolve AppContext memory leak for direct app.express handlers#117
sjorobekov merged 1 commit into
mainfrom
fix/appcontext-leak-direct-routes

Conversation

@sjorobekov
Copy link
Copy Markdown
Contributor

@sjorobekov sjorobekov commented May 18, 2026

Problem

Currently, the per-request AppContext cleanup (res.on('close', () => req.originalContext.end())) only happens inside routeInfoMiddleware. This middleware is only attached per-route during setupRoutes.

If consumers register handlers directly on app.express (e.g., app.express.get('/healthz', handler), they bypass setupRoutes. As a result, every request to these bare routes permanently leaks:

  • An 'abort' event listener on the long-lived root nodekit.ctx.abortSignal
  • An AppContext instance
  • An OTel Span

Solution

This PR moves the per-request AppContext cleanup hook from the per-route routeInfoMiddleware into the global setupBaseMiddleware.

Because setupBaseMiddleware is the same place the context is originally created, the cleanup will now reliably run for every request that hits the Express app, regardless of how the handler was registered.

@sjorobekov sjorobekov marked this pull request as ready for review May 18, 2026 10:01
Comment thread src/base-middleware.ts
@sjorobekov sjorobekov merged commit 227004f into main May 18, 2026
5 checks passed
@sjorobekov sjorobekov deleted the fix/appcontext-leak-direct-routes branch May 18, 2026 10:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants