Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ export function setupRoutes(ctx: AppContext, expressApp: Express, routes: AppRou
});
}
}
});

res.on('close', () => {
setImmediate(() => {
req.originalContext.end();
});
Expand Down
34 changes: 34 additions & 0 deletions src/tests/context-lifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,40 @@ describe('Context Lifecycle', () => {
expect(ctxDuringHandler).not.toBe(originalCtxDuringHandler);
expect(ctxDuringHandler!.parentContext).toBe(originalCtxDuringHandler);
});

it('should close context when client aborts connection', async () => {
let handlerCtx: AppContext | null = null;
let finishEventFired = false;

const nodekit = new NodeKit();
const app = new ExpressKit(nodekit, {
'GET /test': (req: Request, res: Response) => {
handlerCtx = req.originalContext;

res.on('finish', () => {
finishEventFired = true;
});

// Simulate client abort by destroying the socket
req.socket.destroy();
},
});

await request
.agent(app.express)
.get('/test')
.catch(() => {
// Ignore connection error from destroyed socket
});

// We simulate socket destruction, so supertest doesn't wait for proper request completion.
// Need to explicitly wait for setImmediate in 'close' event handler to execute context cleanup.
await new Promise((resolve) => setImmediate(resolve));

expect(handlerCtx).toBeTruthy();
expect(finishEventFired).toBe(false);
expect(handlerCtx!.abortSignal.aborted).toBe(true);
});
});

describe('Context Chain', () => {
Expand Down
Loading