Skip to content

Instantly share code, notes, and snippets.

@djspiewak
Last active July 11, 2025 16:59
Show Gist options
  • Save djspiewak/67dc93b90c25a28039e079ed18784c8c to your computer and use it in GitHub Desktop.
Save djspiewak/67dc93b90c25a28039e079ed18784c8c to your computer and use it in GitHub Desktop.

Where does Cats Effect cancelation become practically unavoidable? I feel like I never really need to think about it.

Timeouts are the easiest practical example to think about. If you contemplate that whole chain, from request handling in the server layer through the fiber which processes that request, making new requests to upstream services, waiting on those results, etc… there's a lot of timeouts involved in that. Any time you have a timeout, you need to recursively cancel everything which rolls up under it, and you need that cancelation to have a few properties:

  • It must be irreversible (if you have a timeout, you can't un-timeout; this also implies a degree of determinism)
  • It must be respected promptly
  • It must backpressure control flow (ensuring that the continuation is not yielded until all finalizers have completed, implying constituent resources are released)
  • It must not create invalid states (use after free) or leaks (related to backpressure)

Some of these are actually in mutual conflict (such as the backpressure and prompt observation), so pragmatically it's necessary to allow users to define how to best mediate the tradeoff space. This is what uncancelable/poll do for us. Note that we could actually make this a lot easier on ourselves by forcing everything into a structured concurrency paradigm (in CE terms, that would mean no start, only background/Supervisor/Parallel/etc), but that's far too restrictive and obviates novel structures (like Pull and Stream), so we take the harder road.

Honestly, the fact that you've never had to really understand this stuff is a profound complement! It means the primitives work and gave rise to a properly compositional ecosystem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment