{"record":{"id":"c36effaf50b9167d","repo":"t8y2/dbx","slug":"agent-session-already-exists-s-c36eff","errorCode":null,"errorMessage":"agent session already exists: %s","messagePattern":"agent session already exists: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"agents/drivers/vastbase-go/main.go","lineNumber":306,"sourceCode":"\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\trelease, permitErr := session.server.acquireOperationPermit(method)\n\t\tif permitErr != nil {\n\t\t\treturn nil, false, permitErr\n\t\t}\n\t\tdefer release()\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\tconnectionRuntime, runtimeKey := r.acquireConnectionRuntime(cp)\n\ts := newServer()\n\tif err := s.connectWithRuntime(cp, connectionRuntime); err != nil {\n\t\tr.releaseConnectionRuntime(runtimeKey)\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\tr.releaseConnectionRuntime(runtimeKey)","sourceCodeStart":288,"sourceCodeEnd":324,"githubUrl":"https://github.com/t8y2/dbx/blob/c0390bff16418b651f4728520d99adf8ce48829a/agents/drivers/vastbase-go/main.go#L288-L324","documentation":"openSession rejects a new agent session when an entry with the same id already exists in runtimeServer.sessions. The id is caller-supplied, so this error means the caller is reusing a session identifier for a session that is still registered (not closed).","triggerScenarios":"Calling openSession twice with the same id without closeSession in between; a prior session with that id failed mid-way but was still registered; concurrent opens racing with the same id (only one wins, the other gets this error even if intended).","commonSituations":"Client reconnect logic reusing a fixed session id after an unclean shutdown; a crashed previous run leaving state if the runtime server is long-lived; generating ids from stable keys (host+port) instead of unique values.","solutions":["Call closeSession(id) before reopening, or check-and-reuse the existing session via session(id) instead of openSession.","Generate a unique session id per connection (e.g. a UUID) so reopens never collide.","If the old session is stale, close it explicitly then retry the open.","Guard concurrent opens with application-level locking or single-flight around openSession."],"exampleFix":"// before\nif err := r.openSession(\"agent-1\", cp); err != nil {\n    return err\n}\n// after\nif _, err := r.session(\"agent-1\"); err == nil {\n    // session already open, reuse it\n    return nil\n}\nreturn r.openSession(fmt.Sprintf(\"agent-%d\", time.Now().UnixNano()), cp)","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"err := r.openSession(id, cp)\nif err != nil {\n    if strings.HasPrefix(err.Error(), \"agent session already exists\") {\n        // reuse or recycle the existing session\n        if s, serr := r.session(id); serr == nil {\n            use(s)\n            return nil\n        }\n        _ = r.closeSession(id)\n        return r.openSession(id, cp)\n    }\n    return err\n}","preventionTips":["Always pair openSession with closeSession (defer it).","Generate unique session ids (UUID) instead of stable keys.","Treat existing-session as reuse, not failure, in reconnect logic.","Single-flight concurrent opens for the same id."],"tags":["session-management","duplicate-key","concurrency"],"backgroundTag":"duplicate-session-id","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"}