Skip to content

Commit 4c2a2cc

Browse files
committed
Fix deploy --dry-run deadlock
Once we got over 1000 items in our deploy list, we deadlocked because nothing consumes the channel until we're done with the "queue everything up" loop, so we need to start consuming that list right away instead. As long as it's part of our waitgroup, this should be fine and work correctly.
1 parent 3e3f75b commit 4c2a2cc

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

cmd/deploy/main.go

+23-15
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ func main() {
2222
args = os.Args[1:]
2323

2424
// --dry-run
25-
dryRun bool
26-
dryRunOuts chan chan []byte
25+
dryRun bool
2726

2827
// --parallel
2928
parallel bool
@@ -35,8 +34,6 @@ func main() {
3534
switch arg {
3635
case "--dry-run":
3736
dryRun = true
38-
// we want to allow parallel, but want the output to be in-order so we resynchronize output with a channel of channels (technically this also limits parallelization, but hopefully this limit is generous enough that it doesn't matter)
39-
dryRunOuts = make(chan chan []byte, 1000)
4037

4138
case "--parallel":
4239
parallel = true
@@ -68,6 +65,27 @@ func main() {
6865
childMutexes := sync.Map{}
6966
wg := sync.WaitGroup{}
7067

68+
var dryRunOuts chan chan []byte
69+
if dryRun {
70+
// we want to allow parallel, but want the output to be in-order so we resynchronize output with a channel of channels (technically this also limits parallelization, but hopefully this limit is generous enough that it doesn't matter)
71+
dryRunOuts = make(chan chan []byte, 100000)
72+
73+
// we also have to start consuming that channel immediately, just in case we *do* hit that parallelization limit 🙈
74+
wg.Add(1)
75+
go func() {
76+
defer wg.Done()
77+
78+
for dryRunOut := range dryRunOuts {
79+
j, ok := <-dryRunOut
80+
if !ok {
81+
// (I think) this means we didn't output anything, so this should be all our "skips"
82+
continue
83+
}
84+
fmt.Printf("%s\n", j)
85+
}
86+
}()
87+
}
88+
7189
dec := json.NewDecoder(stdout)
7290
for dec.More() {
7391
var raw inputRaw
@@ -240,17 +258,7 @@ func main() {
240258
}
241259
}
242260

243-
if dryRun {
244-
close(dryRunOuts)
245-
for dryRunOut := range dryRunOuts {
246-
j, ok := <-dryRunOut
247-
if !ok {
248-
// (I think) this means we didn't output anything, so this should be all our "skips"
249-
continue
250-
}
251-
fmt.Printf("%s\n", j)
252-
}
253-
}
261+
close(dryRunOuts)
254262

255263
wg.Wait()
256264
}

0 commit comments

Comments
 (0)