fix: Fix event bus thread safety and Subscription hash errors
This commit is contained in:
parent
81c26a07d3
commit
933434fb03
|
|
@ -167,6 +167,16 @@ class Subscription:
|
||||||
priority: EventPriority
|
priority: EventPriority
|
||||||
once: bool
|
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:
|
def matches(self, event: Event) -> bool:
|
||||||
"""Check if this subscription matches an event."""
|
"""Check if this subscription matches an event."""
|
||||||
if self.event_types and event.type not in self.event_types:
|
if self.event_types and event.type not in self.event_types:
|
||||||
|
|
@ -403,9 +413,14 @@ class EventBus:
|
||||||
self._queue.append(event)
|
self._queue.append(event)
|
||||||
self._event_count += 1
|
self._event_count += 1
|
||||||
|
|
||||||
# Process immediately in async context
|
# Process immediately in async context (thread-safe)
|
||||||
if asyncio.get_event_loop().is_running():
|
try:
|
||||||
asyncio.create_task(self._process_event(event))
|
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
|
return event
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue