tmplock must be closed before removal

defer are called in LIFO style, so os.Remove was called before tmplock.Close
This commit is contained in:
oliverpool 2017-03-02 15:25:55 +01:00 committed by GitHub
parent 8a00983980
commit 5f902b89e3
1 changed files with 4 additions and 2 deletions

View File

@ -82,8 +82,10 @@ func (l Lockfile) TryLock() error {
if err != nil { if err != nil {
return err return err
} else { } else {
defer tmplock.Close() defer func(){
defer os.Remove(tmplock.Name()) tmplock.Close()
os.Remove(tmplock.Name())
}()
} }
_, err = tmplock.WriteString(fmt.Sprintf("%d\n", os.Getpid())) _, err = tmplock.WriteString(fmt.Sprintf("%d\n", os.Getpid()))