mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2025-05-02 23:44:57 -04:00
Add stop_cancellation
utility function (#12106)
This commit is contained in:
parent
c893632319
commit
91bc15c772
3 changed files with 65 additions and 0 deletions
|
@ -665,3 +665,22 @@ def maybe_awaitable(value: Union[Awaitable[R], R]) -> Awaitable[R]:
|
|||
return value
|
||||
|
||||
return DoneAwaitable(value)
|
||||
|
||||
|
||||
def stop_cancellation(deferred: "defer.Deferred[T]") -> "defer.Deferred[T]":
|
||||
"""Prevent a `Deferred` from being cancelled by wrapping it in another `Deferred`.
|
||||
|
||||
Args:
|
||||
deferred: The `Deferred` to protect against cancellation. Must not follow the
|
||||
Synapse logcontext rules.
|
||||
|
||||
Returns:
|
||||
A new `Deferred`, which will contain the result of the original `Deferred`,
|
||||
but will not propagate cancellation through to the original. When cancelled,
|
||||
the new `Deferred` will fail with a `CancelledError` and will not follow the
|
||||
Synapse logcontext rules. `make_deferred_yieldable` should be used to wrap
|
||||
the new `Deferred`.
|
||||
"""
|
||||
new_deferred: defer.Deferred[T] = defer.Deferred()
|
||||
deferred.chainDeferred(new_deferred)
|
||||
return new_deferred
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue