Skip to content

Fixing 'active_steps' getting mixed in async operations #161

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

Closed
wants to merge 1 commit into from

Conversation

emersonqueiroz
Copy link

Problem
I noticed that some steps were being assigned to the wrong threads, as shown in the screenshot below. After investigating the implementation, I discovered that the active_steps_var ContextVar was sometimes being mixed during parallel executions.

Screenshot 2025-01-22 at 13 31 51

This issue seems similar to the common Python gotcha where using mutable objects as default values can lead to unexpected behavior. Specifically, it's not safe to use mutable objects as the default value for ContextVars in the current implementation:

Solution
Use a non-mutable default value (such as None) for the ContextVar and provide an empty list when retrieving its value. This approach prevents the mixing of ContextVars during parallel executions.

active_steps_var = ContextVar[List["Step"]]("active_steps", default=None)
active_steps_var.get([])  # Provide an empty list as the default value when getting the variable

Code examples

@willydouhard
Copy link
Contributor

Thank you for opening this, I ended up opening #165 which is inspired by this PR.

@emersonqueiroz
Copy link
Author

Hi @willydouhard, I tested it and it seems to be working, thanks!

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

Successfully merging this pull request may close these issues.

2 participants