From e04eff6ccd614ebcacf6313fa1baf572c81e7ce4 Mon Sep 17 00:00:00 2001 From: Matteo Suppo Date: Tue, 5 Sep 2017 08:53:50 +0200 Subject: [PATCH] Mitigate race conditions on upload A better way would be to use sync.Map but we need to update go to 1.9 first --- upload/upload.go | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/upload/upload.go b/upload/upload.go index 8b14f5d55..6a3b5b9e4 100644 --- a/upload/upload.go +++ b/upload/upload.go @@ -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() + } } } @@ -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) @@ -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)