Skip to content

Commit 1b27d2e

Browse files
authored
Improve error when submitting work from a closed client (#9049)
1 parent 224ae88 commit 1b27d2e

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

distributed/client.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,10 @@ class AllExit(Exception):
734734
"""Custom exception class to exit All(...) early."""
735735

736736

737+
class ClosedClientError(Exception):
738+
"""Raised when an action with a closed client can't be performed"""
739+
740+
737741
def _handle_print(event):
738742
_, msg = event
739743
if not isinstance(msg, dict):
@@ -1419,9 +1423,8 @@ def _send_to_scheduler(self, msg):
14191423
if self.status in ("running", "closing", "connecting", "newly-created"):
14201424
self.loop.add_callback(self._send_to_scheduler_safe, msg)
14211425
else:
1422-
raise Exception(
1423-
"Tried sending message after closing. Status: %s\n"
1424-
"Message: %s" % (self.status, msg)
1426+
raise ClosedClientError(
1427+
f"Client is {self.status}. Can't send {msg['op']} message."
14251428
)
14261429

14271430
async def _start(self, timeout=no_default, **kwargs):

distributed/tests/test_client.py

+14
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
)
6464
from distributed.client import (
6565
Client,
66+
ClosedClientError,
6667
Future,
6768
FutureCancelledError,
6869
FuturesCancelledError,
@@ -5711,6 +5712,19 @@ async def test_warn_when_submitting_large_values_memoryview(c, s):
57115712
c.submit(lambda: a)
57125713

57135714

5715+
@gen_cluster(client=True, nthreads=[])
5716+
async def test_closed_client_send_message(c, s):
5717+
# Ensure a meaningful, but concise error is raised when
5718+
# a closed client attempts to send a message to the scheduler
5719+
await c.close()
5720+
with pytest.raises(ClosedClientError, match="update-graph") as exc_info:
5721+
c.submit(lambda x: x + 1)
5722+
5723+
msg = str(exc_info.value)
5724+
assert "Can't send update-graph" in msg
5725+
assert len(msg) < 100
5726+
5727+
57145728
@gen_cluster(client=True)
57155729
async def test_unhashable_function(c, s, a, b):
57165730
func = _UnhashableCallable()

0 commit comments

Comments
 (0)