binarywang/WxJava · warning · TooManyActiveSessionsException

createSession: Too many active sessions

Error message

createSession: Too many active sessions

What it means

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.

Source

Thrown at weixin-java-common/src/main/java/me/chanjar/weixin/common/session/StandardSessionManager.java:145

  @Override
  public InternalSession findSession(String id) {
    if (id == null) {
      return null;
    }
    return this.sessions.get(id);
  }

  @Override
  public InternalSession createSession(String sessionId) {
    if (sessionId == null) {
      throw new IllegalStateException
        (SM.getString("sessionManagerImpl.createSession.ise"));
    }

    if ((this.maxActiveSessions >= 0) &&
      (getActiveSessions() >= this.maxActiveSessions)) {
      this.rejectedSessions++;
      throw new TooManyActiveSessionsException(
        SM.getString("sessionManagerImpl.createSession.tmase"),
        this.maxActiveSessions);
    }

    // Recycle or create a Session instance
    InternalSession session = createEmptySession();

    // Initialize the properties of the new session and return it
    session.setValid(true);
    session.setCreationTime(System.currentTimeMillis());
    session.setMaxInactiveInterval(this.maxInactiveInterval);
    session.setId(sessionId);
    this.sessionCounter++;

    return session;
  }

View on GitHub (pinned to 1c43293a3c)

Solutions

  1. Raise maxActiveSessions on the StandardSessionManager to match expected peak concurrency.
  2. Lower maxInactiveInterval so idle sessions expire and free capacity sooner.
  3. Ensure sessions are invalidated when flows complete rather than relying solely on timeout.
  4. Monitor rejectedSessions and active session count to size the cap correctly.

Example fix

// before
StandardSessionManager mgr = new StandardSessionManager();
// default limits, rejects under load

// after
mgr.setMaxActiveSessions(10000);
mgr.setMaxInactiveInterval(600); // 10 min idle expiry
Defensive patterns

Strategy: try-catch

Validate before calling

int active = sessionManager.getActiveSessions();
if (active >= sessionManager.getMaxActiveSessions()) {
  // evict idle sessions or shed load before creating
  log.warn("Session cap reached: {} / {}", active, sessionManager.getMaxActiveSessions());
}

Try / catch

try {
  return sessionManager.createSession(sessionId);
} catch (TooManyActiveSessionsException e) {
  // shed load, retry after backoff, or increase cap
  log.warn("Too many sessions, shedding", e);
  throw e;
}

Prevention

When it happens

Trigger: More concurrent unique users (each needing a session) than maxActiveSessions are active simultaneously; sessions are not expiring/being evicted fast enough to make room.

Common situations: 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.

Related errors


AI-assisted analysis of binarywang/WxJava@1c43293a3c (2026-08-14). Data as JSON: /api/errors/e00c80e94c0a41f3. Report an issue: GitHub.