Task cancellation
Task cancellation is a request or mechanism that signals an in-progress task or operation to stop before completing 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 stopping concurrent operations when callers cancel work or deadlines expire.
- How deep it goes
- Explains cancellation-aware channel operations and a custom context that detaches cancellation while retaining values.
- Examples
- Uses a timed position publisher, a file watcher, and asynchronous HTTP-triggered publication to expose cancellation choices.
- What you can do after
- Apply cancellation via Go contexts to channel operations and choose whether asynchronous work should inherit a request’s cancellation.
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 →
Also covered in
- API Design Patterns
- Vanilla WebIn early access (MEAP)
- Fast Python: High performance techniques for large datasets
- Latency
What to know first
- threadA thread is a distinct sequence of execution with its own control flow and state, managed or scheduled by a processor, runtime, or operating system.
- exceptionAn exception is a runtime signal or value representing an abnormal condition that interrupts ordinary program flow and can be handled or propagated.
- async/awaitAsync/await is language syntax for defining asynchronous computations that can suspend at awaited operations and resume when those operations produce results.
- channelA channel is a medium or logical pathway for conveying information, messages, or other content between sources and recipients.
Related concepts
- contextused by · In Go, context.Context is an interface carrying cancellation signals, deadlines, and request-scoped values across function and service boundaries.
- asyncioused by · asyncio is Python’s standard-library framework for asynchronous programming, built around coroutines, awaitable objects, and event loops.
- asyncio.wait_forused by · asyncio.wait_for is a Python coroutine utility that awaits an awaitable for a specified duration and cancels it if the timeout expires.
- CancellationExceptionproduces · CancellationException is an exception used by Kotlin coroutines to signal that a coroutine has been cancelled.
- CancelledErrorproduces · CancelledError is the asyncio exception raised in a task when cancellation is delivered while the task is being awaited or resumed.
Questions about task cancellation
What is task cancellation?
Task cancellation is a request or mechanism that signals an in-progress task or operation to stop before completing normally.
Which Manning books explain task cancellation?
Kotlin in Action, 2E, Go by Example, Python Concurrency with asyncio, 100 Go Mistakes and How to Avoid Them and Go in Action, Second Edition.
What should I know before learning task cancellation?
Manning books that teach task cancellation usually introduce thread, exception, async/await and channel first.
What is the difference between task cancellation and asyncio.shield?
Task cancellation is a request or mechanism that signals an in-progress task or operation to stop before completing normally. asyncio.shield is an asyncio function that protects an awaitable from cancellation requested by the surrounding operation.
What is task cancellation also called?
Task cancellation is also called cancel or cancellation function.
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.




