Fix a race condition around .Close() and .Sync()

This commit is contained in:
James Mills 2020-11-17 19:30:44 +10:00
parent e900e2fa77
commit 720f03c6c2
No known key found for this signature in database
GPG Key ID: AC4C014F1440EBD6

@ -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()
}