diff --git a/premium/core/event_bus.py b/premium/core/event_bus.py index 0f3bf7a..173b8c2 100644 --- a/premium/core/event_bus.py +++ b/premium/core/event_bus.py @@ -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(): - asyncio.create_task(self._process_event(event)) + # 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