Cancellable Uploads with AbortController
Cancel an in-flight upload with AbortController, clean up the partial server-side artifact, and refund the UI to idle without leaked listeners.
On this page
Introduction
This guide covers cancellable uploads with AbortController, a recurring concern in the file-upload scenario. A user who clicks cancel expects the upload to stop and the UI to free up, not to keep spinning until the server finishes. AbortController lets you cancel the fetch mid-flight, and wiring it to a server-side cleanup of the partial artifact keeps storage from filling with half files.
One controller per upload
Create a fresh AbortController for each upload so canceling one does not cancel another, and store it where the cancel button can reach it.
The trade-off worth naming is that create a fresh abortcontroller for each upload so canceling one does not cancel another, and store it where the cancel button can reach it 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.
const ctrl = new AbortController();
document.getElementById('cancel').onclick = () => ctrl.abort();
try {
await fetch('/upload', { method: 'POST', body: file, signal: ctrl.signal });
} catch (e) {
if (e.name === 'AbortError') console.log('cancelled');
}
Clean up server-side
On a closed socket or an explicit cancel, delete the partial artifact so storage does not fill with half files, which is the cost of ignoring cancel.
The trade-off worth naming is that on a closed socket or an explicit cancel, delete the partial artifact so storage does not fill with half files, which is the cost of ignoring cancel 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.
req.on('close', async () => {
if (!finished) await storage.delete(partialKey);
});
Refund the UI
On abort or completion, remove the cancel handler and let the controller go out of scope, so the button does not hold a dead reference and the upload row returns to idle.
The trade-off worth naming is that on abort or completion, remove the cancel handler and let the controller go out of scope, so the button does not hold a dead reference and the upload row returns to idle 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.
Abort reads too
If you read the file with a stream, abort the stream as well so the browser stops reading from disk, which is the other half of a prompt cancel.
The trade-off worth naming is that if you read the file with a stream, abort the stream as well so the browser stops reading from disk, which is the other half of a prompt cancel 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 the cancel
Start a large upload, click cancel, and assert the fetch rejects with AbortError, the partial artifact is gone, and the UI is idle, so cancel is real end to end.
The trade-off worth naming is that start a large upload, click cancel, and assert the fetch rejects with aborterror, the partial artifact is gone, and the ui is idle, so cancel is real end to end 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, fetch signal.
FAQ
- Does abort stop the server from writing?
- It stops the client from sending, but the server may have a partial file; have it watch for a closed socket or an explicit cancel call and delete the partial artifact.
- How do I avoid leaking listeners?
- Remove the cancel handler when the upload completes or aborts, and let the controller go out of scope, so the UI does not hold dead references.
Report a problem with this articleOpens a pre-filled issue on GitHub.