{"record":{"id":"695587169431ffa8","repo":"t8y2/dbx","slug":"agent-session-limit-reached-max-sessions","errorCode":null,"errorMessage":"Agent session limit reached: <MAX_SESSIONS>","messagePattern":"Agent session limit reached: <MAX_SESSIONS>","errorType":"exception","errorClass":"java.lang.IllegalStateException","httpStatus":null,"severity":"error","filePath":"agents/drivers/mongodb/src/main/java/com/dbx/agent/mongodb/MongoAgent.java","lineNumber":2014,"sourceCode":"                } else {\n                    String sessionId = params.has(\"agentSessionId\")\n                        ? params.get(\"agentSessionId\").getAsString()\n                        : LEGACY_SESSION_ID;\n                    result = session(sessionId).handle(method, params);\n                }\n                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","sourceCodeStart":1996,"sourceCodeEnd":2032,"githubUrl":"https://github.com/t8y2/dbx/blob/c0390bff16418b651f4728520d99adf8ce48829a/agents/drivers/mongodb/src/main/java/com/dbx/agent/mongodb/MongoAgent.java#L1996-L2032","documentation":"The multi-session agent caps concurrent sessions at MAX_SESSIONS. openSession throws IllegalStateException(\"Agent session limit reached: N\") when opening a new sessionId while `sessions.size() >= MAX_SESSIONS` and the requested id is not already present, to bound resource usage (each session holds a MongoClient).","triggerScenarios":"Calling openSession with a brand-new sessionId when MAX_SESSIONS sessions are already registered; leaking sessions by never calling closeSession; repeated reconnects that open a new id each time under connection churn.","commonSituations":"Per-request session creation in high-traffic services; forgetting closeSession on error paths so sessions accumulate; test suites opening a session per test; scaling worker pools beyond the configured cap.","solutions":["Close idle sessions with closeSession before opening new ones.","Reuse an existing sessionId instead of minting a new one per request.","Raise MAX_SESSIONS if the workload legitimately needs more concurrent sessions (rebuild/reconfigure the agent).","Add session lifecycle management (close on failure/timeout) in client code to stop leaks."],"exampleFix":"// before (leak: never closed)\nopenSession(\"s-\" + UUID.randomUUID(), params); // ... no closeSession\n// after\nString id = \"s-\" + UUID.randomUUID();\ntry { openSession(id, params); work(id); } finally { closeSession(id); }","handlingStrategy":"try-catch","validationCode":"// Java: bound active sessions client-side before opening\nif (activeSessionCount >= MAX_SESSIONS) {\n    closeOldestIdleSession(); // free a slot first\n}","typeGuard":null,"tryCatchPattern":"try { agent.openSession(sessionId, params); }\ncatch (IllegalStateException e) {\n  if (e.getMessage().startsWith(\"Agent session limit reached\")) {\n    closeIdleSessions();\n    agent.openSession(sessionId, params); // retry after freeing slots\n  } else throw e;\n}","preventionTips":["Pool and reuse a bounded set of session ids instead of one per request","Always close sessions in finally blocks to prevent leaks","Monitor session counts and alert before hitting MAX_SESSIONS"],"tags":["mongodb","sessions","resource-limit","lifecycle"],"backgroundTag":"session-limit-reached","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"}