Run rollback code in ExplicitTxn dtor before clearing the currentTxn#5707
Merged
Run rollback code in ExplicitTxn dtor before clearing the currentTxn#5707
Conversation
Cloudflare’s production implementation asserts that all SQLite operations occur with `currentTxn` set to either an ImplicitTxn or an ExplicitTxn. Previously, the ExplicitTxn dtor rolled back the transaction after setting the `currentTxn` to NoTxn, which caused the assertion to fail in certain workloads. The fix is to run the code that resets `currentTxn` after doing the rollback. I used Claude to find the bug, but the fix is my own.
justin-mp
commented
Dec 16, 2025
| } | ||
| ActorSqlite::ExplicitTxn::~ExplicitTxn() noexcept(false) { | ||
| [&]() noexcept { | ||
| KJ_DEFER([&]() noexcept { |
Contributor
Author
There was a problem hiding this comment.
It looks like this will run regardless of the KJ_DEFER? the lambda is called right away, which case would it not run?
KJ_DEFER runs the code in between the parens inside yet another lambda so the code itself doesn't get executed until leaving the scope by return or by exception. In kj's common.h:
#define KJ_DEFER(code) auto KJ_UNIQUE_NAME(_kjDefer) = ::kj::defer([&](){code;})
// Run the given code when the function exits, whether by return or exception.
Also why does this have to be a lambd in first place? Can just be normal code?
The lambda is marked noexcept (compared to noexcept(false) so that if the code throws in any way, it crashes the process. As the existing comment says, this was done to prevent dangling pointers.
shrima-cf
approved these changes
Dec 16, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Cloudflare’s production implementation asserts that all SQLite operations occur with
currentTxnset to either an ImplicitTxn or an ExplicitTxn. Previously, the ExplicitTxn dtor rolled back the transaction after setting thecurrentTxnto NoTxn, which caused the assertion to fail in certain workloads. The fix is to run the code that resetscurrentTxnafter doing the rollback.I used Claude to find the bug, but the fix is my own.