Skip to content

Newly spawned threads should inherit the interpretation context #758

Description

@jfeser

We use contextvar.ContextVar to store the current interpretation context. These variables are thread-local and their value is not inherited by new threads (python/cpython#128555). This causes some surprising behavior when using APIs that call operations from within spawned threads internally.

from concurrent.futures import ThreadPoolExecutor

from effectful.ops.semantics import handler
from effectful.ops.types import Operation


@Operation.define
def x():
    return 0


with handler({x: lambda: 1}):
    assert x() == 1
    with ThreadPoolExecutor(max_workers=1) as executor:
        assert executor.submit(x).result() == 1

There's active upstream discussion about this issue:

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions