Skip to content

The agent that grows with you

fix(gateway): isolate failing session event observers

David Dudok de Wit4758aa1c9b2c
diff --git a/gateway/session_events.py b/gateway/session_events.py
index 870100e422c..77475d89233 100644
--- a/gateway/session_events.py
+++ b/gateway/session_events.py
@@ -1,5 +1,6 @@
"""Authority-local subscription ordering over the existing bounded transports/replay."""
from copy import deepcopy
+import logging
import threading
import uuid
@@ -61,10 +62,16 @@ class SessionEvents:
'payload': {'replay_epoch': self.epoch, 'latest_seq': self.sequence}}}
delivered = self.fanout.write(frame, overflow=overflow)
for observer in tuple(self.observers):
- observer(frame)
+ try:
+ observer(frame)
+ delivered = True
+ except Exception:
+ # A request-owned projection cannot undo committed publication or starve peers.
+ logging.getLogger(__name__).warning('Session event observer failed for %s', session_id,
+ exc_info=True)
# True when a live viewer or observer took the frame; the replay ring alone is not
# delivery (a detached finite viewer reads nothing until it reattaches).
- return delivered or bool(self.observers)
+ return delivered
def since(self, epoch, sequence):
with self.lock:
diff --git a/tests/gateway/test_session_event_observer_isolation.py b/tests/gateway/test_session_event_observer_isolation.py
new file mode 100644
index 00000000000..029fe34dd5c
--- /dev/null
+++ b/tests/gateway/test_session_event_observer_isolation.py
@@ -0,0 +1,21 @@
+"""A failed same-thread projection cannot abort canonical event publication."""
+import pytest
+
+from gateway.session_events import SessionEvents
+
+
[email protected]('healthy', [False, True])
+def test_failed_observer_isolated_and_not_counted_as_delivery(healthy, caplog):
+ stream = SessionEvents()
+ received = []
+ def broken(frame):
+ raise RuntimeError('viewer detached')
+ stream.observers.add(broken)
+ if healthy:
+ stream.observers.add(lambda frame: received.append(frame))
+ assert stream.publish('session', {'text': 'committed'}) is healthy
+ assert bool(received) is healthy
+ replay = stream.since(stream.epoch, 0)
+ assert replay['count'] == 1
+ assert replay['events'][0]['payload']['text'] == 'committed'
+ assert 'observer' in caplog.text.lower()