fix(server): keep the ongoing tx when a statement fails before execution - #2137
Merged
Merged
Conversation
A statement rejected before the SQL engine runs it - a parse error being the common case - returns no transaction while leaving the caller's ongoing one open and uncancelled. Both callers that hold the only reference to that transaction overwrote it with the returned nil, orphaning an OngoingTx that still held its tbtree snapshots. Snapshots are released solely by Commit and Cancel, and tbtree.snapshots only shrinks on Snapshot.Close, so each failure leaked one permanently. After maxActiveSnapshots (default 100) failures every new transaction failed with "tbtree: max active snapshots limit reached" until the server was restarted. The session path made this unrecoverable: with sqlTx nil, IsClosed reports true, so TxSQLExec removed the transaction from the session and neither the client's Rollback nor session close and expiry could reach it any more. That is also why the reported client signature shows Rollback answering "no transaction found". Guard both assignment sites on what actually happened to the transaction rather than on the error: adopt the returned transaction only when the engine produced one, or when the previous one is genuinely closed. Execution-stage errors are unaffected because the engine cancels the transaction itself, so the reference is still replaced and the session still drops it. Note the leak did not originate in Engine.Exec: the session path parses in db.SQLExec (pkg/database/sql.go) and returns before the engine is reached. Other pre-execution returns leak the same way - empty statement lists, parameter normalisation failures, replica rejections - so the fix belongs in the callers that own the reference, not in the individual early returns. Fixes #2127
SimoneLazzaris
approved these changes
Jul 30, 2026
SimoneLazzaris
self-requested a review
July 31, 2026 08:42
SimoneLazzaris
previously approved these changes
Jul 31, 2026
TestTx_Errors asserted the symptom that d491954 removes: a parse error used to leave sqlTx nil, so IsClosed reported true, the session dropped the transaction and the next statement answered "no transaction found". The transaction now stays open, so that statement reaches the parser and returns its own syntax error instead. Assert that error, and roll back at the end - on the old code the rollback is exactly what failed, so it pins the fix through the full client and session path rather than only at the transactions package. The sessions import goes with the ErrTransactionNotFound reference.
Collaborator
SimoneLazzaris
approved these changes
Aug 3, 2026
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.
Fixes #2127. Also very likely fixes #1998, which hits the same error through the pgsql adapter.
The bug
A statement rejected before the SQL engine executes it — a parse error being the common case — returns no transaction while leaving the caller's ongoing one open and uncancelled. The two callers that hold the only reference to that transaction overwrote it with the returned
nil:pkg/server/sessions/internal/transactions/transactions.go:124—tx.sqlTx, _, err = tx.db.SQLExec(...)pkg/pgsql/server/query_machine.go:1205—s.tx = ntx, assigned even whenerr != nilThat orphans an
OngoingTxstill holding its tbtree snapshots. Snapshots are released only byCommit/Cancel, andtbtree.snapshotsshrinks only viaSnapshot.Close(), so each failure leaked one permanently. AftermaxActiveSnapshots(default 100) failures, every new transaction failed withtbtree: max active snapshots limit reacheduntil the server was restarted. The reporter hit this four times in production, with a 1:1 correlation between failed statements and leaked snapshots.On the session path it was unrecoverable: with
sqlTx == nil,IsClosed()reports true, soTxSQLExecremoved the transaction from the session — after which neither the client'sRollbacknorSession.RollbackTransactions()on close/expiry could reach it. That also explains the reported client signature, whereRollbackanswersno transaction found.The fix
Guard both assignment sites on what actually happened to the transaction, not on the error:
Rollbacknow succeeds instead of answeringno transaction found, and close/expiry can reclaim it. Leak gone.currTx.Cancel(), so the tx is closed, the reference is replaced and the session drops it. No behaviour change.COMMIT;— returnsntx == nilwith a closed tx, so the assignment still happens.Why here and not in the engine
The issue proposed fixing
Engine.Exec. That would not have fixed it: the session path parses indb.SQLExec(pkg/database/sql.go:364) and returns before the engine is ever reached.engine.go:756is a second, independent instance of the same defect, reachable only by embedded users.More importantly, the engine returning
nilis not itself the leak — a caller that passestxin still owns it. The leak is assigning thatnilover the only reference to a still-open tx. Pre-execution returns that leave the tx open include:pkg/database/sql.go:364/:379/:386— parse error, empty statement list, replicaembedded/sql/engine.go:756/:795/:800/:809— parse error, empty list,normalizeParamsfailure,nilstatementGuarding the two reference-owning callers closes all of them at once. Patching individual early returns would keep regressing as new ones are added.
Tests
Three tests in
transactions_test.go, written before the fix and confirmed to fail without it:TestSQLExecPreExecutionErrorKeepsTxOpenIsClosed()true,Rollback()returnsErrNoOngoingTxTestSQLExecPreExecutionErrorDoesNotLeakSnapshotsmaxActiveSnapshots = 4) with the snapshot-limit error, reproducing the reported 1:1 correlationTestSQLExecExecutionErrorClosesTxThe leak test mirrors the reported production cycle, including the client carrying on after its rollback fails.
Verification
go build ./...cleango test ./pkg/server/sessions/... ./pkg/server/ ./pkg/pgsql/server/... ./pkg/database/...— 10 packages passgo test ./embedded/sql/... ./embedded/tbtree/...passThe pgsql suite matters here because the second hunk touches the extended-query path recently changed by
ce64e941andc4a5beed.