Skip to content

Commit

Permalink
fix: prevent concurrent map writes in extcmd
Browse files Browse the repository at this point in the history
  • Loading branch information
ReuDa committed Nov 22, 2023
1 parent 75e2b87 commit fa54375
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.8.11

- prevent concurrent map writes in `extcmd`

## 1.8.10

- provide http handler to deal with ETags
Expand Down
10 changes: 5 additions & 5 deletions extcmd/extcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"sync"
)

var states = make(map[string]*CmdState)
var states = sync.Map{}

// NewCmdState create a new CmdState and registers it as a global state. The expected call pattern
// is that NewCmdState is called immediately after the exec.Cmd is created, but before the Cmd
Expand All @@ -32,23 +32,23 @@ func NewCmdState(cmd *exec.Cmd) *CmdState {
cmd.Stdout = state
cmd.Stderr = state

states[state.Id] = state
states.Store(state.Id, state)

return state
}

// GetCmdState returns the state stored under the given ID or an error in case there is no persisted
// state under the ID.
func GetCmdState(id string) (*CmdState, error) {
state, ok := states[id]
state, ok := states.Load(id)
if !ok {
return nil, fmt.Errorf("failed to find a command state with ID '%s'", id)
}
return state, nil
return state.(*CmdState), nil
}

// RemoveCmdState removes the state with the given ID. A no-op in case there is no state with this ID.
// It is the caller's responsibility to ensure that the exec.Cmd itself is stopped.
func RemoveCmdState(id string) {
delete(states, id)
states.Delete(id)
}

0 comments on commit fa54375

Please sign in to comment.