liveBook · Manning

Task cancellation

Also called cancel, cancellation function

Task cancellation is a request or mechanism that signals an in-progress task or operation to stop before completing normally.

How 5 Manning books explain it

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.

Read how Kotlin in Action, 2E explains task cancellation →

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.

Read how Go by Example explains task cancellation →

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

What to know first

Related concepts

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.