{"record":{"id":"0534a9607d13feed","repo":"t8y2/dbx","slug":"agent-session-already-exists-s-0534a9","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/kingbase-go/main.go","lineNumber":289,"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, cp 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\ts := newServer()\n\tif err := s.connect(cp); err != nil {\n\t\treturn err\n\t}\n\tr.mu.Lock()\n\tdefer r.mu.Unlock()\n\tif _, exists := r.sessions[id]; exists {\n\t\t_ = s.disconnect()\n\t\treturn fmt.Errorf(\"agent session already exists: %s\", id)\n\t}\n\tr.sessions[id] = &agentSession{server: s}","sourceCodeStart":271,"sourceCodeEnd":307,"githubUrl":"https://github.com/t8y2/dbx/blob/c0390bff16418b651f4728520d99adf8ce48829a/agents/drivers/kingbase-go/main.go#L271-L307","documentation":"The agent runtime server refuses to open a session whose ID is already registered in r.sessions. This check happens before any connection work, under the runtime mutex, so duplicate openSession calls with the same id fail fast.","triggerScenarios":"Calling the runtime's session-open method (e.g. 'open' JSON-RPC method) with an id that was previously opened and not yet closed; a client retrying an open request after a timeout while the first session is still alive.","commonSituations":"Agent orchestrators that reuse fixed session ids across reconnects without closing the old session; concurrent workers racing to open the same session id; stale sessions left open after a crashed client.","solutions":["Use a unique session id for each open request (e.g. UUID)","Call closeSession/'close' for the existing id before reopening","Treat the error as a duplicate and reuse the existing session instead of opening a new one","If the old session is stale, call 'shutdown' or restart the runtime to clear r.sessions"],"exampleFix":"// before: fixed id reused every run\nid := \"main\"\nif err := rt.OpenSession(id, cp); err != nil { return err }\n// after: close-then-open or unique id\nif err := rt.CloseSession(id); err != nil { /* ignore not-found */ }\nif err := rt.OpenSession(id, cp); err != nil { return err }","handlingStrategy":"try-catch","validationCode":"// Go: check id availability client-side if the runtime exposes listing\nids := rt.ListSessionIDs() // or track locally\nfor _, existing := range ids {\n    if existing == id {\n        return fmt.Errorf(\"session %s already open; close it first\", id)\n    }\n}","typeGuard":null,"tryCatchPattern":"err := rt.OpenSession(id, cp)\nif err != nil && strings.Contains(err.Error(), \"already exists\") {\n    // idempotent: reuse the existing session\n    return rt.Session(id)\n}","preventionTips":["Generate UUID session ids per open","Always close sessions, including on error paths","Track session lifecycle client-side to avoid duplicate opens"],"tags":["kingbase","agent-runtime","session-management","duplicate","go"],"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"}