Skip to content

Commit f9406a2

Browse files
authored
Clean up deprecated client context in different tasks/threads (#9233)
1 parent e9c87f1 commit f9406a2

2 files changed

Lines changed: 6 additions & 34 deletions

File tree

distributed/client.py

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,17 +1713,7 @@ async def __aenter__(self):
17131713

17141714
async def __aexit__(self, exc_type, exc_value, traceback):
17151715
if self._previous_as_current:
1716-
try:
1717-
_current_client.reset(self._previous_as_current)
1718-
except ValueError as e:
1719-
if not e.args[0].endswith(" was created in a different Context"):
1720-
raise # pragma: nocover
1721-
warnings.warn(
1722-
"It is deprecated to enter and exit the Client context "
1723-
"manager from different tasks",
1724-
DeprecationWarning,
1725-
stacklevel=2,
1726-
)
1716+
_current_client.reset(self._previous_as_current)
17271717
await self._close(
17281718
# if we're handling an exception, we assume that it's more
17291719
# important to deliver that exception than shutdown gracefully.
@@ -1732,17 +1722,7 @@ async def __aexit__(self, exc_type, exc_value, traceback):
17321722

17331723
def __exit__(self, exc_type, exc_value, traceback):
17341724
if self._previous_as_current:
1735-
try:
1736-
_current_client.reset(self._previous_as_current)
1737-
except ValueError as e:
1738-
if not e.args[0].endswith(" was created in a different Context"):
1739-
raise # pragma: nocover
1740-
warnings.warn(
1741-
"It is deprecated to enter and exit the Client context "
1742-
"manager from different threads",
1743-
DeprecationWarning,
1744-
stacklevel=2,
1745-
)
1725+
_current_client.reset(self._previous_as_current)
17461726
self.close()
17471727

17481728
def __del__(self):

distributed/tests/test_client.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,30 +1327,22 @@ async def client_2():
13271327
await asyncio.gather(client_1(), client_2())
13281328

13291329

1330-
@gen_cluster(client=False, nthreads=[])
1330+
@gen_cluster(nthreads=[])
13311331
async def test_context_manager_used_from_different_tasks(s):
13321332
c = Client(s.address, asynchronous=True)
13331333
await asyncio.create_task(c.__aenter__())
1334-
with pytest.warns(
1335-
DeprecationWarning,
1336-
match=r"It is deprecated to enter and exit the Client context manager "
1337-
"from different tasks",
1338-
):
1334+
with pytest.raises(ValueError, match="was created in a different Context"):
13391335
await asyncio.create_task(c.__aexit__(None, None, None))
13401336

13411337

1342-
def test_context_manager_used_from_different_threads(s, loop):
1338+
def test_context_manager_used_from_different_threads(s):
13431339
c = Client(s["address"])
13441340
with (
13451341
concurrent.futures.ThreadPoolExecutor(1) as tp1,
13461342
concurrent.futures.ThreadPoolExecutor(1) as tp2,
13471343
):
13481344
tp1.submit(c.__enter__).result()
1349-
with pytest.warns(
1350-
DeprecationWarning,
1351-
match=r"It is deprecated to enter and exit the Client context manager "
1352-
"from different threads",
1353-
):
1345+
with pytest.raises(ValueError, match="was created in a different Context"):
13541346
tp2.submit(c.__exit__, None, None, None).result()
13551347

13561348

0 commit comments

Comments
 (0)