When using asynchronous programming with libraries like trio
or anyio
, cancellation scopes (timeout contexts) are used to
implement timeouts and cancellation. However, these mechanisms only work when there are checkpoints within the scope where cancellation can occur.
Without any checkpoints, the timeout will never be triggered, making it ineffective.
A checkpoint is a point in the code where cancellation can be detected and acted upon. Common checkpoints include:
- Explicit calls to the checkpoint method of the used framework
-
yield
statements
-
await
statements
What is the potential impact?
Without proper checkpoints in cancel scopes:
- Timeouts won’t work as expected
- Resources might be held longer than intended
- The application might become unresponsive or hang
- Cancellation operations won’t be honored