Skip to content

Mitigate race conditions on upload #174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 5, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,14 @@ func Serial(port, commandline string, extra Extra, l Logger) error {
return program(z[0], z[1:], l)
}

var cmds = map[*exec.Cmd]bool{}

// Kill stops any upload process as soon as possible
func Kill() {
log.Println(cmd)
if cmd != nil && cmd.Process.Pid > 0 {
cmd.Process.Kill()
for cmd := range cmds {
if cmd.Process.Pid > 0 {
cmd.Process.Kill()
}
}
}

Expand Down Expand Up @@ -257,14 +260,9 @@ func waitReset(beforeReset []string, l Logger, originalPort string) string {
return port
}

// cmd is the upload command
var cmd *exec.Cmd

// program spawns the given binary with the given args, logging the sdtout and stderr
// through the Logger
func program(binary string, args []string, l Logger) error {
defer func() { cmd = nil }()

// remove quotes form binary command and args
binary = strings.Replace(binary, "\"", "", -1)

Expand All @@ -278,7 +276,13 @@ func program(binary string, args []string, l Logger) error {
extension = ".exe"
}

cmd = exec.Command(binary, args...)
cmd := exec.Command(binary, args...)

// Add the command to the map of running commands
cmds[cmd] = true
defer func() {
delete(cmds, cmd)
}()

utilities.TellCommandNotToSpawnShell(cmd)

Expand Down