🔄 The Abort Signal Nobody Was Listening For
Canceling an in-flight fetch request depends entirely on passing the abort controller’s signal into the request before it starts, since the signal has to already be attached for the browser to know which in-flight request a later abort call is supposed to interrupt. A controller created and aborted from inside a callback that runs after the fetch call has already been issued – a very easy mistake when the controller is built lazily, inside the same function that triggers the request – never gets attached to anything, so calling abort on it does precisely nothing, and the original request keeps running to completion, with its response quietly applied to the page even though the code clearly intended to cancel it.
🔎 The Problem
function search(query) {
// Looks correct - a fresh controller for each search call:
const controller = new AbortController();
if (activeSearch) {
activeSearch.abort(); // aborts the PREVIOUS controller - fine
}
activeSearch = controller;
// The bug: the signal is read from `controller` here, but if
// this function is re-entered before the fetch below actually
// starts (e.g. called again synchronously, or from a debounced
// handler that fires twice), the second call's fetch captures
// a DIFFERENT controller's signal than the one `activeSearch`
// now points to - so aborting activeSearch later aborts a
// controller no in-flight fetch call ever actually used.
return fetch(`/api/search?q=${query}`, { signal: controller.signal })
.then(r => r.json());
}
✅ Fix: Create and Attach the Controller Before Anything Else Can Run
- Create the abort controller and read its signal into the fetch call in the same synchronous block, before any other code – including a debounce wrapper or an event handler re-entry – gets a chance to run and start a second request with a different signal.
- Store the exact controller instance that was actually passed into the in-flight fetch call, not a separately tracked reference that could point to a different one, so the later abort call is guaranteed to target the request it is meant to cancel.
- Add a quick console log of the controller’s identity at both the point it is created and the point abort is called on it during development, to catch a mismatch between the two immediately instead of only noticing a stale response applying itself to the page.
⚠️ Why This Is Easy to Miss
- The code reads top to bottom as if it obviously cancels the previous request before starting a new one, and it does exactly that for the common case of two calls spaced far enough apart – the bug only appears when calls happen close enough together to race.
- A fetch call that never actually gets canceled simply finishes and applies its result, which looks like a normal, successful search response – there is no error or rejected promise to point at the fact that an abort call silently had no target.
An abort signal only cancels the request it was attached to – if two requests existed before the attaching happened, aborting one was never going to touch the other.




