Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bad interaction with contextvars #362

Open
Tinche opened this issue Oct 24, 2022 · 1 comment
Open

Bad interaction with contextvars #362

Tinche opened this issue Oct 24, 2022 · 1 comment

Comments

@Tinche
Copy link

Tinche commented Oct 24, 2022

Hi!

When do you scheduler.spawn at a certain point in the code, chances are a new task will be spawned from the current task. That new task will inherit the current tasks contextvars.

If you're using contextvars for observability (for example), that's not really what you want. I'd expect the job task to inherit a base context, created when the scheduler is initialized.

The cleanest fix I think would be making a copy of the context when the scheduler is initialized and passing in the context to the new task using https://docs.python.org/3.11/library/asyncio-task.html#asyncio.create_task, but that API is 3.11+ only.

What I've done in the meantime is to spawn a pump task when the scheduler is initialized (so the context is clean), and wrap spawn to pass the given coroutine to the pump task using an asyncio.Queue. The pump task then actually spawns the worker task with the correct context.

@Dreamsorcerer
Copy link
Member

I'm not too familiar with contextvars (having still not found a good use for them), but if you want to propose a PR, we can look at it. A 3.11+ only option would be fine (with a sys.version_info check).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants