Skip to content

Commit e04eff6

Browse files
committed
Mitigate race conditions on upload
A better way would be to use sync.Map but we need to update go to 1.9 first
1 parent 0372d93 commit e04eff6

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

upload/upload.go

+13-9
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,14 @@ func Serial(port, commandline string, extra Extra, l Logger) error {
142142
return program(z[0], z[1:], l)
143143
}
144144

145+
var cmds = map[*exec.Cmd]bool{}
146+
145147
// Kill stops any upload process as soon as possible
146148
func Kill() {
147-
log.Println(cmd)
148-
if cmd != nil && cmd.Process.Pid > 0 {
149-
cmd.Process.Kill()
149+
for cmd := range cmds {
150+
if cmd.Process.Pid > 0 {
151+
cmd.Process.Kill()
152+
}
150153
}
151154
}
152155

@@ -257,14 +260,9 @@ func waitReset(beforeReset []string, l Logger, originalPort string) string {
257260
return port
258261
}
259262

260-
// cmd is the upload command
261-
var cmd *exec.Cmd
262-
263263
// program spawns the given binary with the given args, logging the sdtout and stderr
264264
// through the Logger
265265
func program(binary string, args []string, l Logger) error {
266-
defer func() { cmd = nil }()
267-
268266
// remove quotes form binary command and args
269267
binary = strings.Replace(binary, "\"", "", -1)
270268

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

281-
cmd = exec.Command(binary, args...)
279+
cmd := exec.Command(binary, args...)
280+
281+
// Add the command to the map of running commands
282+
cmds[cmd] = true
283+
defer func() {
284+
delete(cmds, cmd)
285+
}()
282286

283287
utilities.TellCommandNotToSpawnShell(cmd)
284288

0 commit comments

Comments
 (0)