fix: Fix event bus thread safety and Subscription hash errors

This commit is contained in:
devmatrix 2026-02-16 23:58:22 +00:00
parent 81c26a07d3
commit 933434fb03
1 changed files with 18 additions and 3 deletions

View File

@ -167,6 +167,16 @@ class Subscription:
priority: EventPriority
once: bool
def __hash__(self) -> int:
"""Make subscription hashable (use id only)."""
return hash(self.id)
def __eq__(self, other) -> bool:
"""Compare subscriptions by id."""
if not isinstance(other, Subscription):
return False
return self.id == other.id
def matches(self, event: Event) -> bool:
"""Check if this subscription matches an event."""
if self.event_types and event.type not in self.event_types:
@ -403,9 +413,14 @@ class EventBus:
self._queue.append(event)
self._event_count += 1
# Process immediately in async context
if asyncio.get_event_loop().is_running():
# Process immediately in async context (thread-safe)
try:
loop = asyncio.get_running_loop()
if loop.is_running():
asyncio.create_task(self._process_event(event))
except RuntimeError:
# No event loop running in this thread - queue for later processing
pass
return event