Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

fix(agent): drop stale socket and fail in-flight commands on reconnect

Juan Denise9172f7af687
diff --git a/backend/app/services/agent_registry.py b/backend/app/services/agent_registry.py
index 78da565f..4cfbf286 100644
--- a/backend/app/services/agent_registry.py
+++ b/backend/app/services/agent_registry.py
@@ -280,14 +280,51 @@ class AgentRegistry:
)
with self._lock:
- # Remove old connection if exists
+ # Replace any existing connection for this server. Capture the old
+ # agent's in-flight commands and socket so we can clean them up
+ # AFTER releasing the lock — doing socket I/O under the lock risks
+ # deadlocking with the disconnect handler, which also takes it.
old_agent = self._agents.get(server_id)
if old_agent:
self._socket_to_server.pop(old_agent.socket_id, None)
+ old_pending = list(old_agent.pending_commands.values())
+ old_agent.pending_commands.clear()
+ old_socket_id = old_agent.socket_id
+ old_transport = old_agent.transport
+ else:
+ old_pending = []
+ old_socket_id = None
+ old_transport = None
self._agents[server_id] = agent
self._socket_to_server[socket_id] = server_id
+ # The previous connection for this server (if any) is now orphaned by
+ # this reconnect. Fail its in-flight commands so callers blocked in
+ # send_command() return immediately with AGENT_RECONNECTED instead of
+ # waiting out the full timeout — the late result would resolve against
+ # the new agent and be unreachable anyway. Then drop the stale WS
+ # socket so the agent isn't left holding two live connections.
+ # (unregister_agent keys off socket_id and we already removed the old
+ # one from _socket_to_server, so the resulting disconnect handler is a
+ # no-op for the freshly-registered agent.)
+ for pending in old_pending:
+ try:
+ pending.result_queue.put({
+ 'success': False,
+ 'error': 'Agent reconnected',
+ 'code': 'AGENT_RECONNECTED',
+ })
+ except Exception:
+ logger.exception("Error failing pending command on reconnect")
+ if (old_socket_id and old_socket_id != socket_id
+ and old_transport == 'ws' and self._socketio is not None):
+ try:
+ self._socketio.server.disconnect(old_socket_id, namespace='/agent')
+ except Exception:
+ logger.debug("Old socket %s already disconnected on reconnect",
+ old_socket_id, exc_info=True)
+
# Update database
try:
server = Server.query.get(server_id)
diff --git a/backend/tests/test_agent_registry_security.py b/backend/tests/test_agent_registry_security.py
index 58e6de97..a3527e8f 100644
--- a/backend/tests/test_agent_registry_security.py
+++ b/backend/tests/test_agent_registry_security.py
@@ -170,3 +170,64 @@ def test_reaper_evicts_genuinely_stale_agent(app, clean_registry):
assert agent_registry.is_agent_connected(sid) is False
assert Server.query.get(sid).status == "offline"
+
+
+# --------------------------------------------------------------------------
+# Bug #4 — reconnect must fail in-flight commands and drop the stale socket
+# --------------------------------------------------------------------------
+
+def test_register_agent_fails_old_pending_commands_on_reconnect(app, clean_registry):
+ from app.services.agent_registry import PendingCommand
+
+ s = Server(name="t", agent_id="agent-reconpending")
+ _db.session.add(s)
+ _db.session.commit()
+ sid = s.id
+
+ agent_registry.register_agent(sid, "socket-OLD", "203.0.113.7", "1.0.0")
+ old = agent_registry.get_agent(sid)
+ pending = PendingCommand(command_id="cmd1", action="noop", params={})
+ with agent_registry._lock:
+ old.pending_commands["cmd1"] = pending
+
+ # Agent reconnects on a new socket while a command is in flight.
+ agent_registry.register_agent(sid, "socket-NEW", "203.0.113.7", "1.0.0")
+
+ # The caller blocked in send_command() gets an immediate failure rather
+ # than waiting out the full timeout.
+ res = pending.result_queue.get(timeout=2)
+ assert res["success"] is False
+ assert res["code"] == "AGENT_RECONNECTED"
+ assert agent_registry.get_agent(sid).socket_id == "socket-NEW"
+
+
+def test_register_agent_disconnects_old_ws_socket(app, clean_registry):
+ class _FakeServer:
+ def __init__(self):
+ self.calls = []
+
+ def disconnect(self, sid, namespace=None):
+ self.calls.append((sid, namespace))
+
+ class _FakeSocketIO:
+ def __init__(self):
+ self.server = _FakeServer()
+
+ def emit(self, *a, **k):
+ pass
+
+ fake = _FakeSocketIO()
+ prev = agent_registry._socketio
+ agent_registry._socketio = fake
+ try:
+ s = Server(name="t", agent_id="agent-recondisc")
+ _db.session.add(s)
+ _db.session.commit()
+ sid = s.id
+
+ agent_registry.register_agent(sid, "socket-OLD", "1.2.3.4", "1.0.0")
+ agent_registry.register_agent(sid, "socket-NEW", "1.2.3.4", "1.0.0")
+
+ assert ("socket-OLD", "/agent") in fake.server.calls
+ finally:
+ agent_registry._socketio = prev