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 expirationView on GitHub (pinned to 1c43293a3c)
Solutions
- Guard invalidate() with isValid(): only invalidate live sessions.
- Make invalidation idempotent at the call site (wrap in try/catch IllegalStateException).
- 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
- Guard invalidate() with isValid() so it is called at most once.
- Centralise session invalidation in one owner to avoid races.
- Make cleanup paths idempotent by swallowing the already-invalid case.
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
- getAttributeNames: Session already invalidated
- setAttribute: Session [{0}] has already been invalidated
- setAttribute: name parameter cannot be null
- createSession: Too many active sessions
- 线程 [{}] 获取会话存档SDK失败,请检查是否已调用 closeAllSdks()
AI-assisted analysis of binarywang/WxJava@1c43293a3c (2026-08-14).
Data as JSON: /api/errors/03ab36fd39c960c7.
Report an issue: GitHub.