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.
This commit is contained in:
Jay authored and GitHub committed 2026-08-05 17:12:55 -05:00
1 parent 62a576c8d4
commit e048e52ce0
3 files changed
+56 -3

No files matched your search

@@ -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<String> 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);
}
}