{"record":{"id":"e00c80e94c0a41f3","repo":"binarywang/WxJava","slug":"createsession-too-many-active-sessions","errorCode":null,"errorMessage":"createSession: Too many active sessions","messagePattern":"createSession: Too many active sessions","errorType":"exception","errorClass":"TooManyActiveSessionsException","httpStatus":null,"severity":"warning","filePath":"weixin-java-common/src/main/java/me/chanjar/weixin/common/session/StandardSessionManager.java","lineNumber":145,"sourceCode":"  @Override\n  public InternalSession findSession(String id) {\n    if (id == null) {\n      return null;\n    }\n    return this.sessions.get(id);\n  }\n\n  @Override\n  public InternalSession createSession(String sessionId) {\n    if (sessionId == null) {\n      throw new IllegalStateException\n        (SM.getString(\"sessionManagerImpl.createSession.ise\"));\n    }\n\n    if ((this.maxActiveSessions >= 0) &&\n      (getActiveSessions() >= this.maxActiveSessions)) {\n      this.rejectedSessions++;\n      throw new TooManyActiveSessionsException(\n        SM.getString(\"sessionManagerImpl.createSession.tmase\"),\n        this.maxActiveSessions);\n    }\n\n    // Recycle or create a Session instance\n    InternalSession session = createEmptySession();\n\n    // Initialize the properties of the new session and return it\n    session.setValid(true);\n    session.setCreationTime(System.currentTimeMillis());\n    session.setMaxInactiveInterval(this.maxInactiveInterval);\n    session.setId(sessionId);\n    this.sessionCounter++;\n\n    return session;\n  }\n\n","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/binarywang/WxJava/blob/1c43293a3c2c9d7e91304b6d037fb017f680d0c6/weixin-java-common/src/main/java/me/chanjar/weixin/common/session/StandardSessionManager.java#L127-L163","documentation":"StandardSessionManager.createSession throws TooManyActiveSessionsException when the number of live sessions is at or above maxActiveSessions (rejectedSessions is incremented first). This caps memory use in high-concurrency messaging scenarios. A null sessionId is also rejected earlier in the same method with a different IllegalStateException.","triggerScenarios":"More concurrent unique users (each needing a session) than maxActiveSessions are active simultaneously; sessions are not expiring/being evicted fast enough to make room.","commonSituations":"A traffic spike on a public WeChat account/miniapp creating many simultaneous user sessions; maxInactiveInterval too long so sessions accumulate; a leak where sessions are created but never invalidated.","solutions":["Raise maxActiveSessions on the StandardSessionManager to match expected peak concurrency.","Lower maxInactiveInterval so idle sessions expire and free capacity sooner.","Ensure sessions are invalidated when flows complete rather than relying solely on timeout.","Monitor rejectedSessions and active session count to size the cap correctly."],"exampleFix":"// before\nStandardSessionManager mgr = new StandardSessionManager();\n// default limits, rejects under load\n\n// after\nmgr.setMaxActiveSessions(10000);\nmgr.setMaxInactiveInterval(600); // 10 min idle expiry","handlingStrategy":"try-catch","validationCode":"int active = sessionManager.getActiveSessions();\nif (active >= sessionManager.getMaxActiveSessions()) {\n  // evict idle sessions or shed load before creating\n  log.warn(\"Session cap reached: {} / {}\", active, sessionManager.getMaxActiveSessions());\n}","typeGuard":null,"tryCatchPattern":"try {\n  return sessionManager.createSession(sessionId);\n} catch (TooManyActiveSessionsException e) {\n  // shed load, retry after backoff, or increase cap\n  log.warn(\"Too many sessions, shedding\", e);\n  throw e;\n}","preventionTips":["Size maxActiveSessions to peak expected unique concurrent users.","Lower maxInactiveInterval so idle sessions free capacity sooner.","Invalidate sessions when flows complete instead of waiting for timeout.","Monitor rejectedSessions to detect sustained cap pressure."],"tags":["session","capacity","message-router","configuration"],"backgroundTag":null,"analyzedSha":"1c43293a3c2c9d7e91304b6d037fb017f680d0c6","analyzedAt":"2026-08-14T02:29:11.060Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}