Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
By submitting this pull request you agree that all contributions to this project are made under the MIT license.
Issues
Closes: #956
Presently, there is no way for async effects to shield themselves from cancellation. This is a problem if an effect needs to perform some cleanup before exiting or if the effect is performing an operation that, if it were to stop halfway through, would leave the program in an invalid state.
Summary
Leverage
anyio.CancelScope
to allow users to shield important operations from cancellation. Cancellation shielding can be achieved by entering a
CancelScope(shield=True)` async context:In the code above, if the effect reaches the
critical_task()
, it will always complete that before exiting, even if the effect has been cancelled by an unmounting component.The current implementation is not perfect though. We ought to block until all effects associated with the current component have exited. This will require asyncifying some of the underlying rendering logic in
Layout
. This isn't too much work, but it can be left for a follow up PR. In its current state, if a user shielded an effect from cancellation it is technically possible for the lifetime of the cancelled (but currently shielded) effect from overlapping with the lifetime of the effect initiated as part of the next render.Checklist
changelog.rst
has been updated with any significant changes.