Skip to content

The agent that grows with you

fix(worker): redial once when the owner closes an idle link before the send

unsupportedpastelse06087d9d252
diff --git a/agent/runtime_session_store.py b/agent/runtime_session_store.py
index 9a6406c06f9..306a87b54eb 100644
--- a/agent/runtime_session_store.py
+++ b/agent/runtime_session_store.py
@@ -80,9 +80,11 @@ class _OwnerLink:
except Exception as exc: # health: allow BLE001 -- handed to the caller, which raises it (ConnectionClosed included)
self.replies.put(exc)
- def call(self, request_id, message, timeout):
+ def send(self, request_id, message):
self.awaiting = request_id
self.ws.send(message)
+
+ def receive(self, timeout):
try:
reply = self.replies.get(timeout=timeout)
except queue.Empty:
@@ -155,12 +157,23 @@ class WorkerRPC:
logger.warning('synchronous_rpc_on_event_loop: %s on thread %s; caller: %s',
method, threading.current_thread().name, _caller_summary())
raise WorkerPersistenceError('synchronous_rpc_on_event_loop')
+ from websockets.exceptions import ConnectionClosed
with self.lock:
self.request_id += 1
request_id = self.request_id
+ message = json.dumps({'jsonrpc': '2.0', 'id': request_id, 'method': method, 'params': params})
try:
- response = self._connection().call(request_id, json.dumps(
- {'jsonrpc': '2.0', 'id': request_id, 'method': method, 'params': params}), 20)
+ link = self._connection()
+ try:
+ link.send(request_id, message)
+ except ConnectionClosed:
+ # websockets raises this from send only before a complete frame is written (closed
+ # on entry, or the socket write failed): the owner closed the link after the idle
+ # check and never saw this request, so redial and send it once more.
+ self._drop()
+ link = self._connection()
+ link.send(request_id, message)
+ response = link.receive(20)
except BaseException:
# Outcome unknown once sent: surface the error and never resend; the next call redials.
self._drop()
diff --git a/tests/agent/test_worker_rpc_connection_reuse.py b/tests/agent/test_worker_rpc_connection_reuse.py
index 2cfcb20af3a..4b1d2668ee6 100644
--- a/tests/agent/test_worker_rpc_connection_reuse.py
+++ b/tests/agent/test_worker_rpc_connection_reuse.py
@@ -57,10 +57,11 @@ def owner(monkeypatch, tmp_path):
import hermes_cli.gateway_runtime_discovery as discovery
state = SimpleNamespace(connections=0, tickets=0, requests=[], drop=None, closed=threading.Event(),
- pongs=[], flooded=threading.Event(), relay=None)
+ pongs=[], flooded=threading.Event(), relay=None, live=None)
def handler(ws):
state.connections += 1
+ state.live = ws
ws.close_timeout = 1
for raw in ws:
request = json.loads(raw)
@@ -150,6 +151,61 @@ def test_in_flight_call_on_a_dropped_connection_fails_and_is_not_resent(owner, t
assert owner.connections == 2
+def _owner_closes_before_the_frame_is_written(owner, monkeypatch):
+ """Boundary hook on the library's send: while ``owner.sabotage`` is set, the idle check has already
+ passed when the owner's close lands (and the worker observes it), just before websockets writes."""
+ from websockets.protocol import State
+ from websockets.sync import client
+ real, dialed = client.connect, []
+
+ def connect(*args, **kwargs):
+ ws = real(*args, **kwargs)
+ dialed.append(ws)
+ real_send = ws.send
+
+ def send(message, *a, **k):
+ if owner.sabotage:
+ owner.sabotage -= 1
+ wait_until(lambda: owner.connections == len(dialed), timeout=5)
+ owner.live.close()
+ wait_until(lambda: ws.protocol.state is State.CLOSED, timeout=5)
+ return real_send(message, *a, **k)
+ ws.send = send
+ return ws
+ monkeypatch.setattr(client, 'connect', connect)
+
+
+def test_owner_close_between_the_idle_check_and_the_send_redials_and_sends_once(owner, tmp_path, monkeypatch):
+ """websockets raises ConnectionClosed from send only before the frame is written: nothing reached
+ the owner, so the call redials once with a fresh ticket instead of failing as outcome-unknown."""
+ from agent.runtime_session_store import WorkerRPC
+ owner.sabotage = 0
+ _owner_closes_before_the_frame_is_written(owner, monkeypatch)
+ rpc = WorkerRPC(tmp_path)
+ assert rpc('worker.persist') == {'n': 1}
+ owner.sabotage = 1
+ assert rpc('worker.persist') == {'n': 2}
+ assert owner.requests == ['worker.persist', 'worker.persist']
+ assert (owner.connections, owner.tickets) == (2, 2)
+ assert rpc('worker.persist') == {'n': 3}
+ assert owner.connections == 2
+
+
+def test_a_redial_whose_send_also_fails_is_not_retried_again(owner, tmp_path, monkeypatch):
+ from websockets.exceptions import ConnectionClosed
+ from agent.runtime_session_store import WorkerRPC
+ owner.sabotage = 0
+ _owner_closes_before_the_frame_is_written(owner, monkeypatch)
+ rpc = WorkerRPC(tmp_path)
+ assert rpc('worker.persist') == {'n': 1}
+ owner.sabotage = 2
+ with pytest.raises(ConnectionClosed):
+ rpc('worker.persist')
+ assert owner.requests == ['worker.persist']
+ assert (owner.connections, owner.tickets) == (2, 2)
+ assert rpc.link is None
+
+
def test_idle_broadcasts_past_the_receive_high_water_mark_keep_the_connection(owner, tmp_path):
"""Events pushed between calls (far over websockets' 16-frame receive mark) are consumed while the
worker is idle, so the owner's keepalive is answered and the next call reuses the connection."""