{"record":{"id":"e097d141526669f8","repo":"louislam/dockge","slug":"socket-client-not-found-for-endpoint","errorCode":null,"errorMessage":"Socket client not found for endpoint: ","messagePattern":"Socket client not found for endpoint: ","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"backend/agent-manager.ts","lineNumber":253,"sourceCode":"        for (let endpoint in list) {\n            let agent = list[endpoint];\n            this.connect(agent.url, agent.username, agent.password);\n        }\n    }\n\n    disconnectAll() {\n        for (let endpoint in this.agentSocketList) {\n            this.disconnect(endpoint);\n        }\n    }\n\n    async emitToEndpoint(endpoint: string, eventName: string, ...args : unknown[]) {\n        log.debug(\"agent-manager\", \"Emitting event to endpoint: \" + endpoint);\n        let client = this.agentSocketList[endpoint];\n\n        if (!client) {\n            log.error(\"agent-manager\", \"Socket client not found for endpoint: \" + endpoint);\n            throw new Error(\"Socket client not found for endpoint: \" + endpoint);\n        }\n\n        if (!client.connected || !this.agentLoggedInList[endpoint]) {\n            // Maybe the request is too quick, the socket is not connected yet, check firstConnectTime\n            // If it is within 10 seconds, we should apply retry logic here\n            let diff = dayjs().diff(this.firstConnectTime, \"second\");\n            log.debug(\"agent-manager\", endpoint + \": diff: \" + diff);\n            let ok = false;\n            while (diff < 10) {\n                if (client.connected && this.agentLoggedInList[endpoint]) {\n                    log.debug(\"agent-manager\", `${endpoint}: Connected & Logged in`);\n                    ok = true;\n                    break;\n                }\n                log.debug(\"agent-manager\", endpoint + \": not ready yet, retrying in 1 second...\");\n                await sleep(1000);\n                diff = dayjs().diff(this.firstConnectTime, \"second\");\n            }","sourceCodeStart":235,"sourceCodeEnd":271,"githubUrl":"https://github.com/louislam/dockge/blob/f809ae192b571944ad773e9866d3e67064ae8043/backend/agent-manager.ts#L235-L271","documentation":"emitToEndpoint() resolves the socket.io client for an agent endpoint from this.agentSocketList[endpoint]. If there is no connected client registered under that endpoint key, it logs an error and throws 'Socket client not found for endpoint: <endpoint>' — the agent is not connected at all, so emitting is impossible.","triggerScenarios":"emitToEndpoint (or emitToAllEndpoints) called with an endpoint whose connect() was never run, whose client was deleted by remove(), or whose key differs (new URL(url).host vs raw url, port included/omitted) from the key used in agentSocketList.","commonSituations":"Emitting right after adding an agent before connect() finished; sending commands to an agent that was just removed; race where the frontend emits before the agent list finished loading; URL normalization mismatch so the endpoint string never matches the map key.","solutions":["Confirm connect(url,...) was called and completed for this endpoint before emitting","Check that the endpoint string equals new URL(agentUrl).host exactly (host+port, no scheme/path)","Verify the agent was not removed (remove() deletes agentSocketList[endpoint]) and that a connect is in progress, then retry after connection is established"],"exampleFix":"// before\nawait agentManager.emitToEndpoint(endpoint, \"requestStackList\");\n// after\nif (agentManager.hasEndpoint(endpoint)) { // or track connected endpoints client-side\n    await agentManager.emitToEndpoint(endpoint, \"requestStackList\");\n} else {\n    await agentManager.connect(agentUrl, username, password);\n    await agentManager.emitToEndpoint(endpoint, \"requestStackList\");\n}","handlingStrategy":"try-catch","validationCode":"const endpoint = new URL(agentUrl).host;\nif (!endpoint) throw new Error(\"Invalid agent URL\");\n// only emit to endpoints known to be connected\nif (!connectedEndpoints.has(endpoint)) {\n    await agentManager.connect(agentUrl, username, password);\n}","typeGuard":"function isValidEndpoint(endpoint: unknown): endpoint is string {\n    return typeof endpoint === \"string\" && endpoint.length > 0 && endpoint.includes(\":\") === false ? new URL(\"http://\" + endpoint).host === endpoint : true;\n}","tryCatchPattern":"try {\n    await agentManager.emitToEndpoint(endpoint, eventName, ...args);\n} catch (e) {\n    if (String(e.message).startsWith(\"Socket client not found\")) {\n        // trigger reconnect for this endpoint, then retry once\n    } else throw e;\n}","preventionTips":["Only emit after the agent status is 'connected'","Use new URL(url).host as the endpoint key everywhere","Never emit to endpoints that were removed","Track connected endpoints client-side before calling emitToAllEndpoints"],"tags":["socket","agent-manager","connection"],"backgroundTag":"socket-client-not-found","analyzedSha":"f809ae192b571944ad773e9866d3e67064ae8043","analyzedAt":"2026-08-31T19:13:50.919Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}