Skip to content

Context variables shared between code don't work with greenlet > 0.4.17 #852

Closed

Description

@c-py

greenlet 0.4.17 introduced support for ContextVars.

It seems the result of this, is that the following code no longer works in bpython:

>>> import contextvars
>>> help = contextvars.ContextVar("helpme")
>>> token = help.set("value")
>>> help.reset(token)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    help.reset(token)
ValueError: <Token var=<ContextVar name='helpme' at 0x10f84b770> at 0x10f603940> was created in a different Context

The workaround is to put all this code in a single function

>>> def do_the_stuff():
...     help = contextvars.ContextVar("helpme")
...     token = help.set("value")
...     help.reset(token)
...     
... 
>>> do_the_stuff()

I believe this might be caused because a new greenlet is created for each line of code in bpython.
Is it possible for everything to run in the same context so that the repl behaviors similar to how a python script would run?

Activity

  1. nhairs commented on Oct 20, 2023

    @nhairs

    I'd like to throw my support behind finding a solution to this without requiring code to be kept as a single "execution block" (as per the above workaround).

    I'm using bpython as an interactive REPL mostly for hand debugging / testing code during development, so being able to write and evaluate different blocks of code and inspecting the various results whilst maintaining the overall context is important to me.

    I'll note that although this issue is tagged with requires-separate-process it may be possible to resolve it whilst continuing to use greenlet - https://greenlet.readthedocs.io/en/latest/contextvars.html (which may be overall less work, but also I'm not super familiar with greenlet or bpython so could be completely wrong).

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions