mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-11 00:58:30 +00:00
fix: Handle thread locks in Flow state serialization
- Add state serialization in Flow events to avoid pickling RLock objects - Update event emission to use serialized state - Add test case for Flow with thread locks Fixes #2120 Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
@@ -348,6 +348,35 @@ def test_flow_uuid_structured():
|
||||
assert flow.state.message == "final"
|
||||
|
||||
|
||||
def test_flow_with_thread_lock():
|
||||
"""Test that Flow properly handles thread locks in state."""
|
||||
import threading
|
||||
|
||||
class LockFlow(Flow):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.lock = threading.RLock()
|
||||
self.counter = 0
|
||||
|
||||
@start()
|
||||
async def step_1(self):
|
||||
with self.lock:
|
||||
self.counter += 1
|
||||
return "step 1"
|
||||
|
||||
@listen(step_1)
|
||||
async def step_2(self, result):
|
||||
with self.lock:
|
||||
self.counter += 1
|
||||
return result + " -> step 2"
|
||||
|
||||
flow = LockFlow()
|
||||
result = flow.kickoff()
|
||||
|
||||
assert result == "step 1 -> step 2"
|
||||
assert flow.counter == 2
|
||||
|
||||
|
||||
def test_router_with_multiple_conditions():
|
||||
"""Test a router that triggers when any of multiple steps complete (OR condition),
|
||||
and another router that triggers only after all specified steps complete (AND condition).
|
||||
|
||||
Reference in New Issue
Block a user