Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Update delay_cancellation to accept any awaitable #12468

Merged
merged 2 commits into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/12468.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update `delay_cancellation` to accept any awaitable, rather than just `Deferred`s.
3 changes: 1 addition & 2 deletions synapse/storage/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
from typing_extensions import Literal

from twisted.enterprise import adbapi
from twisted.internet import defer

from synapse.api.errors import StoreError
from synapse.config.database import DatabaseConnectionConfig
Expand Down Expand Up @@ -794,7 +793,7 @@ async def _runInteraction() -> R:
# We also wait until everything above is done before releasing the
# `CancelledError`, so that logging contexts won't get used after they have been
# finished.
return await delay_cancellation(defer.ensureDeferred(_runInteraction()))
return await delay_cancellation(_runInteraction())

async def runWithConnection(
self,
Expand Down
48 changes: 38 additions & 10 deletions synapse/util/async_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
Awaitable,
Callable,
Collection,
Coroutine,
Dict,
Generic,
Hashable,
Expand Down Expand Up @@ -701,27 +702,54 @@ def stop_cancellation(deferred: "defer.Deferred[T]") -> "defer.Deferred[T]":
return new_deferred


def delay_cancellation(deferred: "defer.Deferred[T]") -> "defer.Deferred[T]":
"""Delay cancellation of a `Deferred` until it resolves.
@overload
def delay_cancellation(awaitable: "defer.Deferred[T]") -> "defer.Deferred[T]":
...


@overload
def delay_cancellation(awaitable: Coroutine[Any, Any, T]) -> "defer.Deferred[T]":
...


@overload
def delay_cancellation(awaitable: Awaitable[T]) -> Awaitable[T]:
...


def delay_cancellation(awaitable: Awaitable[T]) -> Awaitable[T]:
"""Delay cancellation of a coroutine or `Deferred` awaitable until it resolves.

Has the same effect as `stop_cancellation`, but the returned `Deferred` will not
resolve with a `CancelledError` until the original `Deferred` resolves.
resolve with a `CancelledError` until the original awaitable resolves.

Args:
deferred: The `Deferred` to protect against cancellation. May optionally follow
the Synapse logcontext rules.
deferred: The coroutine or `Deferred` to protect against cancellation. May
optionally follow the Synapse logcontext rules.

Returns:
A new `Deferred`, which will contain the result of the original `Deferred`.
The new `Deferred` will not propagate cancellation through to the original.
When cancelled, the new `Deferred` will wait until the original `Deferred`
resolves before failing with a `CancelledError`.
A new `Deferred`, which will contain the result of the original coroutine or
`Deferred`. The new `Deferred` will not propagate cancellation through to the
original coroutine or `Deferred`.

The new `Deferred` will follow the Synapse logcontext rules if `deferred`
When cancelled, the new `Deferred` will wait until the original coroutine or
`Deferred` resolves before failing with a `CancelledError`.

The new `Deferred` will follow the Synapse logcontext rules if `awaitable`
follows the Synapse logcontext rules. Otherwise the new `Deferred` should be
wrapped with `make_deferred_yieldable`.
"""

# First, convert the awaitable into a `Deferred`.
if isinstance(awaitable, defer.Deferred):
deferred = awaitable
elif isinstance(awaitable, Coroutine):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I slightly wonder if this is reliable.

Possibly we want asyncio.iscoroutine which allows a wider range of types, and has some caching.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good idea. Deferred.fromCoroutine accepts anything that asyncio.iscoroutine identifies as a coroutine, so it looks safe to do.

deferred = defer.ensureDeferred(awaitable)
richvdh marked this conversation as resolved.
Show resolved Hide resolved
else:
# We have no idea what to do with this awaitable.
# Let the caller `await` it normally.
return awaitable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what sort of awaitables do we have that are neither coroutine objects nor Deferreds, outside of things returned by make_awaitable in unit tests? Would it be better to raise in this case? If so, perhaps we just call defer.ensureDeferred(awaitable) without bothering to do the type checks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from Futures returned by make_awaitable, we have DoneAwaitables from maybe_awaitable. More generally, I think any awaitable that's already "complete" can be awaited in synapse.

I'm not keen on raising, since I do think we should support arbitrary completed awaitables, ie. if await x works, await delay_cancellation(x) should also work.

I'll add a comment mentioning the types of awaitable we'd expect here.


def handle_cancel(new_deferred: "defer.Deferred[T]") -> None:
# before the new deferred is cancelled, we `pause` it to stop the cancellation
# propagating. we then `unpause` it once the wrapped deferred completes, to
Expand Down
33 changes: 31 additions & 2 deletions tests/util/test_async_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ def test_cancellation(self):
class DelayCancellationTests(TestCase):
"""Tests for the `delay_cancellation` function."""

def test_cancellation(self):
def test_deferred_cancellation(self):
"""Test that cancellation of the new `Deferred` waits for the original."""
deferred: "Deferred[str]" = Deferred()
wrapper_deferred = delay_cancellation(deferred)
Expand All @@ -403,6 +403,35 @@ def test_cancellation(self):
# Now that the original `Deferred` has failed, we should get a `CancelledError`.
self.failureResultOf(wrapper_deferred, CancelledError)

def test_coroutine_cancellation(self):
"""Test that cancellation of the new `Deferred` waits for the original."""
blocking_deferred: "Deferred[None]" = Deferred()
completion_deferred: "Deferred[None]" = Deferred()

async def task():
await blocking_deferred
completion_deferred.callback(None)
# Raise an exception. Twisted should consume it, otherwise unwanted
# tracebacks will be printed in logs.
raise ValueError("abc")

wrapper_deferred = delay_cancellation(task())

# Cancel the new `Deferred`.
wrapper_deferred.cancel()
self.assertNoResult(wrapper_deferred)
self.assertFalse(
blocking_deferred.called, "Cancellation was propagated too deep"
)
self.assertFalse(completion_deferred.called)

# Unblock the task.
blocking_deferred.callback(None)
self.assertTrue(completion_deferred.called)

# Now that the original coroutine has failed, we should get a `CancelledError`.
self.failureResultOf(wrapper_deferred, CancelledError)

def test_suppresses_second_cancellation(self):
"""Test that a second cancellation is suppressed.

Expand Down Expand Up @@ -451,7 +480,7 @@ async def inner():
async def outer():
with LoggingContext("c") as c:
try:
await delay_cancellation(defer.ensureDeferred(inner()))
await delay_cancellation(inner())
self.fail("`CancelledError` was not raised")
except CancelledError:
self.assertEqual(c, current_context())
Expand Down