Cancellation Propagation
Propagate an AbortSignal through async layers so a user cancel stops all in-flight work, and each layer cleans up on abort.
On this page
Introduction
This guide covers propagating cancellation through async layers, a recurring concern in the resilience scenario. A cancel that only stops the top call leaves children running, wasting work and maybe completing after the user has moved on. Threading an AbortSignal through every async layer so each stops and cleans up on abort makes cancel prompt and complete, which is the respectful behavior under load.
Pass the signal down
Accept a signal at the entry and pass it to every child call, so a single abort reaches the whole tree instead of just the root.
The trade-off worth naming is that accept a signal at the entry and pass it to every child call, so a single abort reaches the whole tree instead of just the root adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
async function load(signal) {
const a = await fetch('/a', { signal });
const b = await fetch('/b', { signal });
return [a, b];
}
Check in loops
In a loop, check signal.aborted before each step so a cancel stops the loop instead of running to the end, which is where long work hides.
The trade-off worth naming is that in a loop, check signal.aborted before each step so a cancel stops the loop instead of running to the end, which is where long work hides adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Clean up on abort
Catch AbortError and release partial resources, so a cancelled task does not leak a connection or a half-written file.
The trade-off worth naming is that catch aborterror and release partial resources, so a cancelled task does not leak a connection or a half-written file adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
try { await work(signal); }
catch (e) {
if (e.name === 'AbortError') await cleanup();
else throw e;
}
Combine signals
Use AbortSignal.any to combine a user cancel and a timeout, so either one aborts the work, which is the common real shape.
The trade-off worth naming is that use abortsignal.any to combine a user cancel and a timeout, so either one aborts the work, which is the common real shape adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Stop waiting on rude layers
If a layer ignores the signal, stop waiting on it and treat it as aborted, so one rude layer does not hold the cancel hostage.
The trade-off worth naming is that if a layer ignores the signal, stop waiting on it and treat it as aborted, so one rude layer does not hold the cancel hostage adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Test cancel
Start work, abort, and assert children stop and resources are freed, so cancel is prompt and complete.
The trade-off worth naming is that start work, abort, and assert children stop and resources are freed, so cancel is prompt and complete adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Sources
Sources: AbortController MDN, AbortSignal.any.
FAQ
- What if a layer ignores the signal?
- You cannot force it, but you can stop waiting on it; treat it as aborted and move on, so one rude layer does not hold the cancel.
- AbortSignal.timeout or a controller?
- Use a controller when the user triggers cancel, so you can call abort; use AbortSignal.timeout for a deadline, and combine them with AbortSignal.any.
Report a problem with this articleOpens a pre-filled issue on GitHub.