mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-14 18:48:29 +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:
@@ -807,6 +807,13 @@ class Flow(Generic[T], metaclass=FlowMeta):
|
||||
async def _execute_method(
|
||||
self, method_name: str, method: Callable, *args: Any, **kwargs: Any
|
||||
) -> Any:
|
||||
# Serialize state before event emission to avoid pickling issues
|
||||
state_copy = (
|
||||
type(self._state)(**self._state.model_dump())
|
||||
if isinstance(self._state, BaseModel)
|
||||
else dict(self._state)
|
||||
)
|
||||
|
||||
dumped_params = {f"_{i}": arg for i, arg in enumerate(args)} | (kwargs or {})
|
||||
self.event_emitter.send(
|
||||
self,
|
||||
@@ -815,7 +822,7 @@ class Flow(Generic[T], metaclass=FlowMeta):
|
||||
method_name=method_name,
|
||||
flow_name=self.__class__.__name__,
|
||||
params=dumped_params,
|
||||
state=self._copy_state(),
|
||||
state=state_copy,
|
||||
),
|
||||
)
|
||||
|
||||
@@ -829,13 +836,20 @@ class Flow(Generic[T], metaclass=FlowMeta):
|
||||
self._method_execution_counts.get(method_name, 0) + 1
|
||||
)
|
||||
|
||||
# Serialize state after execution
|
||||
state_copy = (
|
||||
type(self._state)(**self._state.model_dump())
|
||||
if isinstance(self._state, BaseModel)
|
||||
else dict(self._state)
|
||||
)
|
||||
|
||||
self.event_emitter.send(
|
||||
self,
|
||||
event=MethodExecutionFinishedEvent(
|
||||
type="method_execution_finished",
|
||||
method_name=method_name,
|
||||
flow_name=self.__class__.__name__,
|
||||
state=self._copy_state(),
|
||||
state=state_copy,
|
||||
result=result,
|
||||
),
|
||||
)
|
||||
|
||||
@@ -26,6 +26,12 @@ class MethodExecutionStartedEvent(Event):
|
||||
state: Union[Dict[str, Any], BaseModel]
|
||||
params: Optional[Dict[str, Any]] = None
|
||||
|
||||
def __post_init__(self):
|
||||
super().__post_init__()
|
||||
# Create a new instance of BaseModel state to avoid pickling issues
|
||||
if isinstance(self.state, BaseModel):
|
||||
self.state = type(self.state)(**self.state.model_dump())
|
||||
|
||||
|
||||
@dataclass
|
||||
class MethodExecutionFinishedEvent(Event):
|
||||
@@ -33,6 +39,12 @@ class MethodExecutionFinishedEvent(Event):
|
||||
state: Union[Dict[str, Any], BaseModel]
|
||||
result: Any = None
|
||||
|
||||
def __post_init__(self):
|
||||
super().__post_init__()
|
||||
# Create a new instance of BaseModel state to avoid pickling issues
|
||||
if isinstance(self.state, BaseModel):
|
||||
self.state = type(self.state)(**self.state.model_dump())
|
||||
|
||||
|
||||
@dataclass
|
||||
class FlowFinishedEvent(Event):
|
||||
|
||||
Reference in New Issue
Block a user