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