{"record":{"id":"1d88f5fd4c616e08","repo":"t8y2/dbx","slug":"agent-session-not-found-sessionid-1d88f5","errorCode":null,"errorMessage":"Agent session not found: <sessionId>","messagePattern":"Agent session not found: <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":2036,"sourceCode":"            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;\n        }\n\n        private void closeAllSessions() {\n            for (String sessionId : sessions.keySet()) {\n                closeSession(sessionId);\n            }\n        }\n\n        private static String requiredSessionId(JsonObject params) {\n            if (!params.has(\"agentSessionId\") || params.get(\"agentSessionId\").getAsString().trim().isEmpty()) {\n                throw new IllegalArgumentException(\"agentSessionId is required\");\n            }\n            return params.get(\"agentSessionId\").getAsString();\n        }\n\n        private void writeResponse(String response) {","sourceCodeStart":2018,"sourceCodeEnd":2054,"githubUrl":"https://github.com/t8y2/dbx/blob/c0390bff16418b651f4728520d99adf8ce48829a/agents/drivers/mongodb/src/main/java/com/dbx/agent/mongodb/MongoAgent.java#L2018-L2054","documentation":"The Session helper's session(sessionId) lookup returns the registered Session or throws IllegalStateException(\"Agent session not found: <sessionId>\") when the id is absent from the sessions map. Any per-session operation routed under an unknown/closed id fails fast rather than with a NullPointerException.","triggerScenarios":"Calling a session-scoped method with a sessionId that was never opened, or one already closed via closeSession/closeAllSessions; agent restart clearing the map while the client still references old ids; typo'd or case-mismatched session id.","commonSituations":"Stale session handles after agent process restart; closeSession racing subsequent requests; workers configured with different session ids than the opener; connection drops triggering closeAllSessions.","solutions":["Re-open the session with openSession before retrying the operation.","Verify the sessionId string matches the one used at openSession exactly.","Handle close events: invalidate cached session ids after closeSession/shutdown.","Add a health/get-style check to confirm a session exists before issuing data operations."],"exampleFix":"// before\nfind(\"worker-1\", params); // session closed earlier\n// after\ntry { find(\"worker-1\", params); }\ncatch (IllegalStateException e) {\n  if (e.getMessage().contains(\"session not found\")) { openSession(\"worker-1\", openParams); find(\"worker-1\", params); }\n  else throw e;\n}","handlingStrategy":"try-catch","validationCode":"// Java: verify the session exists before use\nif (!knownOpenSessions.contains(sessionId)) {\n    agent.openSession(sessionId, openParams);\n}","typeGuard":null,"tryCatchPattern":"try { result = agent.dispatch(sessionOp); }\ncatch (IllegalStateException e) {\n  if (e.getMessage().startsWith(\"Agent session not found\")) {\n    agent.openSession(sessionId, openParams); // reopen and retry\n    result = agent.dispatch(sessionOp);\n  } else throw e;\n}","preventionTips":["Invalidate cached session ids after closeSession, shutdown, or agent restart","Reopen sessions lazily on 'session not found' with the original open params retained","Use exact, stable session id strings and validate them before dispatch"],"tags":["mongodb","sessions","state","lifecycle"],"backgroundTag":"session-not-found","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"}