Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

fix(coding-agent): reject reconnect-loop requests on socket close instead of parking them (#1909)

Sebastian Müller15ef45668cdd
diff --git a/packages/coding-agent/.changes/eng-5811-reconnect-park.md b/packages/coding-agent/.changes/eng-5811-reconnect-park.md
new file mode 100644
index 0000000000..f4e7b57295
--- /dev/null
+++ b/packages/coding-agent/.changes/eng-5811-reconnect-park.md
@@ -0,0 +1 @@
+- Fixed a reconnect deadlock where a daemon socket close during recovery or post-update restore parked the reconnect loop's own attach, snapshot, and list requests behind a hello that the stuck loop could never produce ([#1905](https://github.com/PrimeIntellect-ai/prime-agent/issues/1905)).
diff --git a/packages/coding-agent/src/modes/agent-connection/daemon-agent-connection.ts b/packages/coding-agent/src/modes/agent-connection/daemon-agent-connection.ts
index 4f1f0139dd..11486c17f3 100644
--- a/packages/coding-agent/src/modes/agent-connection/daemon-agent-connection.ts
+++ b/packages/coding-agent/src/modes/agent-connection/daemon-agent-connection.ts
@@ -318,37 +318,41 @@ export class DaemonAgentConnection implements AgentConnection {
}
}
- async attach(): Promise<void> {
+ async attach(options?: { recoverable?: boolean }): Promise<void> {
const supportsExtensionUi = this.options.supportsExtensionUi !== false;
- const result = await this.requestData<SessionSummary | DaemonAttachResult>({
- type: "attach",
- activeSessionId: this.activeSessionId,
- supportsExtensionUi,
- clientId: this.clientId,
- capabilities: [
- "attach_snapshot",
- "event_sequence",
- ...(supportsExtensionUi ? (["extension_ui"] as const) : []),
- "slim_attach",
- "chunked_snapshot",
- ...(this.options.ownedSession ? (["client_owned_sessions"] as const) : []),
- ],
- env: this.options.sendClientEnv ? collectDaemonClientEnv() : undefined,
- launchEnv: this.options.ownedSession ? collectDaemonLaunchEnv() : undefined,
- ...(this.options.ownedSession &&
- this.options.ownedSessionRecoveryConfig &&
- this.client.supportsServerCapability("owned_session_recovery_context")
- ? { recoveryConfig: this.options.ownedSessionRecoveryConfig }
- : {}),
- telemetryDisabled: this.options.telemetryDisabled,
- resumeCursor:
- this.lastEventCursor === undefined
- ? undefined
- : {
- activeSessionId: this.activeSessionId,
- ...this.lastEventCursor,
- },
- });
+ const result = await this.requestData<SessionSummary | DaemonAttachResult>(
+ {
+ type: "attach",
+ activeSessionId: this.activeSessionId,
+ supportsExtensionUi,
+ clientId: this.clientId,
+ capabilities: [
+ "attach_snapshot",
+ "event_sequence",
+ ...(supportsExtensionUi ? (["extension_ui"] as const) : []),
+ "slim_attach",
+ "chunked_snapshot",
+ ...(this.options.ownedSession ? (["client_owned_sessions"] as const) : []),
+ ],
+ env: this.options.sendClientEnv ? collectDaemonClientEnv() : undefined,
+ launchEnv: this.options.ownedSession ? collectDaemonLaunchEnv() : undefined,
+ ...(this.options.ownedSession &&
+ this.options.ownedSessionRecoveryConfig &&
+ this.client.supportsServerCapability("owned_session_recovery_context")
+ ? { recoveryConfig: this.options.ownedSessionRecoveryConfig }
+ : {}),
+ telemetryDisabled: this.options.telemetryDisabled,
+ resumeCursor:
+ this.lastEventCursor === undefined
+ ? undefined
+ : {
+ activeSessionId: this.activeSessionId,
+ ...this.lastEventCursor,
+ },
+ },
+ undefined,
+ options,
+ );
this.activeSessionId = getAttachActiveSessionId(result);
const summary = "snapshot" in result ? result.snapshot.summary : result;
this.attachedSessionId = summary.sessionId;
@@ -421,7 +425,7 @@ export class DaemonAgentConnection implements AgentConnection {
});
}
- async getInitialSnapshot(): Promise<AgentConnectionSnapshot> {
+ async getInitialSnapshot(options?: { recoverable?: boolean }): Promise<AgentConnectionSnapshot> {
if (this.latestSnapshotIsFresh && this.latestSnapshot) {
return this.latestSnapshot;
}
@@ -431,18 +435,21 @@ export class DaemonAgentConnection implements AgentConnection {
const snapshotCursor = this.lastEventCursor;
const snapshotSequence = this.lastEventSequence;
const [state, messagesData, sessionContextData] = await Promise.all([
- this.requestData<AgentConnectionState>({
- type: "get_connection_state",
- activeSessionId: this.activeSessionId,
- }),
- this.requestData<{ messages: AgentMessage[] }>({
- type: "get_messages",
- activeSessionId: this.activeSessionId,
- }),
- this.requestData<{ context: AgentConnectionSessionContext }>({
- type: "get_session_context",
- activeSessionId: this.activeSessionId,
- }),
+ this.requestData<AgentConnectionState>(
+ { type: "get_connection_state", activeSessionId: this.activeSessionId },
+ undefined,
+ options,
+ ),
+ this.requestData<{ messages: AgentMessage[] }>(
+ { type: "get_messages", activeSessionId: this.activeSessionId },
+ undefined,
+ options,
+ ),
+ this.requestData<{ context: AgentConnectionSessionContext }>(
+ { type: "get_session_context", activeSessionId: this.activeSessionId },
+ undefined,
+ options,
+ ),
]);
const children = this.latestSnapshot?.children;
const streamingMessage = this.latestSnapshot?.streamingMessage;
@@ -1525,9 +1532,10 @@ export class DaemonAgentConnection implements AgentConnection {
}
await this.client.connect(1000);
await this.client.waitForHello(3000);
- await this.attach();
+ // This loop owns the retry: a socket close must reject these instead of parking them behind a hello it can never produce.
+ await this.attach({ recoverable: false });
if (!this.disposed) {
- const snapshot = await this.getInitialSnapshot();
+ const snapshot = await this.getInitialSnapshot({ recoverable: false });
void this.emit({ type: "session_resynced", snapshot });
void this.emit({ type: "connection_status", status: "connected" });
}
@@ -1825,7 +1833,8 @@ export class DaemonAgentConnection implements AgentConnection {
if (this.disposed) {
return;
}
- const response = await this.client.request({ type: "list" }, 30000);
+ // This loop owns the retry: a socket close must reject these instead of parking them behind a hello it can never produce.
+ const response = await this.client.request({ type: "list" }, 30000, { recoverable: false });
if (this.disposed) {
return;
}
@@ -1847,11 +1856,11 @@ export class DaemonAgentConnection implements AgentConnection {
this.lastEventSequence = undefined;
this.lastEventCursor = undefined;
this.retiredEventGenerations.clear();
- await this.attach();
+ await this.attach({ recoverable: false });
if (this.disposed) {
return;
}
- const snapshot = await this.getInitialSnapshot();
+ const snapshot = await this.getInitialSnapshot({ recoverable: false });
if (this.disposed) {
return;
}
diff --git a/packages/coding-agent/src/modes/daemon/daemon-client.ts b/packages/coding-agent/src/modes/daemon/daemon-client.ts
index 72101718c9..fdfd6255e6 100644
--- a/packages/coding-agent/src/modes/daemon/daemon-client.ts
+++ b/packages/coding-agent/src/modes/daemon/daemon-client.ts
@@ -34,7 +34,11 @@ export type DaemonClientProgressListener = (message: DaemonRequestProgress) => v
export interface DaemonClientRequestOptions {
onProgress?: DaemonClientProgressListener;
- /** False opts out of reconnect parking: a close rejects so the caller's own retry loop stays live. */
+ /**
+ * False opts out of reconnect parking: a close rejects so the caller's own retry loop stays live.
+ * Any caller that owns its own bounded retry MUST pass false; a parked request waits for a hello
+ * that only the caller's stuck loop could produce.
+ */
recoverable?: boolean;
}
diff --git a/packages/coding-agent/test/daemon-agent-connection-reconnect-park.test.ts b/packages/coding-agent/test/daemon-agent-connection-reconnect-park.test.ts
new file mode 100644
index 0000000000..6dfb0b82c2
--- /dev/null
+++ b/packages/coding-agent/test/daemon-agent-connection-reconnect-park.test.ts
@@ -0,0 +1,167 @@
+import { mkdtempSync, rmSync } from "node:fs";
+import { createServer, type Server, type Socket } from "node:net";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { afterEach, describe, expect, it } from "vitest";
+import { DaemonAgentConnection } from "../src/modes/agent-connection/daemon-agent-connection.js";
+import { DaemonClient } from "../src/modes/daemon/daemon-client.js";
+
+const tempDirs: string[] = [];
+const servers: Server[] = [];
+
+afterEach(async () => {
+ for (const server of servers.splice(0)) await new Promise((resolveClose) => server.close(resolveClose));
+ for (const directory of tempDirs.splice(0)) rmSync(directory, { recursive: true, force: true });
+});
+
+const sessionSummary = {
+ id: "active-1",
+ sessionId: "session-1",
+ activeSessionId: "active-1",
+ sessionFile: "/tmp/session-1.jsonl",
+ lifecycle: "live",
+ activity: "idle",
+ isSessionActive: true,
+ cwd: "/tmp/project",
+ isStreaming: false,
+ isCompacting: false,
+ attachedClients: 1,
+ messageCount: 0,
+ sessionActions: { queuedCount: 0, steering: [], followUps: [] },
+};
+
+type ScriptedCommand = { type: string };
+type ConnectionScript = (command: ScriptedCommand, socket: Socket, reply: (data?: unknown) => void) => void;
+
+/** Serves every command normally, except the listed types kill the socket mid-flight instead of replying. */
+function respondAll(cutTypes?: readonly string[]): ConnectionScript {
+ return (command, socket, reply) => {
+ if (cutTypes?.includes(command.type)) {
+ socket.destroy();
+ return;
+ }
+ switch (command.type) {
+ case "attach":
+ reply(sessionSummary);
+ return;
+ case "list":
+ reply({ sessions: [sessionSummary] });
+ return;
+ case "get_connection_state":
+ reply({ sessionId: "session-1", sessionFile: "/tmp/session-1.jsonl" });
+ return;
+ case "get_messages":
+ reply({ messages: [] });
+ return;
+ case "get_session_context":
+ reply({ context: {} });
+ return;
+ default:
+ reply();
+ }
+ };
+}
+
+/** Minimal scripted daemon: one script per accepted connection, replaying the last script for extras. */
+async function startScriptedDaemon(
+ socketPath: string,
+ scripts: readonly ConnectionScript[],
+): Promise<{ sockets: Socket[] }> {
+ const sockets: Socket[] = [];
+ const server = createServer((socket) => {
+ const script = scripts[Math.min(sockets.length, scripts.length - 1)];
+ sockets.push(socket);
+ socket.on("error", () => undefined);
+ socket.write(`${JSON.stringify({ type: "daemon_hello", protocol: { version: 7 }, serverCapabilities: [] })}\n`);
+ let buffered = "";
+ socket.on("data", (chunk: Buffer) => {
+ buffered += chunk.toString("utf8");
+ let newlineIndex = buffered.indexOf("\n");
+ while (newlineIndex !== -1 && !socket.destroyed) {
+ const line = buffered.slice(0, newlineIndex);
+ buffered = buffered.slice(newlineIndex + 1);
+ newlineIndex = buffered.indexOf("\n");
+ if (!line.trim()) continue;
+ const wire = JSON.parse(line) as { id?: string; type?: string; command?: ScriptedCommand };
+ const command = wire.type === "command" && wire.command ? wire.command : (wire as ScriptedCommand);
+ if (command.type === "ack_result") continue;
+ script(command, socket, (data?: unknown) => {
+ socket.write(
+ `${JSON.stringify({ type: "response", id: wire.id, command: command.type, success: true, data })}\n`,
+ );
+ });
+ }
+ });
+ });
+ servers.push(server);
+ await new Promise<void>((resolveListen) => server.listen(socketPath, resolveListen));
+ return { sockets };
+}
+
+function waitForResync(connection: DaemonAgentConnection): Promise<void> {
+ return new Promise<void>((resolveResync) => {
+ connection.subscribe((event) => {
+ if (event.type === "session_resynced") resolveResync();
+ });
+ });
+}
+
+describe("daemon agent connection reconnect parking", () => {
+ it("reconnect loop survives cuts mid-attach and mid-snapshot instead of parking its own requests", async () => {
+ const directory = mkdtempSync(join(tmpdir(), "prime-reconnect-park-"));
+ tempDirs.push(directory);
+ const socketPath = join(directory, "daemon.sock");
+ const { sockets } = await startScriptedDaemon(socketPath, [
+ respondAll(),
+ // Cut mid-attach: unfixed, the request parks behind a hello only this stuck loop could produce.
+ respondAll(["attach"]),
+ // Cut mid-initial-snapshot on the next retry.
+ respondAll(["get_connection_state", "get_messages", "get_session_context"]),
+ respondAll(),
+ ]);
+ const client = new DaemonClient(socketPath);
+ try {
+ await client.connect();
+ await client.waitForHello();
+ const connection = await DaemonAgentConnection.attach(client, "active-1", {
+ recoverDaemon: async () => undefined,
+ reconnectTimeoutMs: 10_000,
+ });
+ const resynced = waitForResync(connection);
+ sockets[0]?.destroy();
+ await expect(resynced).resolves.toBeUndefined();
+ await connection.dispose();
+ } finally {
+ client.close();
+ }
+ }, 15_000);
+
+ it("update-restart restore survives cuts mid-list, mid-attach, and mid-snapshot instead of parking", async () => {
+ const directory = mkdtempSync(join(tmpdir(), "prime-update-park-"));
+ tempDirs.push(directory);
+ const socketPath = join(directory, "daemon.sock");
+ const { sockets } = await startScriptedDaemon(socketPath, [
+ respondAll(),
+ respondAll(["list"]),
+ respondAll(["attach"]),
+ respondAll(["get_connection_state", "get_messages", "get_session_context"]),
+ respondAll(),
+ ]);
+ const client = new DaemonClient(socketPath);
+ try {
+ await client.connect();
+ await client.waitForHello();
+ const connection = await DaemonAgentConnection.attach(client, "active-1", {
+ recoverDaemon: async () => undefined,
+ });
+ const resynced = waitForResync(connection);
+ sockets[0]?.write(`${JSON.stringify({ type: "daemon_closing", reason: "update" })}\n`, () => {
+ sockets[0]?.destroy();
+ });
+ await expect(resynced).resolves.toBeUndefined();
+ await connection.dispose();
+ } finally {
+ client.close();
+ }
+ }, 15_000);
+});