Skip to content
This repository was archived by the owner on Aug 5, 2025. It is now read-only.

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 subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants