-
Notifications
You must be signed in to change notification settings - Fork 7.4k
fix(flow): lock nested pydantic models in StateProxy #6036
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
Open
ImmortalDemonGod
wants to merge
1
commit into
crewAIInc:main
Choose a base branch
from
ImmortalDemonGod:fix/state-proxy-nested-model-lock
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+208
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Callable attributes currently execute outside the lock.
LockedModelProxy.__getattr__fetches attributes under lock, but if the value is a bound method/callable, invocation happens after the lock is released. Any mutating model method can still race and bypass serialization.Suggested fix
@@ def __getattr__(self, name: str) -> Any: @@ with lock: value = getattr(model, name) + if callable(value): + def _locked_call(*args: Any, **kwargs: Any) -> Any: + with lock: + result = value(*args, **kwargs) + if isinstance(result, list): + return LockedListProxy(result, lock) + if isinstance(result, dict): + return LockedDictProxy(result, lock) + if isinstance(result, BaseModel): + return LockedModelProxy(result, lock) + return result + return _locked_call + if isinstance(value, list): return LockedListProxy(value, lock) if isinstance(value, dict): return LockedDictProxy(value, lock) if isinstance(value, BaseModel): return LockedModelProxy(value, lock) return value📝 Committable suggestion
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're a sharp bot, and you're right that the lock doesn't currently extend to method invocation:
__getattr__releases the lock before returning a callable, so a mutating method called through the proxy would run unsynchronized.I'm scoping this out of this PR deliberately, for two reasons.
Scope. This PR closes the observed race, which is attribute-level mutation of nested flow state (the assignment /
__setattr__path). Serializing arbitrary method invocation on the state model is a broader guarantee that changes the proxy's contract, and I'd rather keep the blast radius of this fix tight.Correctness of the suggested form.
_state_lockis a plain, non-reentrantthreading.Lock. Wrapping callables to execute under that lock would deadlock whenever the invoked method reads or writes its own attributes, since that re-enters__getattr__/__setattr__and tries to acquire the same lock the wrapper already holds. Pydantic model methods do this routinely, so the wrapper would hang real call paths rather than protect them.Doing it safely means first moving
_state_lock(and the proxy lock) tothreading.RLock, then adding the callable wrapper with re-fetch-under-lock semantics and concurrency tests for the re-entrant paths. That's a coherent follow-up if you'd like full method-call serialization, and I'm glad to open one, but it's a distinct change from the assignment-race fix here. In current flow usage, state is mutated by assignment (now locked), not by methods called through the proxy.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ImmortalDemonGod, that's a well-reasoned scoping call, and the deadlock point is a genuine correctness flaw in my suggestion — I hadn't accounted for the re-entrant access pattern that Pydantic model methods trigger. A non-reentrantthreading.Lockplus a callable wrapper would hang on the first method call that touches its own attributes, which is nearly all of them. The right prerequisite is exactly what you described: promote_state_lock(and the proxies) tothreading.RLockand validate the re-entrant paths explicitly before adding the wrapper.Happy to open a follow-up GitHub issue to track the full method-call serialization work (RLock migration + callable wrapper + re-entrant concurrency tests). Would you like me to do that?
🐇 (thoughtful nod)
✏️ Learnings added