Skip to content

Commit

Permalink
Preserve contextvars during comm offload (#5486)
Browse files Browse the repository at this point in the history
  • Loading branch information
gjoseph92 authored Nov 15, 2021
1 parent 5a75023 commit f6796f7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
13 changes: 13 additions & 0 deletions distributed/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import array
import asyncio
import contextvars
import functools
import io
import os
Expand Down Expand Up @@ -554,6 +555,18 @@ async def test_offload():
assert (await offload(lambda x, y: x + y, 1, y=2)) == 3


@pytest.mark.asyncio
async def test_offload_preserves_contextvars():
var = contextvars.ContextVar("var")

async def set_var(v: str):
var.set(v)
r = await offload(var.get)
assert r == v

await asyncio.gather(set_var("foo"), set_var("bar"))


def test_serialize_for_cli_deprecated():
with pytest.warns(FutureWarning, match="serialize_for_cli is deprecated"):
from distributed.utils import serialize_for_cli
Expand Down
7 changes: 6 additions & 1 deletion distributed/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import asyncio
import contextvars
import functools
import importlib
import inspect
Expand Down Expand Up @@ -1326,7 +1327,11 @@ def import_term(name: str):

async def offload(fn, *args, **kwargs):
loop = asyncio.get_event_loop()
return await loop.run_in_executor(_offload_executor, lambda: fn(*args, **kwargs))
# Retain context vars while deserializing; see https://bugs.python.org/issue34014
context = contextvars.copy_context()
return await loop.run_in_executor(
_offload_executor, lambda: context.run(fn, *args, **kwargs)
)


class EmptyContext:
Expand Down

0 comments on commit f6796f7

Please sign in to comment.