Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion keploy-sdk/src/main/java/io/keploy/dedup/JobGate.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ static boolean hasExpiredRunning() {
* No-op when Keploy is unarmed.
*
* @param job the object whose {@code run()} runs the job
* @param kind "quartz", "scheduled", "kafka", or "listener"
* @param kind "quartz", "scheduled", "kafka", "listener", or "session-listener"
* @return the token to pass to {@link #exit(Object)} when it is done
*/
public static Object enter(Object job, String kind) {
Expand All @@ -125,6 +125,13 @@ public static Object enter(Object job, String kind) {
// whose window already closed; fail closed on asyncExecutors.
KeployDedupAgent.noteObservedUntrackedAsyncLibrary(
"scheduled job (" + run.name + ")", true);
} else if ("session-listener".equals(kind)) {
// A container session-expiry listener firing outside any tracked
// request or job may be executing application cleanup for a
// session created by an earlier HTTP test; fail closed on
// asyncExecutors in addition to contaminating overlapping windows.
KeployDedupAgent.noteObservedUntrackedAsyncLibrary(
"session listener outside request window (" + run.name + ")", true);
}
} catch (Throwable t) {
// The job runs on. A run counted as going on before this (an
Expand Down Expand Up @@ -391,6 +398,9 @@ static String name(Object job, String kind) {
if ("kafka".equals(kind) || "listener".equals(kind)) {
return cachedListenerName(job, kind);
}
if ("session-listener".equals(kind) && job instanceof String) {
return kind + ":" + job;
}
String name;
try {
if ("quartz".equals(kind)) {
Expand Down
90 changes: 90 additions & 0 deletions keploy-sdk/src/main/java/io/keploy/dedup/RequestGate.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,21 @@ public final class RequestGate {
// keeps marking every subsequent window "armed-mid-flight" until exit().
private static final Object UNARMED_TOKEN = new Object();
private static final LongAdder UNARMED_IN_FLIGHT = new LongAdder();
private static final ThreadLocal<Integer> UNARMED_THREAD_DEPTH = new ThreadLocal<>();

// Exact container-internal listener bridge classes skipped ONLY on
// container-native listener interfaces (never on Servlet HttpSession*Listener
// interfaces), because these bridges either delegate into hooked Servlet
// session listener dispatchers or perform container-only SSO bookkeeping.
private static final Set<String> INTERNAL_CONTAINER_SESSION_LISTENERS = Collections.unmodifiableSet(
new java.util.HashSet<>(java.util.Arrays.asList(
"io.undertow.servlet.core.SessionListenerBridge",
"org.eclipse.jetty.ee11.servlet.SessionHandler",
"org.eclipse.jetty.ee10.servlet.SessionHandler",
"org.eclipse.jetty.ee9.nested.SessionHandler",
"org.eclipse.jetty.ee8.nested.SessionHandler",
"org.apache.catalina.authenticator.SingleSignOnListener"
)));
// Timestamp of the last unarmed async hand-off (zero-allocation, zero
// wrapping) so windows opened shortly after arming still carry
// armed-mid-flight even if the initiating unarmed request already exited.
Expand Down Expand Up @@ -432,6 +447,8 @@ static void noteUnarmedActivity() {
static Object enterUnarmed() {
noteUnarmedActivity();
UNARMED_IN_FLIGHT.increment();
Integer depth = UNARMED_THREAD_DEPTH.get();
UNARMED_THREAD_DEPTH.set(depth == null ? Integer.valueOf(1) : Integer.valueOf(depth.intValue() + 1));
return UNARMED_TOKEN;
}

Expand All @@ -441,6 +458,17 @@ static boolean isUnarmedToken(Object token) {

static void exitUnarmed() {
UNARMED_IN_FLIGHT.decrement();
Integer depth = UNARMED_THREAD_DEPTH.get();
if (depth == null || depth.intValue() <= 1) {
UNARMED_THREAD_DEPTH.remove();
} else {
UNARMED_THREAD_DEPTH.set(Integer.valueOf(depth.intValue() - 1));
}
}

static boolean isInsideUnarmedThread() {
Integer depth = UNARMED_THREAD_DEPTH.get();
return depth != null && depth.intValue() > 0;
}

static long unarmedInFlight() {
Expand Down Expand Up @@ -480,6 +508,7 @@ static void disarm() {
unarmedEntered = false;
pendingArmedMidFlight = false;
lastUnarmedHandOffNanos = 0L;
UNARMED_THREAD_DEPTH.remove();
ARMED_MID_FLIGHT_CONSUMED.set(false);
externalUntrackedTailPending = false;
externalUntrackedTailExpiresAtNanos = 0L;
Expand Down Expand Up @@ -5513,6 +5542,67 @@ public String toString() {
}
}

/**
* Guards a container session-listener invocation (e.g.
* {@code HttpSessionListener.sessionDestroyed},
* {@code HttpSessionAttributeListener.attributeRemoved},
* {@code HttpSessionBindingListener.valueUnbound}). When invoked on a
* thread already inside a tracked request/dispatch or tracked job (such as
* synchronous {@code session.invalidate()} inside a handler), returns
* {@code null} with zero latch and zero contamination. When invoked on a
* background expiry thread outside any tracked request or job, enters
* {@link JobGate} so overlapping test windows are marked contaminated and
* latches {@code asyncExecutors=false}.
*
* @param listener the session listener instance being invoked
* @param eventName the listener method name (e.g. {@code "sessionDestroyed"})
* @param containerNative {@code true} for container-native listener
* interfaces where exact container-internal bridge listeners are
* skipped; {@code false} for standard Servlet session listener
* interfaces
* @return a token to pass to {@link #exitSessionListener(Object)}, or
* {@code null} when no background tracking was needed
*/
public static Object enterSessionListener(Object listener, String eventName, boolean containerNative) {
if (listener == null) {
return null;
}
String listenerClass = listener.getClass().getName();
if (containerNative && INTERNAL_CONTAINER_SESSION_LISTENERS.contains(listenerClass)) {
return null;
}
if (isInsideUnarmedThread()) {
return null;
}
String label = (eventName == null || eventName.isEmpty())
? listenerClass
: listenerClass + "#" + eventName;
if (!armed) {
KeployDedupAgent.noteObservedUntrackedAsyncLibrary(
"session listener outside request window (" + label + ")", false);
return enterUnarmed();
}
if (INSIDE.get() != null
|| JobGate.runningAttribution() != null
|| JobGate.expiredRunningName() != null) {
return null;
}
return JobGate.enter(label, "session-listener");
}

/**
* Completes a background session-listener run started by
* {@link #enterSessionListener(Object, String, boolean)}.
*
* @param token the token returned by {@code enterSessionListener}, or {@code null}
*/
public static void exitSessionListener(Object token) {
if (token == null) {
return;
}
JobGate.exit(token);
}

private static final class HandedSupplier<T> extends Hand implements Supplier<T> {

final Supplier<T> task;
Expand Down
Loading
Loading