mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(lambda): commit enqueue dispatch/requeue decision atomically
Per @abanna's review of #2098. enqueue()'s deferred callback read waitingCtx.response().ended() twice — once inside `synchronized(lock)` deciding requeue, once outside deciding send. A client disconnect between the two reads left neither branch firing and the invocation stranded in inFlight until the function deadline. Read ended() exactly once inside the lock and commit dispatch/requeue before releasing. Adds a deterministic latch-gated race test (two new Test-only protected seams) that fails on the double-read shape and passes on the fix. 50/50 tests pass.
This commit is contained in:
1 parent
f07fc31d1a
commit
a1a659b406
2 files changed
+104
-16
No files matched your search
+42
-13
@@ -163,6 +163,21 @@ public class RuntimeApiServer {
|
||||
*/
|
||||
protected void afterQuiesceStoppedFlagSet() { }
|
||||
|
||||
/**
|
||||
* Test-only: called inside enqueue()'s deferred dispatch after lock release,
|
||||
* before the send/requeue decision is acted on. Test subclasses override to
|
||||
* mutate the parked ctx (e.g. end it) and prove the decision was committed
|
||||
* atomically inside the lock.
|
||||
*/
|
||||
protected void afterEnqueueDispatchLockReleased(RoutingContext waitingCtx) { }
|
||||
|
||||
/**
|
||||
* Test-only: called at the top of sendInvocation so tests can observe whether
|
||||
* a dispatch actually ran. Distinguishes "sent to a dead ctx" from "skipped,
|
||||
* stranded" — both leave the invocation in inFlight otherwise.
|
||||
*/
|
||||
protected void beforeSendInvocationWrite(String requestId) { }
|
||||
|
||||
// Set once an extension reports an init/exit error. Real AWS treats both as fatal to the
|
||||
// execution environment, so the container must neither accept new work nor be reused.
|
||||
//
|
||||
@@ -758,23 +773,36 @@ public class RuntimeApiServer {
|
||||
final RoutingContext waitingCtx = waitingCtxForInvocation;
|
||||
vertx.runOnContext(v -> {
|
||||
beforeEnqueueDeferredDispatch();
|
||||
// Re-check `stopped` under the lock: quiesce() may have run since the
|
||||
// enclosing enqueue() released it, in which case it already completed
|
||||
// the caller's future with ContainerStopped. Dispatching now would
|
||||
// silently discard the runtime's /response.
|
||||
boolean alreadyHandled;
|
||||
boolean shouldRequeue;
|
||||
// Two things could have changed between enqueue()'s lock release and
|
||||
// this deferred dispatch reaching the event loop:
|
||||
// 1. quiesce() may have run — atomic with clearing inFlight and
|
||||
// completing the caller's future with ContainerStopped. Skip.
|
||||
// 2. The parked client may have disconnected — the ctx is ended and
|
||||
// writing to it would fail. Requeue for the next /next poller
|
||||
// and drop this stale inFlight entry (the next poller will re-put
|
||||
// the invocation into inFlight under its own lock).
|
||||
//
|
||||
// Read waitingCtx.response().ended() ONCE, under the lock, so the
|
||||
// dispatch/requeue decision is committed atomically. Reading it a
|
||||
// second time outside the lock (as the code did previously) let a
|
||||
// disconnect between the two reads produce a stranded inFlight entry:
|
||||
// the inside-lock read saw !ended → no requeue; the outside-lock read
|
||||
// saw ended → no send; neither branch fired.
|
||||
boolean shouldDispatch;
|
||||
synchronized (lock) {
|
||||
alreadyHandled = stopped;
|
||||
shouldRequeue = !alreadyHandled && waitingCtx.response().ended();
|
||||
if (shouldRequeue) {
|
||||
if (stopped) {
|
||||
return;
|
||||
}
|
||||
if (waitingCtx.response().ended()) {
|
||||
pendingQueue.offer(invocation);
|
||||
inFlight.remove(invocation.getRequestId());
|
||||
shouldDispatch = false;
|
||||
} else {
|
||||
shouldDispatch = true;
|
||||
}
|
||||
}
|
||||
if (alreadyHandled) {
|
||||
return;
|
||||
}
|
||||
if (!waitingCtx.response().ended()) {
|
||||
afterEnqueueDispatchLockReleased(waitingCtx);
|
||||
if (shouldDispatch) {
|
||||
sendInvocation(waitingCtx, invocation);
|
||||
}
|
||||
});
|
||||
@@ -808,6 +836,7 @@ public class RuntimeApiServer {
|
||||
}
|
||||
|
||||
private void sendInvocation(RoutingContext ctx, PendingInvocation invocation) {
|
||||
beforeSendInvocationWrite(invocation.getRequestId());
|
||||
// Invariant: callers put the invocation in inFlight under the lock before
|
||||
// dispatching, so no inFlight.put here.
|
||||
byte[] payload = invocation.getPayload();
|
||||
|
||||
+62
-3
@@ -264,9 +264,9 @@ class RuntimeApiServerTest {
|
||||
|
||||
/**
|
||||
* Replaces the default {@code server} with a subclass whose test-only overrides
|
||||
* gate the three race points on the caller-supplied Runnables. Used only by the
|
||||
* two race tests below; other tests keep the plain server. Rebinds the port
|
||||
* because start() binds on construction.
|
||||
* gate the four race points on the caller-supplied Runnables. Used only by the
|
||||
* race tests below; other tests keep the plain server. Rebinds the port because
|
||||
* start() binds on construction.
|
||||
*/
|
||||
private void installGatedServer(Runnable beforeEnqueueDispatch,
|
||||
Runnable beforeNextPathGuard,
|
||||
@@ -281,6 +281,27 @@ class RuntimeApiServerTest {
|
||||
server.start().get(5, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Variant of {@link #installGatedServer} for the ctx-ends-during-dispatch race:
|
||||
* inject state after enqueue()'s dispatch lock is released, observe whether
|
||||
* sendInvocation subsequently ran.
|
||||
*/
|
||||
private void installGatedServerWithDispatchObservation(
|
||||
java.util.function.Consumer<io.vertx.ext.web.RoutingContext> afterEnqueueLockReleased,
|
||||
java.util.function.Consumer<String> onSendInvocation) throws Exception {
|
||||
server.stop().get(5, TimeUnit.SECONDS);
|
||||
port = findFreePort();
|
||||
server = new RuntimeApiServer(vertx, port) {
|
||||
@Override protected void afterEnqueueDispatchLockReleased(io.vertx.ext.web.RoutingContext waitingCtx) {
|
||||
afterEnqueueLockReleased.accept(waitingCtx);
|
||||
}
|
||||
@Override protected void beforeSendInvocationWrite(String requestId) {
|
||||
onSendInvocation.accept(requestId);
|
||||
}
|
||||
};
|
||||
server.start().get(5, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Timeout(15)
|
||||
void enqueueDeferredDispatch_racedByQuiesce_doesNotDeliverAfterSweep() throws Exception {
|
||||
@@ -432,6 +453,44 @@ class RuntimeApiServerTest {
|
||||
assertTrue(new String(result.getPayload()).contains("ContainerStopped"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Timeout(15)
|
||||
void enqueueDeferredDispatch_ctxEndsAfterLockRelease_dispatchesOrRequeuesAtomically() throws Exception {
|
||||
// Pre-fix, enqueue()'s deferred callback read waitingCtx.response().ended()
|
||||
// twice — once inside the lock deciding requeue, once outside deciding send.
|
||||
// A disconnect between the two reads made neither branch fire and stranded
|
||||
// the invocation in inFlight until the function deadline. Force that
|
||||
// interleaving by ending the ctx from afterEnqueueDispatchLockReleased.
|
||||
java.util.concurrent.atomic.AtomicInteger sendInvocationCalls =
|
||||
new java.util.concurrent.atomic.AtomicInteger();
|
||||
installGatedServerWithDispatchObservation(
|
||||
waitingCtx -> {
|
||||
if (!waitingCtx.response().ended()) {
|
||||
waitingCtx.response().setStatusCode(500).end();
|
||||
}
|
||||
},
|
||||
requestId -> sendInvocationCalls.incrementAndGet());
|
||||
|
||||
HttpRequest parkedRequest = HttpRequest.newBuilder()
|
||||
.uri(URI.create("http://localhost:" + port + "/2018-06-01/runtime/invocation/next"))
|
||||
.GET().build();
|
||||
httpClient.sendAsync(parkedRequest, HttpResponse.BodyHandlers.ofString());
|
||||
awaitWaitingContexts(1);
|
||||
|
||||
PendingInvocation invocation = new PendingInvocation(
|
||||
"req-ctx-ends", "{}".getBytes(), System.currentTimeMillis() + 60_000,
|
||||
"arn:aws:lambda:us-east-1:000000000000:function:test",
|
||||
new CompletableFuture<>());
|
||||
server.enqueue(invocation);
|
||||
|
||||
// Wait for the runOnContext-scheduled dispatch to have fired.
|
||||
Thread.sleep(500);
|
||||
|
||||
assertEquals(1, sendInvocationCalls.get(),
|
||||
"sendInvocation must run exactly once — a zero count means the "
|
||||
+ "invocation was stranded (the pre-fix stranded-inFlight bug).");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Timeout(15)
|
||||
void quiesceAtomicallyClearsInFlightWithStopped() throws Exception {
|
||||
|
||||
Reference in new issue
Block a user