Skip to content

Commit 94e9f37

Browse files
authored
Merge pull request #120 from infosiftr/dry-run-deadlock
Fix `deploy --dry-run` deadlock
2 parents 3e3f75b + bf82d71 commit 94e9f37

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

cmd/deploy/main.go

+22-12
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
@@ -242,14 +260,6 @@ func main() {
242260

243261
if dryRun {
244262
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-
}
253263
}
254264

255265
wg.Wait()

0 commit comments

Comments
 (0)