Definition
Cancellation is a request to stop an operation that has been queued or started but has not yet reported its final result. The request runs at the same time as the operation, and whichever reaches the deciding step first wins. So the operation may never run, may stop part-way and undo its partial work, or may complete anyway. A cancellation protocol states which outcomes are possible, how the requester learns which one happened, and when the operation’s resources may be released.
The idea
A program asks a storage service to append a record to a log and waits 10 milliseconds (ms) for an answer. The service writes the record at 9 ms, but its reply is still on the way when the program’s 10 ms timeout §9.34 expires. The program sends a cancellation and tells its user that nothing happened. The user tries again, and the record is now in the log twice. The cancellation arrived after the effect had happened, and the program reported the outcome it hoped for instead of the one that occurred.
The picture is recalling a letter. While the letter is still in your own out-tray, you can take it back and nothing has happened. Once the carrier has collected it, you can ask the post office to intercept it, and you may or may not be in time. Once it is delivered, recalling it is impossible. You can only send a second letter asking the reader to ignore the first, which is a new action, not a cancellation. Cancellation in software has the same three regions. The picture stops at one point: the writer of a letter usually finds out later which region applied, while a program learns it only if the protocol reports it.
How it works
Model each request as a state machine §8.33, a set of states with defined transitions between them. A minimal one has these states.
Queued: accepted, and no worker has started it.
Running: a worker is executing it, and no lasting effect exists yet.
Committed: the effect has happened and cannot be stopped; for example, the record is in the log.
Succeeded, failed and canceled: the terminal states, in which the outcome is final and reported.
Cancellation acts differently in each state.
From queued: remove the request before a worker takes it. The canceller and a worker may act at the same moment, so the move out of queued must be one atomic operation §9.8, an operation that no other activity can observe half done. A compare-and-swap §9.9, which changes the state only if it still holds the expected value, does this: exactly one of “queued to canceled” and “queued to running” succeeds. If the canceller wins, the effect certainly never happened.
From running: the canceller cannot safely stop the worker at an arbitrary instruction, because the worker may hold locks or have left shared data half updated. Instead the canceller sets a cancel-requested flag, and the worker checks the flag at safe points, places where it holds no lock and has no half-done update. At a safe point the worker undoes its partial work, releases what it holds, and moves the state to canceled. This is cooperative cancellation.
From committed: too late. The effect has happened, and the correct outcome is succeeded. Reversing the effect needs a compensating operation, a new operation that undoes the effect at the level of the application, such as a refund for a payment. Some effects, such as a message that someone has already read, cannot be compensated at all.
Two acknowledgments must be kept apart. The first says that the cancellation request was received. The second is the final completion: the request has reached a terminal state, and no worker, device, timer or callback §14.22 will touch its resources again. Only the second allows the requester to free buffers or reuse the request record. Many systems offer a cancel call that returns at once and a synchronous one that also waits until a handler that is already running has finished; in the Linux kernel, for example, cancel_work_sync waits for deferred work that is already running.
Finally, report the actual outcome: succeeded, failed, canceled, or unknown when the requester cannot tell, for example because the reply was lost. An unknown outcome must not be reported as canceled. The requester resolves it by asking the worker for the status of the request by its identifier. It can also make a retry harmless by giving every request a unique identifier that the worker remembers, so that a repeated request is recognized and not applied twice. A request handled this way is idempotent §8.31: applying it twice has the same effect as applying it once.
Worked example
Times are in milliseconds (ms). Request 501 appends one record to a log, and request 502 appends another. Every state change is made with a compare-and-swap.
At 0 ms request 501 is submitted, and a worker moves it from queued to running.
At 3 ms request 502 is submitted and waits in the queue behind 501.
At 4 ms the caller cancels 502. The compare-and-swap “queued to canceled” finds 502 still queued and succeeds.
At 6 ms the worker tries “queued to running” on 502. The compare-and-swap finds canceled instead of queued and fails, so the worker skips 502. Its record is never written, and the reported outcome, canceled, is true.
At 9 ms the worker writes the record of 501 to the log and moves 501 to committed.
At 10 ms the caller’s timeout for 501 expires, and the caller requests cancellation of 501. The request comes ms after the commit.
At 11 ms the worker sees the cancel-requested flag, but 501 is committed, so it moves 501 to succeeded and reports “succeeded; the cancellation came too late”.
Had the caller treated its timeout at 10 ms as “nothing happened” and submitted the record again as a new request, the log would hold 2 copies. If the resubmission instead carries the same identifier, 501, the worker finds 501 already applied and returns the earlier result, and the log holds 1 copy.
Stated precisely
Write for a transition from state to state . Let the states be (queued), (running), (committed), and the terminal states (succeeded), (failed) and (canceled). The allowed transitions are (a worker starts), (canceled before start), (the worker stops at a safe point after a cancel request and after undoing its partial work), (failure, with partial work undone), (commit), and . A correct protocol guarantees four properties.
One winner: each transition out of a state is a single atomic step, so when the canceller and the worker race, exactly one transition happens.
No effect when canceled: reaching means that the effect did not happen.
No cancellation after commit: no transition leads from to .
Final means final: the terminal state is reported exactly once, and only after every party has stopped using the request’s resources.
These guarantees rest on two assumptions. The first is the atomicity §8.3 of the commit: it happens completely or not at all, so no state lies between running and committed. The second is that the worker checks for cancellation only at safe points.
Limits and common mistakes
A cancel request is not a stop. A flag that says “cancel requested” records intent. Only the final completion shows that the work has stopped.
Killing a thread is not cancellation. Forcibly ending a thread, as the Windows function TerminateThread does, can leave a lock held forever, shared data half updated and a device in the middle of a command. POSIX (Portable Operating System Interface) threads offer deferred cancellation, which acts only at defined cancellation points, and asynchronous cancellation, which can act at any instruction and is safe only in code written for it.
A timeout is not a cancellation. A timeout ends the caller’s waiting, and the operation may continue; the Timeout versus cancellation §13.25 entry treats the difference.
Cancellation does not undo a committed effect. Compensation is a different operation, with its own failures.
Cancellation must itself be idempotent. Canceling twice, or canceling a request that has already finished, must be harmless and must not release resources a second time.
If both the cancel path and the completion path release the request’s resources, without one atomic decision about which path wins, the result is a double free §4.7 or a use-after-free §4.6.
Across a network, the remote side may never receive the cancellation, so the outcome may stay unknown until the requester asks.
A transaction §8.7, an idempotent request identifier and a compensating operation solve different problems. A transaction makes a group of changes all-or-nothing, an identifier makes a repeat harmless, and compensation reverses a finished effect. A cancellation interface provides none of them by itself.
Questions answered
Can cancellation arrive after the operation has already committed?
Yes, cancellation can arrive after the operation has already committed. Cancellation and the operation run at the same time, so the operation can reach its commit, the step after which its effect has happened, before the cancellation request reaches the worker or before the worker next checks for cancellation. A request that arrives after the commit cannot undo the effect. The correct outcome is then “succeeded”, and the requester must treat the effect as done.
A sound protocol handles this case in four ways. It decides the race with one atomic state change, so that “committed” and “canceled” can never both be recorded. It reports the actual outcome, including “unknown” when the reply was lost, and never reports “canceled” for work that committed. It makes retries harmless with a unique request identifier, so that a retry after an unknown outcome is not applied twice. And it releases the request’s buffers only after a final completion that guarantees nothing will touch them again, not when the cancel flag is set. Undoing a committed effect needs a separate compensating operation, such as a refund.
Related entries: Asynchronous completion (§9.28), Deadline versus timeout (§9.34), Ownership (§4.2), Timeout versus cancellation (§13.25), Idempotency (§8.31), Compare-and-swap (§9.9), Finite-state machine (§8.33).