@@ -8,6 +8,8 @@ package mdbx
88import "C"
99
1010import (
11+ "fmt"
12+ "github.com/erigontech/mdbx-go/mdbx/threads"
1113 "log"
1214 "time"
1315 "unsafe"
@@ -77,6 +79,8 @@ type Txn struct {
7779 // be paid. The id of a Txn cannot change over its life, even if it is
7880 // reset/renewed
7981 id uint64
82+
83+ tid uint64
8084}
8185
8286// beginTxn does not lock the OS thread which is a prerequisite for creating a
@@ -86,6 +90,7 @@ func beginTxn(env *Env, parent *Txn, flags uint) (*Txn, error) {
8690 readonly : flags & Readonly != 0 ,
8791 env : env ,
8892 }
93+ txn .tid = threads .CurrentThreadID ()
8994
9095 var ptxn * C.MDBX_txn
9196 if parent != nil {
@@ -243,6 +248,7 @@ type CommitLatencyGC struct {
243248
244249func (txn * Txn ) commit () (CommitLatency , error ) {
245250 var _stat C.MDBX_commit_latency
251+ txn .strictThreadCheck ()
246252 ret := C .mdbx_txn_commit_ex (txn ._txn , & _stat )
247253 txn .clearTxn ()
248254 s := CommitLatency {
@@ -297,6 +303,14 @@ func (txn *Txn) Abort() {
297303 txn .abort ()
298304}
299305
306+ func (txn * Txn ) strictThreadCheck () {
307+ currentThread := threads .CurrentThreadID ()
308+ if currentThread != txn .tid {
309+ msg := fmt .Sprintf ("thread mismatch. not allowed current %d, open in %d" , currentThread , txn .tid )
310+ panic (msg )
311+ }
312+ }
313+
300314func (txn * Txn ) abort () {
301315 if txn ._txn == nil {
302316 return
@@ -305,6 +319,7 @@ func (txn *Txn) abort() {
305319 // Get a read-lock on the environment so we can abort txn if needed.
306320 // txn.env **should** terminate all readers otherwise when it closes.
307321 txn .env .closeLock .RLock ()
322+ txn .strictThreadCheck ()
308323 if txn .env ._env != nil {
309324 C .mdbx_txn_abort (txn ._txn )
310325 }
0 commit comments