{"record":{"id":"34012824eb8012b0","repo":"t8y2/dbx","slug":"agent-session-already-exists-s-340128","errorCode":null,"errorMessage":"agent session already exists: %s","messagePattern":"agent session already exists: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"agents/drivers/xugu/main.go","lineNumber":851,"sourceCode":"\t}\n}\n\nfunc (r *runtimeServer) withSession(agentSessionID, method string, params map[string]json.RawMessage) (any, bool, error) {\n\tsession, err := r.session(agentSessionID)\n\tif err != nil {\n\t\treturn nil, false, err\n\t}\n\t// Database, schema, transaction, and cursor state are connection-scoped.\n\tsession.mu.Lock()\n\tdefer session.mu.Unlock()\n\treturn session.server.dispatch(method, params)\n}\n\nfunc (r *runtimeServer) openSession(agentSessionID string, params connectParams) error {\n\tr.mu.Lock()\n\tif _, exists := r.sessions[agentSessionID]; exists {\n\t\tr.mu.Unlock()\n\t\treturn fmt.Errorf(\"agent session already exists: %s\", agentSessionID)\n\t}\n\tif len(r.sessions) >= maxAgentSessions {\n\t\tr.mu.Unlock()\n\t\treturn fmt.Errorf(\"%w: %d\", errAgentSessionLimit, maxAgentSessions)\n\t}\n\tr.mu.Unlock()\n\n\tserver := newServer()\n\t// APP_NAME is useful for identifying a business session from SYS_SESSIONS,\n\t// but some Xugu server/driver combinations close the socket when an ordinary\n\t// user sends this optional login attribute. Keep the original parameters for\n\t// the permission-degraded path and add APP_NAME only when SYSTEM control is\n\t// actually available.\n\tbusinessParams := params\n\tif !xuguControlSessionEligible(params) {\n\t\tr.connectMu.Lock()\n\t\t_, err := server.connectWithControl(businessParams, nil, false)\n\t\tr.connectMu.Unlock()","sourceCodeStart":833,"sourceCodeEnd":869,"githubUrl":"https://github.com/t8y2/dbx/blob/c0390bff16418b651f4728520d99adf8ce48829a/agents/drivers/xugu/main.go#L833-L869","documentation":"The Xugu driver's runtime server tracks open agent sessions by agentSessionID; openSession rejects a new open request when an entry with the same ID already exists in r.sessions. This guards against duplicate session registration and keeps the session map consistent.","triggerScenarios":"Calling the open-session API (OpenSession) with an agentSessionID that was already opened and not yet closed — e.g. retrying an open after a timeout without closing the first, or two components reusing the same ID concurrently.","commonSituations":"Client retry logic re-sending an open request whose original response was lost; connection pool reusing a stale session ID; misconfigured clients generating constant session IDs instead of unique ones.","solutions":["Close the existing session (CloseSession) before opening a new one with the same ID.","Generate a fresh unique agentSessionID (UUID) for each new session.","Fix retry logic so an open request isn't blindly repeated after an unknown outcome — check session state first.","If the old session is stale, use the driver's session cleanup/close to free the ID."],"exampleFix":"// before\n// retrying open with the same ID after a lost response\nid := \"agent-1\"\nsrv.OpenSession(id, params) // succeeded, response lost\nsrv.OpenSession(id, params) // agent session already exists: agent-1\n\n// after\nid := uuid.NewString()\nif err := srv.OpenSession(id, params); err != nil {\n    srv.CloseSession(id) // ensure cleanup on ambiguous failure\n}","handlingStrategy":"try-catch","validationCode":"// track opened IDs client-side to avoid duplicate opens\nopened := map[string]bool{}\nfunc ensureNotOpen(id string) error {\n    if opened[id] { return fmt.Errorf(\"session %s already opened locally\", id) }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"err := srv.OpenSession(id, params)\nif err != nil && strings.Contains(err.Error(), \"agent session already exists\") {\n    _ = srv.CloseSession(id) // release then reopen\n    err = srv.OpenSession(id, params)\n}","preventionTips":["Generate a unique session ID (UUID) per open call.","Make retries idempotent: close-then-reopen or check state before retrying.","Always close sessions on failure paths to free IDs.","Never hardcode constant session IDs across clients."],"tags":["xugu","sessions","duplicate-resource"],"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"}