Answer to the question
KrakenJS does not automatically inject an AbortController into the request context when a route timeout occurs. The framework only aborts the HTTP response; any pending asynchronous work continues to run unless you add explicit cancellation logic. The most pragmatic way to get cancellation propagation is to add a small middleware that creates an AbortController, attaches it to the request, and triggers abort() when the configured timeout fires.
Why this works
Node v18+ exposes AbortController globally, and many async APIs (fetch, streams, timers) accept an AbortSignal. By passing the same signal to every downstream async call, you give each layer the chance to stop early when the controller aborts. KrakenJS’s timeout option only ends the HTTP response; it never touches the event loop.
Step‑by‑step implementation
- Create a middleware that attaches an AbortController.
function abortControllerMiddleware(req, res, next) {
req.abortController = new AbortController();
next();
}
- Register the middleware before your routes.
app.use(abortControllerMiddleware);
- Configure the route timeout.
KrakenJS lets you set a timeout in milliseconds.
app.get(
'/data',
{ timeout: 2000 }, // 2‑second timeout
async (req, res) => {
try {
const result = await dataService.fetchData(req.abortController.signal);
res.send(result);
} catch (err) {
if (err.name === 'AbortError') {
res.status(499).send('Client aborted');
} else {
throw err;
}
}
}
);
- In the service layer, accept a signal and pass it to all async operations.
async function fetchData(signal) {
// Example: HTTP request
const response = await fetch('https://api.example.com', { signal });
return await response.json();
}
- Optional: listen for the request’s
close event to abort early.
req.on('close', () => {
req.abortController.abort();
});
Things to watch out for
- Make sure every async call you use supports
AbortSignal (Node fetch, streams, timers, many database drivers).
- Do not swallow
AbortError in the service; let it propagate so you can respond with a 499 or similar status.
- When using
setTimeout or setInterval, pass the signal to setTimeout(..., signal) or manually clear the timer on abort.
- If you rely on third‑party libraries that do not expose a signal parameter, you’ll need to wrap or replace them with a cancellable alternative.
Missing diagnostic detail
Do you currently use a database driver or HTTP client that accepts an AbortSignal? Knowing this will help determine whether you need to rewrite those calls or use a wrapper that forwards the signal.
Verification steps
- Start the server and send a request that triggers the timeout (e.g., a slow database query).
- Confirm the client receives a 499 status and the server logs show the
AbortError.
- Check that the database query or external fetch stops early by inspecting logs or using a profiling tool.
Conclusion
KrakenJS does not ship a built‑in cancellation mechanism for route timeouts. The recommended pattern is to add a lightweight middleware that creates an AbortController, attach it to the request, and propagate the signal to every async operation in the service layer. This gives you a single source of truth for cancellation and keeps boilerplate out of each route handler.