Fixes for writeDirtyToDisk.

This commit fixes two issues in the writeDirtyToDisk function:

First, closing the temporary files is now done using a defer, so they
are always closed.

Second, the various account mutexs are no longer unlocked using a
defer, preventing more than one from being held at once and causing a
deadlock caused by incorrect locking order.
This commit is contained in:
Josh Rickmar 2014-01-28 14:09:19 -05:00
parent 0d903a5a29
commit dd3d7467c3

View file

@ -217,20 +217,22 @@ func (a *Account) writeDirtyToDisk() error {
if err != nil { if err != nil {
return err return err
} }
defer tmpfile.Close()
a.UtxoStore.Lock() a.UtxoStore.RLock()
defer a.UtxoStore.Unlock() _, err = a.UtxoStore.s.WriteTo(tmpfile)
a.UtxoStore.RUnlock()
if _, err = a.UtxoStore.s.WriteTo(tmpfile); err != nil { if err != nil {
return err return err
} }
tmpfile.Close()
if err = Rename(tmpfile.Name(), utxofilepath); err != nil { if err = Rename(tmpfile.Name(), utxofilepath); err != nil {
return err return err
} }
a.UtxoStore.Lock()
a.UtxoStore.dirty = false a.UtxoStore.dirty = false
a.UtxoStore.Unlock()
} }
// Transactions // Transactions
@ -243,20 +245,22 @@ func (a *Account) writeDirtyToDisk() error {
if err != nil { if err != nil {
return err return err
} }
defer tmpfile.Close()
a.TxStore.Lock() a.TxStore.RLock()
defer a.TxStore.Unlock() _, err = a.TxStore.s.WriteTo(tmpfile)
a.TxStore.RUnlock()
if _, err = a.TxStore.s.WriteTo(tmpfile); err != nil { if err != nil {
return err return err
} }
tmpfile.Close()
if err = Rename(tmpfile.Name(), txfilepath); err != nil { if err = Rename(tmpfile.Name(), txfilepath); err != nil {
return err return err
} }
a.TxStore.Lock()
a.TxStore.dirty = false a.TxStore.dirty = false
a.TxStore.Unlock()
} }
// Wallet // Wallet
@ -269,20 +273,22 @@ func (a *Account) writeDirtyToDisk() error {
if err != nil { if err != nil {
return err return err
} }
defer tmpfile.Close()
a.mtx.Lock() a.mtx.RLock()
defer a.mtx.Unlock() _, err = a.Wallet.WriteTo(tmpfile)
a.mtx.RUnlock()
if _, err = a.WriteTo(tmpfile); err != nil { if err != nil {
return err return err
} }
tmpfile.Close()
if err = Rename(tmpfile.Name(), wfilepath); err != nil { if err = Rename(tmpfile.Name(), wfilepath); err != nil {
return err return err
} }
a.mtx.Lock()
a.dirty = false a.dirty = false
a.mtx.Unlock()
} }
return nil return nil