Skip to content

Commit

Permalink
added fd mutex to prevent data race on history updates with closing t…
Browse files Browse the repository at this point in the history
…he history file (chzyer#66)
  • Loading branch information
jcramb authored and chzyer committed Jul 29, 2016
1 parent 62c6fe6 commit a0c5244
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions history.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"container/list"
"fmt"
"os"
"sync"
"strings"
)

Expand All @@ -25,6 +26,7 @@ type opHistory struct {
historyVer int64
current *list.Element
fd *os.File
fdLock sync.Mutex
}

func newOpHistory(cfg *Config) (o *opHistory) {
Expand All @@ -41,6 +43,8 @@ func (o *opHistory) Reset() {
}

func (o *opHistory) IsHistoryClosed() bool {
o.fdLock.Lock()
defer o.fdLock.Unlock()
return o.fd.Fd() == ^(uintptr(0))
}

Expand All @@ -58,6 +62,8 @@ func (o *opHistory) initHistory() {

// only called by newOpHistory
func (o *opHistory) historyUpdatePath(path string) {
o.fdLock.Lock()
defer o.fdLock.Unlock()
f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
return
Expand Down Expand Up @@ -93,6 +99,8 @@ func (o *opHistory) Compact() {
}

func (o *opHistory) Rewrite() {
o.fdLock.Lock()
defer o.fdLock.Unlock()
if o.cfg.HistoryFile == "" {
return
}
Expand Down Expand Up @@ -123,6 +131,8 @@ func (o *opHistory) Rewrite() {
}

func (o *opHistory) Close() {
o.fdLock.Lock()
defer o.fdLock.Unlock()
if o.fd != nil {
o.fd.Close()
}
Expand Down Expand Up @@ -267,6 +277,8 @@ func (o *opHistory) Revert() {
}

func (o *opHistory) Update(s []rune, commit bool) (err error) {
o.fdLock.Lock()
defer o.fdLock.Unlock()
s = runes.Copy(s)
if o.current == nil {
o.Push(s)
Expand Down

0 comments on commit a0c5244

Please sign in to comment.