binarywang/WxJava · warning · IllegalStateException

invalidate: Session already invalidated

Error message

invalidate: Session already invalidated

What it means

StandardSession.invalidate throws IllegalStateException if the session is already invalid. invalidate() is meant to be called exactly once; calling it again (or after expiry) is rejected. Internally it calls expire() to clear attributes and mark the session dead.

Source

Thrown at weixin-java-common/src/main/java/me/chanjar/weixin/common/session/StandardSession.java:133

    // Validate our current state
    if (!isValidInternal()) {
      throw new IllegalStateException(SM.getString("sessionImpl.setAttribute.ise", getIdInternal()));
    }

    this.attributes.put(name, value);

  }

  @Override
  public void removeAttribute(String name) {
    removeAttributeInternal(name);
  }

  @Override
  public void invalidate() {
    if (!isValidInternal()) {
      throw new IllegalStateException(SM.getString("sessionImpl.invalidate.ise"));
    }

    // Cause this session to expire
    expire();

  }

  @Override
  public WxSession getSession() {
    if (this.facade == null) {
      this.facade = new StandardSessionFacade(this);
    }

    return this.facade;
  }

  /**
   * Return the <code>isValid</code> flag for this session without any expiration

View on GitHub (pinned to 1c43293a3c)

Solutions

  1. Guard invalidate() with isValid(): only invalidate live sessions.
  2. Make invalidation idempotent at the call site (wrap in try/catch IllegalStateException).
  3. Ensure only one owner (router/manager) invalidates a session to avoid double-invalidation races.

Example fix

// before
session.invalidate(); // throws on second call

// after
if (session.isValid()) {
  session.invalidate();
}
Defensive patterns

Strategy: validation

Validate before calling

if (session.isValid()) {
  session.invalidate();
}

Type guard

private static boolean sessionAccessible(WxSession s) {
  return s != null && s.isValid();
}

Try / catch

try {
  session.invalidate();
} catch (IllegalStateException ignored) {
  // already invalidated — nothing to do
}

Prevention

When it happens

Trigger: Calling session.invalidate() a second time, or calling it after the session already expired via maxInactiveInterval.

Common situations: Cleanup code that invalidates defensively on every request without checking validity first; a timeout-driven expiry racing with an explicit invalidate(); error-handling paths that invalidate then re-attempt.

Related errors


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