Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
synapse
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Maunium
synapse
Commits
91bc15c7
Unverified
Commit
91bc15c7
authored
3 years ago
by
Sean Quah
Committed by
GitHub
3 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Add `stop_cancellation` utility function (#12106)
parent
c8936323
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
changelog.d/12106.misc
+1
-0
1 addition, 0 deletions
changelog.d/12106.misc
synapse/util/async_helpers.py
+19
-0
19 additions, 0 deletions
synapse/util/async_helpers.py
tests/util/test_async_helpers.py
+45
-0
45 additions, 0 deletions
tests/util/test_async_helpers.py
with
65 additions
and
0 deletions
changelog.d/12106.misc
0 → 100644
+
1
−
0
View file @
91bc15c7
Add `stop_cancellation` utility function to stop `Deferred`s from being cancelled.
This diff is collapsed.
Click to expand it.
synapse/util/async_helpers.py
+
19
−
0
View file @
91bc15c7
...
...
@@ -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
This diff is collapsed.
Click to expand it.
tests/util/test_async_helpers.py
+
45
−
0
View file @
91bc15c7
...
...
@@ -27,6 +27,7 @@ from synapse.logging.context import (
from
synapse.util.async_helpers
import
(
ObservableDeferred
,
concurrently_execute
,
stop_cancellation
,
timeout_deferred
,
)
...
...
@@ -282,3 +283,47 @@ class ConcurrentlyExecuteTest(TestCase):
d2
=
ensureDeferred
(
caller
())
d1
.
callback
(
0
)
self
.
successResultOf
(
d2
)
class
StopCancellationTests
(
TestCase
):
"""
Tests for the `stop_cancellation` function.
"""
def
test_succeed
(
self
):
"""
Test that the new `Deferred` receives the result.
"""
deferred
:
"
Deferred[str]
"
=
Deferred
()
wrapper_deferred
=
stop_cancellation
(
deferred
)
# Success should propagate through.
deferred
.
callback
(
"
success
"
)
self
.
assertTrue
(
wrapper_deferred
.
called
)
self
.
assertEqual
(
"
success
"
,
self
.
successResultOf
(
wrapper_deferred
))
def
test_failure
(
self
):
"""
Test that the new `Deferred` receives the `Failure`.
"""
deferred
:
"
Deferred[str]
"
=
Deferred
()
wrapper_deferred
=
stop_cancellation
(
deferred
)
# Failure should propagate through.
deferred
.
errback
(
ValueError
(
"
abc
"
))
self
.
assertTrue
(
wrapper_deferred
.
called
)
self
.
failureResultOf
(
wrapper_deferred
,
ValueError
)
self
.
assertIsNone
(
deferred
.
result
,
"
`Failure` was not consumed
"
)
def
test_cancellation
(
self
):
"""
Test that cancellation of the new `Deferred` leaves the original running.
"""
deferred
:
"
Deferred[str]
"
=
Deferred
()
wrapper_deferred
=
stop_cancellation
(
deferred
)
# Cancel the new `Deferred`.
wrapper_deferred
.
cancel
()
self
.
assertTrue
(
wrapper_deferred
.
called
)
self
.
failureResultOf
(
wrapper_deferred
,
CancelledError
)
self
.
assertFalse
(
deferred
.
called
,
"
Original `Deferred` was unexpectedly cancelled.
"
)
# Now make the inner `Deferred` fail.
# The `Failure` must be consumed, otherwise unwanted tracebacks will be printed
# in logs.
deferred
.
errback
(
ValueError
(
"
abc
"
))
self
.
assertIsNone
(
deferred
.
result
,
"
`Failure` was not consumed
"
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment