Skip to content

fix(dedup): exempt verified Undertow session expiry from XNIO latch - #200

Merged
slayerjain merged 1 commit into
mainfrom
fix/undertow-expiry-xnio-latch
Nov 29, 2026
Merged

slayerjain merged 1 commit into
mainfrom
fix/undertow-expiry-xnio-latch

Conversation

@slayerjain

Copy link
Copy Markdown
Member

Root Cause

Undertow's InMemorySessionManager$SessionImpl schedules a timer cancelTask (SessionImpl$2#run) on the XNIO I/O thread outside any HTTP request window. When the timer fires, SessionImpl$2#run() dispatches new SessionImpl$2$1(this) onto SessionImpl.this.worker via org.xnio.XnioWorker.execute(Runnable).

Because RequestGate.handXnioWorker treated every outside-window XnioWorker.execute call as an untracked background worker, every Undertow session expiry permanently latched asyncExecutors=false (untracked-async:xnio-worker-outside-window), even on listener-free apps (both standalone Undertow 2.2/2.3 and Spring Boot 3.3 spring-boot-starter-undertow).

Fix (By Construction, No Blanket Allowlist)

  1. ServerEntries.wrapXnioWorkerExecute: passes both this (ALOAD 0) and task (ALOAD 1) to RequestGate.handXnioWorker(Object, Runnable). The 1-arg handXnioWorker(Runnable) overload is retained for binary/source compatibility and delegates with receiver = null (fail-closed).
  2. SinkAllowlist.isAllowedUndertowSessionExpiryXnioDispatch: verifies Undertow's internal expiry dispatch end-to-end before permitting it:
    • receiver != null and task != null;
    • fast-path exact runtime task class io.undertow.server.session.InMemorySessionManager$SessionImpl$2$1 (O(1) fail-closed for all other tasks without walking the stack);
    • exact immediate external caller frame (skipping only Keploy frames and org.xnio.XnioWorker, never reflection/MethodHandles or app frames) equals io.undertow.server.session.InMemorySessionManager$SessionImpl$2#run;
    • same-ClassLoader owner-identity chain: task.this$1 is exact InMemorySessionManager$SessionImpl$2, cancelTask.this$0 is exact InMemorySessionManager$SessionImpl, session.cancelTask == cancelTask, and session.worker == receiver.
  3. RequestGate: wraps verified expiry dispatches in a non-Hand XnioIdleBackgroundTask recognized by enterXnioWorkerDispatch so XnioWorker's internal task-pool/thread-start sinks stay clean during the dispatch while SessionImpl$2$1#run() executes on the worker thread with no window permit and no tracked-dispatch scope. Application session listeners (hooked by fix(dedup): contaminate and latch asyncExecutors on background HTTP session-expiry listeners #198) and application async work submitted to XnioWorker.execute outside a request window still fail closed and latch asyncExecutors=false.
  4. SessionListenerHookTest: re-enables all 4 Undertow assertions previously skipped because of this pre-existing XNIO latch (baseline warm, HttpSessionBindingListener.valueUnbound, synchronous in-request invalidate(), and listener-free background expiry).

Verification

  • Live Undertow 2.3.18 (ws=false, ws=true) + Spring Boot 3.3.5 starter-undertow (both KEPLOY_PROVEN unset and KEPLOY_PROVEN=1):
    • main (fc11dd7): reproduces failure across all 6 rows (asyncExecutors=false, untracked-async:xnio-worker-outside-window).
    • This branch: passes all 6 rows (base_async=true, post-expiry asyncExecutors=true, contaminated=[], zero latches).
  • Unit & forked-JVM suites:
    • JdkSinkBridgeTest#undertowSessionExpiryXnioDispatchAllowsOnlyExactOwnedCancelTaskAndFailsClosedOtherwise: covers real Undertow 2.2 SessionImpl$2#run armed/unarmed positive paths and adversarial negative matrix (direct/reflective SessionImpl$2$1, arbitrary app Runnable, null receiver, foreign XnioWorker, wrong caller, detached SessionImpl$2, forged same-ClassLoader Runnable with matching this$1).
    • ForkedAsyncAndHonestyTest: verifies Undertow 2.2.37 and Undertow 2.3.18 session expiry alongside outside-window app XnioWorker.execute escape detection in both prefix and index modes (KEPLOY_PROVEN unset and KEPLOY_PROVEN=1).
    • SessionListenerHookTest (with all 4 Undertow exclusions dropped): 6/6 pass in both KEPLOY_PROVEN unset and KEPLOY_PROVEN=1.
    • Full keploy-sdk test suite, ./scripts/smoke-javaagent.sh, and reproducible release build (mvn -B -P release -Dgpg.skip -DskipTests clean verify + no-clean verify + sha256sum -c) all pass.
    • 7 targeted mutations covering every guard (SinkAllowlist exemption, caller check, task-class check, worker identity, cancelTask identity, enterXnioWorkerDispatch, and ServerEntries receiver passing) all fail the unit suite cleanly (7/7 killed).

Undertow's InMemorySessionManager$SessionImpl schedules a timer
cancelTask ($2#run) that executes outside any HTTP request window and
dispatches a SessionImpl$2$1 task onto SessionImpl.this.worker via
XnioWorker.execute(Runnable). Because RequestGate.handXnioWorker had
no verification for this internal expiry dispatch, every Undertow
session expiry latched asyncExecutors=false
(untracked-async:xnio-worker-outside-window), even with no application
session listeners.

Verify Undertow's session-expiry dispatch by construction without any
blanket allowlist:
- ServerEntries.wrapXnioWorkerExecute passes both the receiver (this)
  and task to RequestGate.handXnioWorker(Object, Runnable), keeping
  the 1-arg overload for compatibility (receiver=null fails closed).
- SinkAllowlist.isAllowedUndertowSessionExpiryXnioDispatch checks:
  1. exact runtime task class InMemorySessionManager$SessionImpl$2$1;
  2. exact immediate external caller frame (skipping only Keploy and
     org.xnio.XnioWorker, never reflection or app frames)
     InMemorySessionManager$SessionImpl$2#run;
  3. same-ClassLoader owner-identity chain: task.this$1 is exact
     SessionImpl$2, cancelTask.this$0 is exact SessionImpl,
     session.cancelTask == cancelTask, and session.worker == receiver.
- RequestGate wraps verified expiry tasks in a non-Hand
  XnioIdleBackgroundTask recognized by enterXnioWorkerDispatch so
  XnioWorker's internal pool/thread sinks stay clean while the task
  runs on the worker thread with no window permit and no tracked
  dispatch scope (application listeners and user async work still fail
  closed).
- Drop the Undertow exclusions from SessionListenerHookTest now that
  listener-free Undertow expiry keeps asyncExecutors=true while
  application listeners and HttpSessionBindingListener still
  contaminate and latch.

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