Skip to content

Commit

Permalink
Ensure that status lockfile is closed before trying to release work
Browse files Browse the repository at this point in the history
For context, see:

- ansible#319
- ansible/awx#9961
  • Loading branch information
shanemcd committed May 13, 2021
1 parent e8a789e commit a3134f8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/workceptor/interfaces.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package workceptor

import (
"github.com/rogpeppe/go-internal/lockedfile"
)

// WorkUnit represents a local unit of work
type WorkUnit interface {
ID() string
Expand Down Expand Up @@ -31,4 +35,5 @@ type StatusFileData struct {
StdoutSize int64
WorkType string
ExtraData interface{}
LockFile *lockedfile.File
}
8 changes: 8 additions & 0 deletions pkg/workceptor/workunitbase.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func (bwu *BaseWorkUnit) StdoutFileName() string {
func (sfd *StatusFileData) lockStatusFile(filename string) (*lockedfile.File, error) {
lockFileName := filename + ".lock"
lockFile, err := lockedfile.OpenFile(lockFileName, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
sfd.LockFile = lockFile
if err != nil {
return nil, err
}
Expand All @@ -123,6 +124,7 @@ func (sfd *StatusFileData) lockStatusFile(filename string) (*lockedfile.File, er
// unlockStatusFile releases the lock on the status file
func (sfd *StatusFileData) unlockStatusFile(filename string, lockFile *lockedfile.File) {
err := lockFile.Close()
sfd.LockFile = nil
if err != nil {
logger.Error("Error closing %s.lock: %s", filename, err)
}
Expand Down Expand Up @@ -366,8 +368,14 @@ func (bwu *BaseWorkUnit) UnredactedStatus() *StatusFileData {
func (bwu *BaseWorkUnit) Release(force bool) error {
bwu.statusLock.Lock()
defer bwu.statusLock.Unlock()
if bwu.status.LockFile != nil {
// There seems to be a race condition with the `defer`s that
// handle closing this lockfile.
bwu.status.LockFile.Close()
}
err := os.RemoveAll(bwu.UnitDir())
if err != nil && !force {
logger.Error("Error when releasing work: %s\n", err)
return err
}
bwu.w.activeUnitsLock.Lock()
Expand Down

0 comments on commit a3134f8

Please sign in to comment.