{"record":{"id":"154f3b9cf3bea683","repo":"t8y2/dbx","slug":"agent-session-already-exists-s-154f3b","errorCode":null,"errorMessage":"agent session already exists: %s","messagePattern":"agent session already exists: (.+?)","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"agents/drivers/neo4j-go/main.go","lineNumber":264,"sourceCode":"\t\tid := stringParam(params, \"agentSessionId\")\n\t\tif id == \"\" {\n\t\t\tid = legacyAgentSessionID\n\t\t}\n\t\tsession, err := r.session(id)\n\t\tif err != nil {\n\t\t\treturn nil, false, err\n\t\t}\n\t\tsession.mu.Lock()\n\t\tdefer session.mu.Unlock()\n\t\treturn session.server.dispatch(method, params)\n\t}\n}\n\nfunc (r *runtimeServer) openSession(id string, params connectParams) error {\n\tr.mu.Lock()\n\tif _, exists := r.sessions[id]; exists {\n\t\tr.mu.Unlock()\n\t\treturn fmt.Errorf(\"agent session already exists: %s\", id)\n\t}\n\tif len(r.sessions) >= maxAgentSessions {\n\t\tr.mu.Unlock()\n\t\treturn fmt.Errorf(\"agent session limit reached: %d\", maxAgentSessions)\n\t}\n\tr.mu.Unlock()\n\n\truntime, key, err := r.acquireRuntime(params)\n\tif err != nil {\n\t\treturn err\n\t}\n\tserver := newServer(runtime, params)\n\tif err := server.validateConnection(); err != nil {\n\t\tr.releaseRuntime(key)\n\t\treturn err\n\t}\n\n\tr.mu.Lock()","sourceCodeStart":246,"sourceCodeEnd":282,"githubUrl":"https://github.com/t8y2/dbx/blob/c0390bff16418b651f4728520d99adf8ce48829a/agents/drivers/neo4j-go/main.go#L246-L282","documentation":"openSession registers each agent session under a caller-supplied id in a protected map. If openSession is called with an id that is already registered, the server refuses to overwrite the existing session and returns this error instead. It prevents silent session replacement and leaked runtime resources.","triggerScenarios":"Calling openSession (via the connect/open-session RPC method) with an id string that was previously used and not yet closed. The check happens under r.mu before any runtime is acquired, so the duplicate id is detected immediately.","commonSituations":"Client retries a connect call after a network blip without closing the first session; two workers configured with the same session id; client state was lost (restart) so it reuses an id the server still holds.","solutions":["Call closeSession/disconnect for the existing id before opening a new session with the same id","Generate unique session ids (e.g. UUID) per client connection","On the client, treat this error as 'already connected' and reuse the existing session rather than retrying blindly","Restart the agent process if stale sessions cannot be closed through the protocol"],"exampleFix":"// before\nserver.call(\"connect\", map[string]any{\"session_id\": \"main\"}) // retry after reconnect\n// after\nif err := server.call(\"connect\", map[string]any{\"session_id\": id}); err != nil && strings.Contains(err.Error(), \"already exists\") {\n    server.call(\"disconnect\", nil)\n    server.call(\"connect\", map[string]any{\"session_id\": id})\n}","handlingStrategy":"try-catch","validationCode":"// client-side: track open ids before calling connect\nif _, open := openSessions[id]; open {\n    return fmt.Errorf(\"refusing to connect: session %s already open\", id)\n}","typeGuard":null,"tryCatchPattern":"resp, err := call(\"connect\", params)\nif err != nil {\n    if strings.Contains(err.Error(), \"agent session already exists\") {\n        // reuse or recycle: disconnect then reconnect once\n        call(\"disconnect\", map[string]any{\"id\": id})\n        resp, err = call(\"connect\", params)\n    }\n    if err != nil { return err }\n}","preventionTips":["Generate unique session ids (UUID) per client process","Always disconnect in a defer/cleanup path","Handle reconnects by closing before re-opening","Avoid hardcoding shared session ids in multi-worker configs"],"tags":["neo4j","session","duplicate-id","rpc"],"backgroundTag":"session-already-exists","analyzedSha":"c0390bff16418b651f4728520d99adf8ce48829a","analyzedAt":"2026-09-05T23:05:10.900Z","contentChangedAt":"2026-09-05T23:05:10.900Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}