{"record":{"id":"06687256ddb3df63","repo":"alibaba/spring-ai-alibaba","slug":"error-066872","errorCode":null,"errorMessage":"会话不存在: ","messagePattern":"会话不存在: ","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"spring-ai-alibaba-admin/spring-ai-alibaba-admin-server-start/src/main/java/com/alibaba/cloud/ai/studio/admin/service/impl/ChatSessionServiceImpl.java","lineNumber":140,"sourceCode":"        });\n        \n        if (cleanedCount[0] > 0) {\n            log.info(\"清理了 {} 个过期会话\", cleanedCount[0]);\n        }\n    }\n    \n    @Override\n    public ChatClient getSessionChatClient(String sessionId) {\n        return sessionClients.get(sessionId);\n    }\n    \n    \n    @Override\n    public ChatClient getOrCreateSessionChatClient(String sessionId, Map<String, String> observationMetadata) {\n        return sessionClients.computeIfAbsent(sessionId, key -> {\n            ChatSession session = getSession(sessionId);\n            if (session == null) {\n                throw new RuntimeException(\"会话不存在: \" + sessionId);\n            }\n            return chatClientFactoryDelegate.createChatClient(session.getModelConfig().getModelId(),\n                    session.getModelConfig().getParameters(), observationMetadata);\n        });\n    }\n    \n    /**\n     * 检查会话是否过期\n     */\n    private boolean isSessionExpired(ChatSession session) {\n        long currentTime = System.currentTimeMillis();\n        return (currentTime - session.getLastUpdateTime()) > SESSION_EXPIRE_TIME;\n    }\n    \n    /**\n     * 获取当前会话总数（用于监控）\n     */\n    public int getSessionCount() {","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/alibaba/spring-ai-alibaba/blob/f82da0b50f35744c13968191be2b1cd2452ef550/spring-ai-alibaba-admin/spring-ai-alibaba-admin-server-start/src/main/java/com/alibaba/cloud/ai/studio/admin/service/impl/ChatSessionServiceImpl.java#L122-L158","documentation":"ChatSessionServiceImpl.getOrCreateSessionChatClient looks up the ChatSession for the given sessionId inside computeIfAbsent and throws this RuntimeException when getSession returns null. Without a session there is no bound model config, so no ChatClient can be created.","triggerScenarios":"Calling getOrCreateSessionChatClient(sessionId, ...) with a sessionId that was never created, was deleted, or whose storage lookup fails — including inside a computeIfAbsent where the throw propagates as a CompletionException-like wrap to the caller.","commonSituations":"Client sends a stale sessionId after server restart (in-memory session store lost); sessions expired/cleaned up while the front end still holds the id; typo or truncation in sessionId passed from the request.","solutions":["Verify the sessionId was created first (call the create-session endpoint) and that the exact value is passed","Recreate the session if the server restarted and the in-memory sessionClients/session store was cleared","Check the session persistence store (repository/DB) to confirm the session record still exists","In client code, handle this error by creating a new session and retrying the request"],"exampleFix":"// before\nChatClient c = service.getOrCreateSessionChatClient(oldSessionId, meta); // session lost after restart\n// after\nChatSession s = service.getSession(oldSessionId);\nif (s == null) {\n    String newId = service.createSession(request); // recreate, then get client\n}\nChatClient c = service.getOrCreateSessionChatClient(newId, meta);","handlingStrategy":"try-catch","validationCode":"// Java: check session existence before requesting a client\nChatSession s = chatSessionService.getSession(sessionId);\nif (s == null) {\n    sessionId = chatSessionService.createSession(newSessionRequest); // recreate first\n}","typeGuard":"boolean sessionExists(ChatSession s) { return s != null && s.getModelConfig() != null && s.getModelConfig().getModelId() != null; }","tryCatchPattern":"try {\n    client = service.getOrCreateSessionChatClient(sessionId, metadata);\n} catch (RuntimeException e) {\n    if (e.getMessage().startsWith(\"会话不存在\")) {\n        sessionId = createNewSession(); // recreate and retry once\n    }\n}","preventionTips":["Handle server restarts: treat any cached sessionId as invalid after a reconnect and recreate","Don't assume session longevity — verify with getSession before reuse","Include session-expiry handling in the client (recreate session on 404-style responses)"],"tags":["not-found","session","lookup"],"backgroundTag":"entity-not-found","analyzedSha":"f82da0b50f35744c13968191be2b1cd2452ef550","analyzedAt":"2026-09-09T15:32:42.421Z","contentChangedAt":"2026-09-09T15:32:42.421Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}