| 1 | Subject: [PATCH] instance: do not leave goroutines blocked after Start returns |
| 2 | |
| 3 | StartWithPaths races two goroutines (the hostagent's events and its exit) |
| 4 | on unbuffered channels and returns on the first. The loser blocks on its |
| 5 | send for as long as the program lives: one goroutine per start in a program |
| 6 | that outlives the call (limactl exits, so it does not show there). The |
| 7 | channels hold one value, so the sends complete; the exit goroutine also |
| 8 | finishes its Wait, which reaps the hostagent when it ends. |
| 9 | |
| 10 | No test: StartWithPaths starts a hostagent, which Lima's unit tests do not. |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | @@ -319,12 +319,15 @@ func StartWithPaths(ctx context.Context, inst *limatype.Instance, launchHostAgen |
| 18 | return err |
| 19 | } |
| 20 | |
| 21 | - watchErrCh := make(chan error) |
| 22 | + // Buffered: the goroutine that loses the race below must not block on its |
| 23 | + // send for as long as the program lives (it would, once StartWithPaths |
| 24 | + // has returned). |
| 25 | + watchErrCh := make(chan error, 1) |
| 26 | go func() { |
| 27 | watchErrCh <- watchHostAgentEvents(ctx, inst, haStdoutPath, haStderrPath, begin, showProgress) |
| 28 | close(watchErrCh) |
| 29 | }() |
| 30 | - waitErrCh := make(chan error) |
| 31 | + waitErrCh := make(chan error, 1) |
| 32 | if haCmd != nil { |
| 33 | go func() { |
| 34 | waitErrCh <- haCmd.Wait() |