Repository navigation
avoid Client._handle_report cancelling itself on Client._close - #5672
Merged
Merged
Conversation
Collaborator
|
Can one of the admins verify this patch? |
Member
Author
|
I made a test for this but didn't submit it because it happens intermittently due to the gc not collecting the From 1aa4df8993083a8a64e57a653d9e29c0a016337d Mon Sep 17 00:00:00 2001
From: Thomas Grainger <[email protected]>
Date: Thu, 20 Jan 2022 11:57:55 +0000
Subject: [PATCH] add test for _GatheringFuture self-cancel
---
distributed/tests/test_client.py | 44 ++++++++++++++++++++++++++------
1 file changed, 36 insertions(+), 8 deletions(-)
diff --git a/distributed/tests/test_client.py b/distributed/tests/test_client.py
index fbdfec48..d7e4f9cb 100644
--- a/distributed/tests/test_client.py
+++ b/distributed/tests/test_client.py
@@ -16,7 +16,7 @@ import warnings
import weakref
import zipfile
from collections import deque
-from contextlib import suppress
+from contextlib import suppress, contextmanager
from functools import partial
from operator import add
from threading import Semaphore
@@ -3782,15 +3782,43 @@ def test_reconnect(loop):
c.close()
+class UnhandledException(Exception):
+ pass
+
+
+
+@contextmanager
+def _catch_unhandled_exceptions() -> Generator[None, None, None]:
+ loop = asyncio.get_running_loop()
+ ctx: dict[str, object] | None = None
+
+ old_handler = loop.get_exception_handler()
+
+ @loop.set_exception_handler
+ def _(loop: object, context: dict[str, object]) -> None:
+ nonlocal ctx
+ ctx = context
+
+ try:
+ yield
+ finally:
+ loop.set_exception_handler(old_handler)
+ if ctx:
+ raise UnhandledException(ctx["message"]) from ctx.get("exception")
+
+
+
+
@gen_cluster(client=True, nthreads=[], client_kwargs={"timeout": 0.5})
async def test_reconnect_timeout(c, s):
- with captured_logger(logging.getLogger("distributed.client")) as logger:
- await s.close()
- while c.status != "closed":
- await c._update_scheduler_info()
- await asyncio.sleep(0.05)
- text = logger.getvalue()
- assert "Failed to reconnect" in text
+ with _catch_unhandled_exceptions():
+ with captured_logger(logging.getLogger("distributed.client")) as logger:
+ await s.close()
+ while c.status != "closed":
+ await c._update_scheduler_info()
+ await asyncio.sleep(0.05)
+ text = logger.getvalue()
+ assert "Failed to reconnect" in text
@pytest.mark.avoid_ci(reason="hangs on github actions ubuntu-latest CI")
--
2.25.1 |
in dask#4617 and dask#5666 a asyncio.gather call isn't correctly waited on and logs the following asyncio unhandled error: ``` _GatheringFuture exception was never retrieved future: <_GatheringFuture finished exception=CancelledError()> asyncio.exceptions.CancelledError ``` this exception is happening because on reconnect `_close` cancels itself before calling gather: https://github.com/dask/distributed/blob/feac52b49292781e78beff8226407f3a5f2e563e/distributed/client.py#L1335-L1343 `_handle_report()` calls `_reconnect()` calls `_close()` which then cancels itself (edited) `self.coroutines` can only ever contain 1 task - `_handle_report` and so can be removed in favour of explicitly tracking the `_handle_report` task.
graingert
force-pushed
the
gen-coroutine-not-awaited
branch
from
January 21, 2022 11:52
ff9bd24 to
f6afc6d
Compare
Member
Author
|
the test failures seem to be unrelated to this change #5680 |
graingert
marked this pull request as ready for review
January 21, 2022 15:52
fjetter
approved these changes
Jan 25, 2022
fjetter
left a comment
Member
There was a problem hiding this comment.
This looks great, thank you @graingert !
Comment on lines
+3791
to
+3808
| @contextmanager | ||
| def catch_unhandled_exceptions() -> Generator[None, None, None]: | ||
| loop = asyncio.get_running_loop() | ||
| ctx: dict[str, Any] | None = None | ||
|
|
||
| old_handler = loop.get_exception_handler() | ||
|
|
||
| @loop.set_exception_handler | ||
| def _(loop: object, context: dict[str, Any]) -> None: | ||
| nonlocal ctx | ||
| ctx = context | ||
|
|
||
| try: | ||
| yield | ||
| finally: | ||
| loop.set_exception_handler(old_handler) | ||
| if ctx: | ||
| raise UnhandledException(ctx["message"]) from ctx.get("exception") |
Member
There was a problem hiding this comment.
Eventually, we might want to add this to
distributed/distributed/utils_test.py
Lines 1696 to 1711 in 4e2c1b3
gjoseph92
pushed a commit
to gjoseph92/distributed
that referenced
this pull request
Feb 1, 2022
…5672) in dask#4617 and dask#5666 a asyncio.gather call isn't correctly waited on and logs the following asyncio unhandled error: ``` _GatheringFuture exception was never retrieved future: <_GatheringFuture finished exception=CancelledError()> asyncio.exceptions.CancelledError ``` this exception is happening because on reconnect `_close` cancels itself before calling gather: https://github.com/dask/distributed/blob/feac52b49292781e78beff8226407f3a5f2e563e/distributed/client.py#L1335-L1343 `_handle_report()` calls `_reconnect()` calls `_close()` which then cancels itself (edited) `self.coroutines` can only ever contain 1 task - `_handle_report` and so can be removed in favour of explicitly tracking the `_handle_report` task.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
in #4617 and #5666
a asyncio.gather call isn't correctly waited on and logs the following
asyncio unhandled error:
this exception is happening because on reconnect
_closecancels itselfbefore calling gather:
distributed/distributed/client.py
Lines 1335 to 1343 in feac52b
_handle_report()calls_reconnect()calls_close()which then cancels itself (edited)self.coroutinescan only ever contain 1 task -_handle_reportand socan be removed in favour of explicitly tracking the
_handle_reporttask.
pre-commit run --all-files