Repository navigation
Protect Transaction#close in Database#runAsync from cancellation - #13754
jkt-signal wants to merge 4 commits into
Conversation
8910301 to
f163296
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
I believe the two failing tests are unrelated to this change (one is in the C API bindings which I have not touched, and one is a build failure with no additional information that I suspect to be a timeout). How can I request the required review from a second approver? |
saintstack
left a comment
There was a problem hiding this comment.
From local LLM, there are other locations that could do with the fix. What you think?
Review: PR #13754 — Protect Transaction#close in Database#runAsync from cancellation
Verdict: not yet — the 2-line production fix is correct and I'd take it as-is, but the new test can call Database_dispose(0), and the same bug exists at three other sites.
The diagnosis is correct, and precisely so
The mechanism is subtler than "cancellation skips callbacks," and the PR body gets it right. In the JDK, uniWhenComplete guards its action with if (result == null) — the action runs only if the dependent stage isn't already complete. So:
- Before:
X.thenApply(fixup).whenComplete(close)— the returned future is thewhenCompletestage.cancel(true)completes it, so whenXlater finishes, the action is skipped. Transaction leaked. - After:
X.whenComplete(close).thenApply(fixup)— the returned future is thethenApplystage. Cancelling it leaves thewhenCompletestage untouched, soclose()still runs whenXcompletes.
The swap is also type- and exception-preserving: the whenComplete lambda ignores both arguments, whenComplete passes the upstream result through unchanged, and fixup (o -> returnValue.get()) ignores its input, so the intermediate type is irrelevant. Exceptional completion propagates identically in both orders, including when close() itself throws.
readAsync delegates to runAsync (FDBDatabase.java:110-113), so it's covered by the same change — worth noting explicitly, since a reviewer would otherwise wonder.
Worth stating: the fix bounds the leak, it doesn't eliminate the wait
CompletableFuture cancellation doesn't propagate upstream, so after runFuture.cancel(true) the underlying retry/commit chain keeps running and the transaction closes only when it completes. That's strictly better than never closing, but a caller cancelling because they want to abandon a slow transaction still won't get the handle released promptly. The PR title ("protect from cancellation") is accurate; the description could say that the close is deferred rather than immediate, so nobody reads this as making cancellation eager.
Incomplete: three sibling sites have the identical bug
// RangeQuery.java:89-90
FutureResults range = tr.getRange_internal(...);
return range.thenApply(result -> result.get().values)
.whenComplete((result, e) -> range.close());
// MappedRangeQuery.java:92
return range.thenApply(result -> result.get().values).whenComplete((result, e) -> range.close());
// FDBTransaction.java:741 (onError)
return f.thenApply(v -> tr).whenComplete((v, t) -> { if (t != null) { tr.close(); } });All three are thenApply(...).whenComplete(close) with the returned future being the whenComplete stage — same skip, same leak, same one-line fix.
The first two matter more than the one being fixed: they leak native FutureResults/FutureMappedResults handles, and abandoning a range query is far more common than cancelling runAsync. The onError one leaks a transaction that transfer() just handed over, so nobody else owns it.
I'd want these in this PR — three reordered lines, identical reasoning, and you already have the model loaded. If you'd rather keep the change surgical, please at least file them, because the next person to hit a FutureResults leak will have no reason to connect it to this fix.
Test defect: Database_dispose(0)
The anonymous FDBDatabase subclass overrides createTransaction and setOption but not closeInternal. So:
FDBDatabase.finalize() → checkUnclosed("Database") → close() → closeInternal(0) → Database_dispose(0) — a JNI call with a null handle, at GC time.
FakeFDBTransaction overrides closeInternal at line 150 for exactly this reason, so the precedent is already in the codebase and was missed here. Two consequences:
- A latent, nondeterministic JVM crash whenever GC happens to finalize the fake database. It may never fire in a short run, which is the worst failure mode.
checkUnclosedprintsDatabase not closedto stderr — ironic in a PR whose stated goal is eliminatingTransaction not closedwarnings.
Fix: override closeInternal to a no-op in the anonymous subclass, and close the database in the test (try-with-resources, since Database is AutoCloseable).
On the dismissed CI failure
You wrote that the foundationdb-pr-clang failure (ctest … exit status 8) is unrelated because "one is in the C API bindings which I have not touched." That's plausible, and the macOS failure is clearly the ci.lock timeout. But this PR introduces a native wrapper constructed with a null pointer that gets disposed at finalization — which is exactly the shape of thing that produces a confusing native-side ctest failure. I can't see the log from here, so I'm not claiming they're connected; I'm saying "unrelated" should be established by naming the failing test rather than inferred. Fixing the closeInternal override first and re-running is the cheap way to settle it.
Otherwise the test is good
Using an inline executor (command.run()) makes it fully deterministic — someLongOperation.complete(null) drives the whole chain synchronously, so there's no sleep and no flake. And it genuinely fails without the production change, which is the property that matters. Registering it in tests.cmake is correct, and the "does not require a running server" javadoc is a useful orientation note.
Minor: EXECUTOR should be final; assertTrue would read better with a message.
Process note
To answer your question in-thread: FDB requires two approvals, and the usual route is to ping a maintainer directly on the PR rather than through any UI affordance.
Result of foundationdb-pr-clang-ide on Linux RHEL 9
|
Result of foundationdb-pr-clang-arm on Linux RHEL 9
|
Result of foundationdb-pr-clang on Linux RHEL 9
|
Result of foundationdb-pr-macos-m1 on macOS 14.x
|
Result of foundationdb-pr on Linux RHEL 9
|
Result of foundationdb-pr-cluster-tests on Linux RHEL 9
|
Result of foundationdb-pr-macos on macOS 14.x
|
f163296 to
60d7e27
Compare
|
Apologies for abandoning this; I missed the LLM review comments among the CI notes. I've made the equivalent change in the extra callsites listed and fixed the test to not call |
|
@gxglass @saintstack this seems to have fallen by the wayside; any chance I can get some eyes on it? |
FDBDatabase#runAsyncreturns a future on a complicated chain of retry and cleanup logic to create a transaction, run the user-supplied closure, commit the transaction, and close it. The final future actually returned is the result ofCompletableFuture#whenComplete(/* close the open transaction */).Unfortunately, this means that if the returned future is cancelled, the
whenCompletecallback is also not called, resulting in the last-opened transaction not being closed, and in turnTransaction not closedwarnings appearing on stderr.This change fixes that problem by swapping the order of two
CompletableFuturemethod calls, moving thewhenCompleteabove aCompletableFuture#thenApplycall that fixes up the return value to whatrunAsync's future is expected to supply. Cancelling that future is not a problem, since the fixed-up return value would be clobbered by the resultingCancellationExceptionanyway, andCompletableFuturecancellation does not propagate to parent futures' tasks.We also add a unit test verifying the behavior of
db.runAsync(something).cancel(true)with respect to closing opened transactions. This test reliably fails without the change toFDBDatabaseand passes with it.