binarywang/WxJava · warning · IllegalStateException
setAttribute: Session [{0}] has already been invalidated
Error message
setAttribute: Session [{0}] has already been invalidated What it means
StandardSession.setAttribute throws IllegalStateException when the session is invalid (expired/invalidated) at the time of the setAttribute call. Unlike the null-name check (error 34), this validity check runs after the null-value→removeAttribute shortcut but before storing. Writing to an expired session is forbidden to prevent silent data loss.
Source
Thrown at weixin-java-common/src/main/java/me/chanjar/weixin/common/session/StandardSession.java:118
return Collections.enumeration(names);
}
@Override
public void setAttribute(String name, Object value) {
// Name cannot be null
if (name == null) {
throw new IllegalArgumentException(SM.getString("sessionImpl.setAttribute.namenull"));
}
// Null value is the same as removeAttribute()
if (value == null) {
removeAttribute(name);
return;
}
// 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 expireView on GitHub (pinned to 1c43293a3c)
Solutions
- Check session.isValid() before setAttribute.
- Re-acquire a fresh session from the manager/router when the old one is invalid.
- Increase maxInactiveInterval if legitimate flows exceed it.
- Catch IllegalStateException and re-establish the session for the user.
Example fix
// before
session.setAttribute("state", ctx); // throws if session expired
// after
if (session.isValid()) {
session.setAttribute("state", ctx);
} else {
session = sessionManager.createSession(sessionId);
session.setAttribute("state", ctx);
} Defensive patterns
Strategy: validation
Validate before calling
if (session.isValid()) {
session.setAttribute(name, value);
} else {
session = sessionManager.createSession(sessionId);
session.setAttribute(name, value);
} Type guard
private static boolean sessionAccessible(WxSession s) {
return s != null && s.isValid();
} Try / catch
try {
session.setAttribute(name, value);
} catch (IllegalStateException e) {
// session expired mid-flow — re-establish and retry
session = sessionManager.createSession(sessionId);
session.setAttribute(name, value);
} Prevention
- Check session.isValid() before writing state on sessions held across message gaps.
- Re-acquire a fresh session when the prior one expired.
- Set maxInactiveInterval above the longest expected inter-message gap.
When it happens
Trigger: Calling session.setAttribute(name, value) after session.invalidate() or after the session exceeded maxInactiveInterval.
Common situations: A handler stores per-user state on a session that already timed out between two messages; async work writing to a stale session reference; long gaps between user messages exceeding the inactive interval.
Related errors
- getAttributeNames: Session already invalidated
- invalidate: Session already invalidated
- createSession: Too many active sessions
- setAttribute: name parameter cannot be null
AI-assisted analysis of binarywang/WxJava@1c43293a3c (2026-08-14).
Data as JSON: /api/errors/06b0662dc27822bc.
Report an issue: GitHub.