From 6f2de8606205d3ea87dd12260e8928b5ed9ddb84 Mon Sep 17 00:00:00 2001 From: Miguel Molina Date: Tue, 2 Apr 2019 16:51:21 +0200 Subject: [PATCH] sql/index/pilosa: correctly guard mapping transactions Signed-off-by: Miguel Molina --- sql/index/pilosa/mapping.go | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/sql/index/pilosa/mapping.go b/sql/index/pilosa/mapping.go index 718b56831..4642b96de 100644 --- a/sql/index/pilosa/mapping.go +++ b/sql/index/pilosa/mapping.go @@ -20,9 +20,8 @@ import ( type mapping struct { path string - mut sync.RWMutex - txmut sync.RWMutex - db *bolt.DB + mut sync.RWMutex + db *bolt.DB // in create mode there's only one transaction closed explicitly by // commit function @@ -129,20 +128,19 @@ func (m *mapping) rollback() error { } func (m *mapping) transaction(writable bool, f func(*bolt.Tx) error) error { + m.clientMut.Lock() + defer m.clientMut.Unlock() + var tx *bolt.Tx var err error if m.create { - m.clientMut.Lock() if m.tx == nil { m.tx, err = m.db.Begin(true) if err != nil { - m.clientMut.Unlock() return err } } - m.clientMut.Unlock() - tx = m.tx } else { tx, err = m.db.Begin(writable) @@ -151,15 +149,8 @@ func (m *mapping) transaction(writable bool, f func(*bolt.Tx) error) error { } } - m.txmut.Lock() err = f(tx) - m.txmut.Unlock() - - m.clientMut.Lock() - create := m.create - m.clientMut.Unlock() - - if create { + if m.create { return err }