Repository navigation
mdbx: guard Txn.Reset and Txn.Renew with closeLock, export ErrEnvClosed - #260
Closed
AskAlexSharov wants to merge 7 commits into
Closed
AskAlexSharov wants to merge 7 commits into
AskAlexSharov wants to merge 7 commits into
Conversation
Both called into the env after Env.Close freed it; libmdbx reports MDBX_EBADSIGN. No strictThreadCheck: a read-only txn may be reset and renewed on another thread.
The post-close test passes even with both RLock pairs removed, so it covered the nil-env check rather than the locking this guards.
Callers pool on Reset() == nil, and C.mdbx_txn_reset(nil) used to return EINVAL, so returning nil would queue a dead txn.
Abort the txn, wait for the goroutine to start before timing the block, and drop a wrong claim that a reset read txn is what keeps Close off MDBX_BUSY.
Cursor.Close, Commit and Rollback also dereferenced a freed env; a deferred cursor Close outliving Env.Close is the normal idiom. Callers could not match the error either, so errNotOpen is now exported.
Callers own the do-not-use-after-Env.Close contract and erigon already waits for readers, so the bindings need not absorb that. A read lock per cursor close would also contend the very path the read-txn pool exists to decontend.
AskAlexSharov
requested review from
awskii and
sudeepdino008
and
a balanced review from Copilot
September 23, 2026 04:12
AskAlexSharov
marked this pull request as draft
September 24, 2026 08:05
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
Only a non-blocking documentation nit remains.
Review effort: Balanced
Findings: 1
What changed in this PR
Prevents Txn.Reset and Txn.Renew from racing with Env.Close and exports a matchable environment error.
Changes:
- Guards reset and renew operations with
closeLock. - Exports
ErrEnvClosed. - Adds transaction lifecycle and locking tests.
| File | Description |
|---|---|
mdbx/txn.go |
Adds close locking and environment checks. |
mdbx/txn_test.go |
Adds lifecycle and synchronization tests. |
mdbx/env.go |
Exports ErrEnvClosed; its documentation should cover all not-open states (nit). |
mdbx/env_windows.go |
Uses the exported sentinel on Windows. |
mdbx/env_not_win.go |
Uses the exported sentinel on non-Windows systems. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| } | ||
|
|
||
| var errNotOpen = errors.New("environment is not open") | ||
| // ErrEnvClosed is returned by a Txn or Cursor operation whose Env has been closed. |
Collaborator
Author
|
Closing: erigon already orders the read-txn pool against Env.Close (Rollback resets before trackTxEnd, BeginRo renews after trackTxBegin, Close waits for the count), so this race cannot happen from erigon, and the read lock would land on every pooled read transaction, the path the limiter removal just made lock-free. |
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.

Txn.ResetandTxn.Renewcalled into the env without holdingcloseLock, so a concurrentEnv.Closecould free the env mid-call — libmdbx then reportsMDBX_EBADSIGN.Txn.Abortalready took that guard; these two now do the same:reset()/renew()takecloseLock.RLock()and returnErrEnvClosedwhenenv._env == nil.strictThreadCheckin either: a read-only txn may legitimately be reset and renewed on a different thread than the one that began it.Related changes:
errNotOpenis now exported asErrEnvClosed. Callers had no way to match it witherrors.Is.Reset()on a terminated txn no longer returnsnil. Callers pool onReset() == nil, so returningnilfor a dead txn queued it back into the pool. The_txn == nilfast path is gone; libmdbx's owncheck_txn()rejects the null handle withMDBX_EINVAL, so the caller gets an error either way.RLockpairs removed — it covered the nil-env check, not the locking. The new one waits for the goroutine to start before timing the block, aborts the txn, and drops a wrong claim that a reset read txn is what keepsCloseoffMDBX_BUSY.Cursor.Close,CommitandRollbackkeep no guard. Callers own the do-not-use-after-Env.Closecontract and erigon already waits for readers. A read lock per cursor close would contend exactly the path the read-txn pool exists to decontend.go testandgo test -racepass on darwin/arm64.