Task cancellation
Task cancellation is a request or mechanism for stopping an asynchronous or in-progress operation before it completes normally.
Kotlin in Action, 2E
- How it is used
- Prevents obsolete coroutine work and coordinates cleanup when screens, requests, scopes, or concurrent computations end.
- How deep it goes
- Explains triggering, hierarchy propagation, suspension-point exceptions, cooperative CPU work, resource cleanup, and framework-managed scopes.
- Examples
- Uses launch and async handles, nested child coroutines, CPU-heavy loops, database connections, ViewModel scopes, Ktor requests, and failing sibling tasks.
- What you can do after
- Build cancellable coroutine code, propagate cancellation correctly, and clean up resources across lifecycle and failure paths.
Go by Example
A cancellation signal is an indication that an ongoing operation should stop because it is no longer needed or cannot continue.
- How it is used
- Stops unnecessary concurrent pipeline and HTTP-client work when consumers leave, deadlines expire, or users interrupt execution.
- How deep it goes
- Explains Context trees, timeout derivation, Done/select handling, request propagation, and pipeline cleanup, while deferring a full dispatcher-stage fix as an exercise.
- Examples
- Uses a SendN request pipeline, derived timeout contexts, a cancellable iterator, and a Ctrl+C-interrupted HIT command.
- What you can do after
- Apply it via context.Context to stop pipelines and in-flight HTTP requests while avoiding goroutine and resource leaks.
Python Concurrency with asyncio
Task cancellation is the cooperative termination of an asynchronous task by requesting that it stop at a cancellation point.
- How it is used
- Prevents indefinite asynchronous work and supports responsive user requests, timeouts, graceful shutdown, and coordinated request cancellation.
- How deep it goes
- Explains direct cancellation, await-point delivery, exception handling, timeouts, shielding, and pending-task cleanup without descending into runtime internals.
- Examples
- Demonstrates a timed delay, periodic status checks, wait_for timeout handling, shielded completion, and canceled aiohttp requests.
- What you can do after
- Apply it via asyncio task APIs to stop work, enforce time limits, preserve selected tasks, and clean up requests after failures.
Read how Python Concurrency with asyncio explains task cancellation →
100 Go Mistakes and How to Avoid Them
Context cancellation is the signaling of a context's invalidation so operations observing it can stop or abort their work.
- How it is used
- Coordinates when concurrent operations should stop, whether a caller signals shutdown or a deadline makes work obsolete.
- How deep it goes
- Traces cancellation through Done, Err, channel selects, and the methods of a custom context.
- Examples
- Uses a file watcher, timed position publication, blocking channel operations, and asynchronous HTTP-triggered publication.
- What you can do after
- Apply it via Go's context package to stop waiting operations and avoid unintentionally canceling asynchronous work.
Read how 100 Go Mistakes and How to Avoid Them explains task cancellation →
Go in Action, Second Edition
A cancellation signal is an indication that ongoing work should stop because it is no longer needed.
- How it is used
- Stops concurrent request work when clients disconnect, deadlines expire, or operations become unnecessary.
- How deep it goes
- Explains propagation, timeouts, derived contexts, deferred cancellation, and errgroup coordination without detailing context internals.
- Examples
- Uses an HTTP request lifecycle and concurrent RSS feed fetching with a 60-second timeout and error-triggered cancellation.
- What you can do after
- Apply it via context.Context and errgroup to stop timed-out or failed concurrent operations and release downstream work.
Read how Go in Action, Second Edition explains task cancellation →
Related concepts
- contextused by · In Go, context.Context is an interface carrying cancellation signals, deadlines, and request-scoped values across function and service boundaries.
- CancelledErrorproduces · CancelledError is the asyncio exception raised in a task when cancellation is delivered while the task is being awaited or resumed.
- coroutine hierarchyuses · A coroutine hierarchy is a parent-child organization of coroutines through which execution lifetimes and failures can be related.
- CancellationExceptionuses · CancellationException is an exception used by Kotlin coroutines to signal that a coroutine has been cancelled.
- context packageproduced by · The context package is Go’s standard-library package for propagating cancellation, deadlines, timeouts, and request-scoped values across call boundaries.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.




