Skip to content

fix(dedup): contaminate and latch asyncExecutors on background HTTP session-expiry listeners - #198

Merged
slayerjain merged 2 commits into
mainfrom
fix/session-expiry-listener-latch
Nov 29, 2026
Merged

slayerjain merged 2 commits into
mainfrom
fix/session-expiry-listener-latch

Conversation

@slayerjain

@slayerjain slayerjain commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Summary

Background HTTP session-expiry timers (Tomcat StandardManager/ManagerBase, Jetty HouseKeeper/SessionInactivityTimer, Undertow InMemorySessionManager) can invoke application session listeners (HttpSessionListener, HttpSessionAttributeListener, HttpSessionBindingListener, HttpSessionActivationListener, HttpSessionIdListener, and container-native session listeners) outside any HTTP request. Without a session-listener hook, application cleanup code executed on session expiry can land silently inside an unrelated later test's coverage window.

Changes

  • ServerEntries.java:
    • Hooks container session-listener dispatch call sites across:
      • Tomcat 8.5 / 9 / 10.1 / 11: org/apache/catalina/session/StandardSession (HttpSessionListener, HttpSessionAttributeListener, HttpSessionBindingListener, HttpSessionActivationListener, HttpSessionIdListener, org/apache/catalina/SessionListener).
      • Jetty 9.4 / 11 / 12.0 / 12.1: org/eclipse/jetty/server/session/SessionHandler, org/eclipse/jetty/server/session/Session, org/eclipse/jetty/ee8/nested/SessionHandler ($CoreSessionManager required; outer class optional), org/eclipse/jetty/ee9/nested/SessionHandler ($CoreSessionManager required; outer class optional), org/eclipse/jetty/ee10/servlet/SessionHandler, org/eclipse/jetty/ee11/servlet/SessionHandler, org/eclipse/jetty/session/AbstractSessionManager (Session$LifeCycleListener), and org/eclipse/jetty/session/ManagedSession (Session$ValueListener: onSessionAttributeUpdate, onSessionActivation, onSessionPassivation).
      • Undertow 2.2 / 2.3: io/undertow/servlet/core/ApplicationListeners, io/undertow/servlet/core/SessionListenerBridge, io/undertow/servlet/handlers/SessionRestoringHandler, and io/undertow/server/session/SessionListeners.
    • Wraps each dispatch call site in an ASM try/finally bridge calling RequestGate.enterSessionListener(listener, methodName, containerNative) and RequestGate.exitSessionListener(token) with fail-closed loader/callsite checks and idempotent load-time/retransform handling.
  • RequestGate.java & JobGate.java:
    • Fast-paths as a no-op when invoked inside an active HTTP request or tracked/expired job (so synchronous HttpSession.invalidate() and changeSessionId() stay clean and never false-latch).
    • Outside any active request/job, enters JobGate.enter(label, "session-listener"): contaminates any overlapping test window with session-listener:<listenerClass>#<methodName> AND latches asyncExecutors=false.
    • Skips exact container-internal bridge listeners (SessionListenerBridge, Jetty 12 ee8/ee9 nested & ee10/ee11 SessionHandler, Tomcat SingleSignOnListener) ONLY on container-native interfaces (containerNative=true), and skips exact org.apache.tomcat.websocket.server.WsSessionListener (auto-registered by Tomcat WsSci in Spring Boot and standalone Tomcat) ONLY on Servlet interfaces (containerNative=false, never subclasses), so listener-free session expiries stay clean.
  • README.md:
    • Documents background HTTP session-expiry listener contamination and asyncExecutors=false latching, in-request clean fast path, Tomcat default WsSessionListener exemption, and limits (external session-store adapters such as Spring Session's SessionEventHttpSessionListenerAdapter/HttpSessionAdapter, or containers loaded before the agent).
  • Tests (SessionListenerHookTest.java, SessionExpiryApp.java, SessionExpiryTail.java):
    • Unit tests covering armed/unarmed, in-request/in-job/expired-job fast paths, exact SessionListenerBridge & WsSessionListener skips vs subclass fail-closed, ASM bytecode rewrite across all container dispatch classes (including HttpSessionIdListener, multi-arg onSessionAttributeUpdate, exception cleanup, containerNative bridge propagation, load-time idempotency, and Jetty 12 ee8/ee9 outer vs $CoreSessionManager required semantics), and forked-JVM end-to-end tests on Tomcat 9, Jetty 9.4, and Undertow 2.2.

Verification

  • Full Maven CI suite (clean verify release profile + mvn -B -pl keploy-sdk -am test + ./scripts/smoke-javaagent.sh + no-clean verify with sha256sum -c): 451/451 PASS on JDK 21 (0 failures, 0 errors, 0 skipped).
  • Compile-checked, baseline-verified, assertion-checked mutation suite (single-method Surefire filters, Failures > 0 && Errors == 0): 17/17 KILLED (0 survived, 0 compile errors, 0 runner errors).
  • Real-jar load+link verification across Jetty 12.0.0, 12.0.16, 12.1.15 (ee8/ee9 nested $CoreSessionManager, bridges=10) + Tomcat 10.1.34 (StandardSession, bridges=11): verifyFail=0, 0 hook-failure warnings.
  • Live server verification across Tomcat 9/10.1 (including ws=True with WsSessionListener and HttpSessionIdListener), Spring Boot 3.3.5 default Tomcat, and Jetty 12 ee9/ee10/ee11 in both KEPLOY_PROVEN modes: 144/144 rows PASS (0 failures, 0 false marks/latches on ws=True & Spring Boot default Tomcat).

…ession-expiry listeners

Background HTTP session-expiry timers (Tomcat StandardManager/ManagerBase,
Jetty HouseKeeper/SessionInactivityTimer, Undertow InMemorySessionManager)
can invoke application session listeners outside any HTTP request. Without
a hook, cleanup code executed during background session expiry can land
silently inside an unrelated later test's coverage window.

Hook container session-listener dispatch invokeinterface call sites across
Tomcat 8.5/9/10.1/11, Jetty 9.4/11/12.0/12.1 (including ee8/ee9 nested
SessionHandler, ee10/ee11 SessionHandler, and Jetty 12
core AbstractSessionManager/ManagedSession), and Undertow 2.2/2.3 for
HttpSessionListener, HttpSessionAttributeListener, HttpSessionBindingListener,
HttpSessionActivationListener, HttpSessionIdListener, and container-native
session listener interfaces:
- Keep Jetty 12 ee8/ee9 outer nested SessionHandler optional while requiring
  its nested CoreSessionManager so Jetty 12.0.0/12.0.16/12.1.15 never log
  false hook-failure warnings or falsely latch asyncExecutors=false.
- Fast-path as a no-op inside an active HTTP request or tracked/expired job
  so synchronous HttpSession.invalidate() and changeSessionId() stay clean.
- Outside any tracked request or job, enter JobGate("session-listener") to
  contaminate overlapping windows (session-listener:<class>#<method>) and
  latch asyncExecutors=false.
- Skip exact container-internal bridges (SessionListenerBridge, Jetty 12
  SessionHandler, Tomcat SingleSignOnListener) only on container-native
  interfaces, and skip exact org.apache.tomcat.websocket.server.WsSessionListener
  (auto-registered by Tomcat WsSci in Spring Boot and standalone Tomcat) only
  on Servlet interfaces (never subclasses) so listener-free expiries stay clean.
- Document behavior and limits (e.g. Spring Session adapters) in README.md.

Signed-off-by: slayerjain <shubhamkjain@outlook.com>
Skip io.undertow.websockets.jsr.JsrWebSocketFilter$LogoutListener by exact class name on Servlet session-listener interfaces (matching Tomcat's WsSessionListener) so listener-free session expiries on Undertow deployments with WebSocket support (including Spring Boot's starter-undertow defaults) do not falsely contaminate overlapping windows or latch asyncExecutors=false. Subclasses remain tracked.

Signed-off-by: slayerjain <shubhamkjain@outlook.com>
@slayerjain
slayerjain force-pushed the fix/session-expiry-listener-latch branch from 113eb2c to f882094 Compare November 29, 2026 10:12
@slayerjain
slayerjain merged commit fc11dd7 into main Nov 29, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant