{"record":{"id":"c0cc7b692d43bd07","repo":"t8y2/dbx","slug":"agent-session-already-exists-sessionid-c0cc7b","errorCode":null,"errorMessage":"Agent session already exists: <sessionId>","messagePattern":"Agent session already exists: <sessionId>","errorType":"exception","errorClass":"java.lang.IllegalStateException","httpStatus":null,"severity":"error","filePath":"agents/drivers/mongodb/src/main/java/com/dbx/agent/mongodb/MongoAgent.java","lineNumber":2020,"sourceCode":"                response.add(\"result\", GSON.toJsonTree(result));\n            } catch (Exception error) {\n                JsonObject rpcError = new JsonObject();\n                rpcError.addProperty(\"code\", -1);\n                rpcError.addProperty(\"message\", error.getMessage() == null ? \"Unknown error\" : error.getMessage());\n                response.add(\"error\", rpcError);\n            }\n            return GSON.toJson(response);\n        }\n\n        private Object openSession(String sessionId, JsonObject params) {\n            if (sessions.size() >= MAX_SESSIONS && !sessions.containsKey(sessionId)) {\n                throw new IllegalStateException(\"Agent session limit reached: \" + MAX_SESSIONS);\n            }\n            Session created = new Session(openClient(params));\n            Session existing = sessions.putIfAbsent(sessionId, created);\n            if (existing != null) {\n                created.close();\n                throw new IllegalStateException(\"Agent session already exists: \" + sessionId);\n            }\n            return Collections.singletonMap(\"ok\", true);\n        }\n\n        private Object closeSession(String sessionId) {\n            Session removed = sessions.remove(sessionId);\n            if (removed != null) {\n                removed.close();\n            }\n            return Collections.singletonMap(\"ok\", true);\n        }\n\n        private Session session(String sessionId) {\n            Session session = sessions.get(sessionId);\n            if (session == null) {\n                throw new IllegalStateException(\"Agent session not found: \" + sessionId);\n            }\n            return session;","sourceCodeStart":2002,"sourceCodeEnd":2038,"githubUrl":"https://github.com/t8y2/dbx/blob/c0390bff16418b651f4728520d99adf8ce48829a/agents/drivers/mongodb/src/main/java/com/dbx/agent/mongodb/MongoAgent.java#L2002-L2038","documentation":"openSession registers a new Session with putIfAbsent and, if the sessionId already exists, closes the freshly created client and throws IllegalStateException(\"Agent session already exists: <sessionId>\"). Session ids are unique keys, so duplicates are rejected to avoid clobbering a live connection.","triggerScenarios":"Calling openSession twice with the same sessionId without closing it first; retrying a request after a timeout when the first attempt actually succeeded; two workers racing to open the same named session.","commonSituations":"Idempotency mistakes in retry logic; config files assigning the same static session id to multiple workers; re-running an init script that opens sessions unconditionally.","solutions":["Check session existence (or attempt closeSession) before openSession with that id.","Generate unique session ids (UUID) per logical connection.","Treat this error as 'session already ready' and reuse the existing session instead of failing.","Make init scripts idempotent: open only if the session is not found."],"exampleFix":"// before\nopenSession(\"main\", params); // called on every startup\n// after\ntry { openSession(\"main\", params); }\ncatch (IllegalStateException e) { if (!e.getMessage().contains(\"already exists\")) throw e; }","handlingStrategy":"try-catch","validationCode":"// Java: make opening idempotent client-side\nif (!openedSessions.contains(sessionId)) {\n    agent.openSession(sessionId, params);\n    openedSessions.add(sessionId);\n}","typeGuard":null,"tryCatchPattern":"try { agent.openSession(sessionId, params); }\ncatch (IllegalStateException e) {\n  if (e.getMessage().startsWith(\"Agent session already exists\")) {\n    // session is already live; proceed using it\n  } else throw e;\n}","preventionTips":["Use unique (UUID) session ids per logical connection","Make init/retry logic idempotent: treat 'already exists' as success","Avoid static session ids shared across workers"],"tags":["mongodb","sessions","duplicate-key","lifecycle"],"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-14T00:17:10.932Z"}