Skip to content

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

Open
slayerjain wants to merge 1 commit into
mainfrom
fix/session-expiry-listener-latch
Open

slayerjain wants to merge 1 commit into
mainfrom
fix/session-expiry-listener-latch

Conversation

@slayerjain

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.sessionDestroyed, HttpSessionAttributeListener.attributeRemoved, HttpSessionBindingListener.valueUnbound, HttpSessionActivationListener, 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, org/apache/catalina/SessionListener).
      • Jetty 9.4 / 11 / 12.0 / 12.1: org/eclipse/jetty/server/session/Session, org/eclipse/jetty/ee8/nested/SessionHandler, org/eclipse/jetty/ee9/nested/SessionHandler, 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 retransformation.
  • RequestGate.java & JobGate.java:
    • Fast-paths as a no-op when invoked inside an active HTTP request or tracked job (so synchronous HttpSession.invalidate() stays completely clean and never false-latches).
    • Outside any active request/job, enters JobGate.enter(listener, "session-listener"): contaminates any overlapping test window with session-listener:<listenerClass>#<methodName> AND latches asyncExecutors=false.
    • Skips only exact container-native internal bridge listeners (StandardSession, SessionHandler, SingleSignOnListener, SessionListenerBridge) when invoked via containerNative=true, so listener-free session expiry never false-latches.
  • Tests (SessionListenerHookTest.java, SessionExpiryApp.java, SessionExpiryTail.java):
    • Unit tests covering armed/unarmed, in-request fast path, container-native bridge skip, ASM bytecode rewrite across every container dispatch class (including multi-arg onSessionAttributeUpdate and exception cleanup), and forked-JVM end-to-end tests on Tomcat 9, Jetty 9.4, and Undertow 2.2.

Verification

  • Full Maven test suite + release-profile Javadoc + scripts/smoke-javaagent.sh on JDK 21: 449/449 PASS (0 failures, 0 errors, 0 skipped).
  • Compile-checked mutation suite (mvn test-compile before test run): 9/9 KILLED (M1–M9, 0 survived, 0 compile errors).
  • Live 5-server Jakarta verification (Tomcat 10.1.34, Jetty 11.0.24, Jetty 12.0.16, Jetty 12.1.15, Undertow 2.3.18): 20/20 rows PASS (HttpSessionListener bg, HttpSessionBindingListener bg, sync invalidate, no-listener bg).

…ession-expiry listeners

Background HTTP session-expiry timers (Tomcat StandardManager/ManagerBase, Jetty HouseKeeper/SessionInactivityTimer, Undertow InMemorySessionManager) can invoke application HttpSessionListener, HttpSessionAttributeListener, HttpSessionBindingListener, HttpSessionActivationListener, and container-native session listeners outside any HTTP request, causing application cleanup code to land silently in a later test's coverage window.

- Hook container session-listener dispatch call sites in ServerEntries across Tomcat 8.5/9/10.1/11 (StandardSession), Jetty 9.4/11/12.0/12.1 (Session, ee8/ee9/nested/SessionHandler, ee10/ee11/servlet/SessionHandler, AbstractSessionManager, ManagedSession), and Undertow 2.2/2.3 (ApplicationListeners, SessionListenerBridge, SessionRestoringHandler, SessionListeners) using ASM try/finally bridges with exact fail-closed loader/callsite checks and retransform idempotency.
- Route through RequestGate.enterSessionListener / exitSessionListener -> JobGate.enter(listener, "session-listener"): no-op inside an active HTTP request or tracked job (so synchronous HttpSession.invalidate() stays clean), and outside any request/job both contaminates overlapping windows (session-listener:<class>#<method>) and latches asyncExecutors=false.
- Skip only exact container-native internal bridge listeners (StandardSession, SessionHandler, SingleSignOnListener, SessionListenerBridge) when invoked via container-native hooks so listener-free session expiry never false-latches.
- Add SessionListenerHookTest (unit, ASM bytecode rewrite across all container dispatch classes, and forked-JVM Tomcat 9 / Jetty 9.4 / Undertow 2.2 rows).

Signed-off-by: slayerjain <shubhamkjain@outlook.com>
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