From 720f03c6c23c8791e667daeaf923af8246afc2e8 Mon Sep 17 00:00:00 2001 From: James Mills Date: Tue, 17 Nov 2020 19:30:44 +1000 Subject: [PATCH] Fix a race condition around .Close() and .Sync() --- bitcask.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/bitcask.go b/bitcask.go index 00898c9..2ed5567 100644 --- a/bitcask.go +++ b/bitcask.go @@ -87,7 +87,10 @@ func (b *Bitcask) Stats() (stats Stats, err error) { // Close() as this is the only way to cleanup the lock held by the open // database. func (b *Bitcask) Close() error { + b.mu.RLock() + defer func() { + b.mu.RUnlock() b.Flock.Unlock() os.Remove(b.Flock.Path()) }() @@ -107,6 +110,8 @@ func (b *Bitcask) Close() error { // Sync flushes all buffers to disk ensuring all data is written func (b *Bitcask) Sync() error { + b.mu.RLock() + defer b.mu.RUnlock() return b.curr.Sync() }