- Add logging import and structured error handling with logger
- Update class docstring to document thread safety guarantees
- Add thread-safe deregister_handler method with proper locking
- Add comprehensive tests for handler deregistration thread safety
- Add test for deregistering non-existent handlers
- Improve error handling with structured logging and exc_info
- Maintain backward compatibility while enhancing functionality
Addresses review suggestions from PR #2992 while keeping the core
thread safety fix intact and adding valuable enhancements.
Co-Authored-By: João <joao@crewai.com>
- Add proper locking using existing _lock mechanism in emit() method
- Add thread safety to register_handler() method
- Add comprehensive thread safety tests for concurrent event emission
- Add tests for concurrent handler registration
- Add tests for mixed concurrent operations
- Fixes issue #2991 where streaming events could get mixed between users
The emit() method now uses the existing _lock to ensure thread-safe access
to the _handlers dictionary and signal emission. This prevents race conditions
that could cause data mixing when multiple users interact with streaming
functionality simultaneously.
Resolves#2991
Co-Authored-By: João <joao@crewai.com>
* build(dev): add pytest-randomly dependency
By randomizing the test execution order, this helps identify tests
that unintentionally depend on shared state or specific execution
order, which can lead to flaky or unreliable test behavior.
* build(dev): add pytest-timeout
This will prevent a test from running indefinitely
* test: block external requests in CI and set default 10s timeout per test
* test: adding missing cassettes
We notice that those cassettes are missing after enabling block-network on CI
* test: increase tests timeout on CI
* test: fix flaky test ValueError: Circular reference detected (id repeated)
* fix: prevent crash when event handler raises exception
Previously, if a registered event handler raised an exception during execution,
it could crash the entire application or interrupt the event dispatch process.
This change wraps handler execution in a try/except block within the `emit` method,
ensuring that exceptions are caught and logged without affecting other handlers or flow.
This improves the resilience of the event bus, especially when handling third-party
or temporary listeners.
- Renamed `CrewEvent` to `BaseEvent` across the codebase for consistency
- Created a `CrewBaseEvent` that automatically identifies fingerprints for DRY
- Added a new `to_json()` method for serializing events
* Support wildcard handling in `emit()`
Change `emit()` to call handlers registered for parent classes using
`isinstance()`. Ensures that base event handlers receive derived
events.
* Fix failing test
* Remove unused variable