Skip to content

Run rollback code in ExplicitTxn dtor before clearing the currentTxn#5707

Merged
justin-mp merged 1 commit intomainfrom
jmp/transaction-unexpected-write
Dec 16, 2025
Merged

Run rollback code in ExplicitTxn dtor before clearing the currentTxn#5707
justin-mp merged 1 commit intomainfrom
jmp/transaction-unexpected-write

Conversation

@justin-mp
Copy link
Copy Markdown
Contributor

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.

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 justin-mp requested review from a team as code owners December 16, 2025 18:17
}
ActorSqlite::ExplicitTxn::~ExplicitTxn() noexcept(false) {
[&]() noexcept {
KJ_DEFER([&]() noexcept {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@justin-mp justin-mp merged commit 871858f into main Dec 16, 2025
31 of 33 checks passed
@justin-mp justin-mp deleted the jmp/transaction-unexpected-write branch December 16, 2025 22:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants