Skip to content

mdbx: guard Txn.Reset and Txn.Renew with closeLock, export ErrEnvClosed - #260

Closed
AskAlexSharov wants to merge 7 commits into
masterfrom
fix-reset-renew-closelock
Closed

AskAlexSharov wants to merge 7 commits into
masterfrom
fix-reset-renew-closelock

Conversation

@AskAlexSharov

Copy link
Copy Markdown
Collaborator

Txn.Reset and Txn.Renew called into the env without holding closeLock, so a concurrent Env.Close could free the env mid-call — libmdbx then reports MDBX_EBADSIGN. Txn.Abort already took that guard; these two now do the same:

  • reset() / renew() take closeLock.RLock() and return ErrEnvClosed when env._env == nil.
  • No strictThreadCheck in either: a read-only txn may legitimately be reset and renewed on a different thread than the one that began it.

Related changes:

  • errNotOpen is now exported as ErrEnvClosed. Callers had no way to match it with errors.Is.
  • Reset() on a terminated txn no longer returns nil. Callers pool on Reset() == nil, so returning nil for a dead txn queued it back into the pool. The _txn == nil fast path is gone; libmdbx's own check_txn() rejects the null handle with MDBX_EINVAL, so the caller gets an error either way.
  • Tests tightened. The old post-close test passed with both RLock pairs 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 keeps Close off MDBX_BUSY.
  • Cursor.Close, Commit and Rollback keep no guard. Callers own the do-not-use-after-Env.Close contract 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 test and go test -race pass on darwin/arm64.

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
AskAlexSharov requested review from awskii and sudeepdino008 and a balanced review from Copilot September 23, 2026 04:12
@AskAlexSharov
AskAlexSharov marked this pull request as draft September 24, 2026 08:05

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot review overview

🟢 Approval recommended

Only a non-blocking documentation nit remains.

Review effort: Balanced
Findings: 1 Low severity

Open (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.

Comment thread mdbx/env.go
}

var errNotOpen = errors.New("environment is not open")
// ErrEnvClosed is returned by a Txn or Cursor operation whose Env has been closed.
@AskAlexSharov

Copy link
Copy Markdown
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.

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