From e048e52ce060316e3939c787aea97e8ff2316756 Mon Sep 17 00:00:00 2001 From: Jay <1204942+rogueserenity@users.noreply.github.com> Date: Wed, 5 Aug 2026 16:12:55 -0600 Subject: [PATCH] test(lambda): wait for the parked poll instead of sleeping in extension stop race (#2131) extensionRegister_racedAgainstStop_eventuallyDeliversShutdown slept 5ms to let the extension's /event/next poll connect and park before racing stop() against it, then asserted SHUTDOWN came back on that request. The sleep asserts the poll has parked without checking. On a loaded machine it expires first, so stop() closes the listening socket while the request is still in flight: the poll never reaches the handler, stop()'s SHUTDOWN fan-out finds no waiting context to hand the event to, and the client sees the connection die instead of the SHUTDOWN the test is about. That surfaces as an EOFException (connection accepted, then torn down) or a ConnectException (lost the race before the connect even landed) out of asyncNext.get(), and it failed CI this way once in 8325 tests. Replace the sleep with awaitExtensionParked(), polling a new package-private RuntimeApiServer.isExtensionParked() until the poll has provably parked, so the race the test sets up is the one it means to test. isExtensionParked reads under the same lock as the rest of the extension state, per the class's documented locking discipline; RegisteredExtension.hasWaitingContext() peeks without consuming, unlike takeWaitingContext(). The test still fails if the "poll pendingEvents before checking stopped" fix in the /event/next handler is reverted, so this removes the flake without weakening the regression it covers. Verified by breaking that fix and watching this test go red, and by reproducing the original failure under CPU saturation before and after the change. --- .../lambda/model/RegisteredExtension.java | 9 ++++++ .../lambda/runtime/RuntimeApiServer.java | 19 ++++++++++++ .../lambda/runtime/RuntimeApiServerTest.java | 31 +++++++++++++++++-- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/model/RegisteredExtension.java b/src/main/java/io/github/hectorvent/floci/services/lambda/model/RegisteredExtension.java index 6eb5feda09..526f02f997 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/model/RegisteredExtension.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/model/RegisteredExtension.java @@ -49,6 +49,15 @@ public class RegisteredExtension { waitingContext = ctx; } + /** + * Whether a poll is currently parked, without consuming it — unlike + * {@link #takeWaitingContext()}, which clears the context as it hands it over. Called under the + * owning server's lock, like the rest of this class. + */ + public boolean hasWaitingContext() { + return waitingContext != null; + } + /** * Records that this extension has issued its first {@code /extension/event/next}, which is * what AWS treats as the extension being init-ready (registering alone only obtains an diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServer.java b/src/main/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServer.java index 8dd861a27d..73156e8511 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServer.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServer.java @@ -218,6 +218,25 @@ public class RuntimeApiServer { return latch.await(timeoutMs, java.util.concurrent.TimeUnit.MILLISECONDS); } + /** + * True when this extension is currently parked on {@code /extension/event/next} — its request + * reached the handler and was stored as the waiting context rather than answered immediately. + * + *

Package-private and used only by tests that race {@link #stop()} against a poll already in + * flight. Such a test cannot use a sleep to decide the poll has landed: the request parking is + * what makes {@code stop()}'s SHUTDOWN fan-out see a waiting context instead of queueing the + * event for a connection that is about to be torn down, and on a loaded machine an arbitrary + * sleep expires before that happens. Polling this instead makes the wait condition-based. + * + *

Read under {@code lock}, per the locking discipline in the class doc. + */ + boolean isExtensionParked(String identifier) { + synchronized (lock) { + RegisteredExtension extension = extensions.get(identifier); + return extension != null && extension.hasWaitingContext(); + } + } + public CompletableFuture start() { CompletableFuture started = new CompletableFuture<>(); diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServerTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServerTest.java index eaaf27d637..14144ae6ab 100644 --- a/src/test/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServerTest.java @@ -1152,9 +1152,15 @@ class RuntimeApiServerTest { .header("Lambda-Extension-Identifier", extensionId) .GET().build(), HttpResponse.BodyHandlers.ofString()); - // Give the request time to actually connect and park before stop() closes - // the listening socket — see extensionShutdown_racedAgainstStop_isNeverOrphaned. - Thread.sleep(5); + // Wait for the poll to actually park before racing stop() against it. A sleep here + // is not enough: it asserts the request has parked without checking, and on a + // loaded machine it expires first, so stop() closes the socket while the request + // is still in flight. The poll then never reaches the handler, stop()'s fan-out + // finds no waiting context to hand SHUTDOWN to, and the client sees the connection + // die (EOFException, or ConnectException if it lost the race even earlier) rather + // than the SHUTDOWN this test is about. + assertTrue(awaitExtensionParked(freshServer, extensionId, 5000), + "extension never parked on /event/next; the stop() race was never set up"); freshServer.stop(); HttpResponse response = asyncNext.get(5, TimeUnit.SECONDS); @@ -1372,4 +1378,23 @@ class RuntimeApiServerTest { } throw new IOException("no free port in " + TEST_PORT_BASE + "-" + (TEST_PORT_BASE + TEST_PORT_RANGE)); } + + /** + * Polls until the extension is parked on {@code /extension/event/next}, so a test can race + * {@code stop()} against a poll that has provably landed rather than one assumed to have + * landed after a fixed sleep. + * + * @return true if it parked within the timeout; false if it never did. + */ + private static boolean awaitExtensionParked(RuntimeApiServer server, String extensionId, long timeoutMs) + throws InterruptedException { + long deadline = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(timeoutMs); + while (System.nanoTime() < deadline) { + if (server.isExtensionParked(extensionId)) { + return true; + } + Thread.sleep(1); + } + return server.isExtensionParked(extensionId); + } }