Propagate request cancellation to stop unnecessary downstream work
When a client no longer needs a response or a request deadline expires, the server should pass cancellation to the child RPCs, queries, and computations started for that request. Receiving a cancellation signal does not automatically stop application work. Each operation must observe cancellation, stop safely, and release the resources it owns.
When a client no longer needs a response or a request deadline expires, the server should pass cancellation to the child RPCs, queries, and computations started for that request. Receiving a cancellation signal does not automatically stop application work. Each operation must observe cancellation, stop safely, and release the resources it owns.
Why cancellation matters
Continuing work whose result is no longer needed wastes CPU, threads, database connections, and downstream capacity. If one request fans out across services or parallel calls, cancellation should reach the whole call tree. The same principle applies to hedged requests: after one replica wins, cancel the redundant calls.
Implementation steps
**Identify cancellation sources.** Distinguish user cancellation, an HTTP connection closing, RPC cancellation, deadline expiry, and a sibling task already producing the result. The reason may not be transmitted to a remote service, so design protocol status and internal diagnostics separately.
**Pass request-scoped cancellation context.** Carry the same request context, or equivalent token, from the handler through the data-access layer. Child contexts derived from it should cancel when the parent does. Treat deliberately detached background work as a separate job with an explicit owner, time limit, retry policy, and shutdown behavior.
**Check cooperatively during long work.** In loops and streaming processing, check cancellation at safe points. Once canceled, do not start more work; exit. Do not assume a library forcibly interrupts the application handler.
**Connect the signal to downstream I/O.** Pass the request context to RPCs, database queries, and file or network operations that support it. Check whether waiting for a connection-pool slot is also bounded by that context. Group parallel tasks so a failed child can cancel its remaining siblings when appropriate.
**Design shutdown and cleanup.** On cancellation, close cursors and response streams, remove temporary files, release semaphores, and finish the handler. Cancellation does not undo committed data changes. Use transaction boundaries, idempotency keys, or compensating work to handle partial completion safely.
**Observe cancellation separately from faults.** Track cancellation count, time to observe it, work that fails to stop, and cleanup outcomes. Classify expected cancellation errors separately from service failures without hiding genuine downstream errors.
What to check in gRPC and Go
In gRPC, a client can cancel an RPC; deadline expiry and I/O errors can also trigger cancellation. The server application handler may still be running, so it must check for cancellation and stop its work. Automatic cancellation of outgoing RPCs varies by language implementation. The gRPC guide lists automatic support for outgoing calls in Java, Go, and C++; handlers in other languages may need to do this themselves.
A Go HTTP handler can pass r.Context() to downstream APIs such as database methods. If the client disconnects or cancels the request, that context is canceled, allowing a query that receives it to stop early. When deriving a context with context.WithTimeout or context.WithDeadline, call the returned cancel function to release resources held by the context.
How to verify
Cancel a client request during a slow downstream call or long computation, then check that the server handler, child RPCs, and queries finish within the expected time. For parallel calls, verify that losers stop after a winner returns. Put time limits or isolation around library calls that cannot receive cancellation. Do not assume cancellation rolls back an already committed change. No live service was run or tested for this article.
Sources
[gRPC: Cancellation](https://grpc.io/docs/guides/cancellation/)
[Go: Canceling in-progress operations](https://go.dev/doc/database/cancel-operations)
[Google SRE Book: Addressing Cascading Failures — Cancellation propagation](https://sre.google/sre-book/addressing-cascading-failures/)