| 1 | Subject: [PATCH] qemu: close the wait channel only after both producers finish |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | @@ -553,29 +553,22 @@ |
| 8 | return nil, err |
| 9 | } |
| 10 | l.qCmd = qCmd |
| 11 | - l.qWaitCh = make(chan error, 1) |
| 12 | - go func() { |
| 13 | - defer close(l.qWaitCh) |
| 14 | - defer cancel() |
| 15 | - err := qCmd.Wait() |
| 16 | - if l.usernetStop != nil { |
| 17 | - l.usernetStop() |
| 18 | - } |
| 19 | - l.qWaitCh <- err |
| 20 | - }() |
| 21 | l.vhostCmds = vhostCmds |
| 22 | - go func() { |
| 23 | + l.qWaitCh = watchQEMU(qCmd.Wait, func() error { |
| 24 | client := l.usernetClient |
| 25 | if usernetIndex := limayaml.FirstUsernetIndex(l.Instance.Config); usernetIndex != -1 { |
| 26 | client = usernet.NewClientByName(l.Instance.Config.Networks[usernetIndex].Lima) |
| 27 | } |
| 28 | if client != nil { |
| 29 | - err := client.ConfigureDriver(ctx, l.Instance, l.SSHLocalPort) |
| 30 | - if err != nil { |
| 31 | - l.qWaitCh <- err |
| 32 | - } |
| 33 | - } |
| 34 | - }() |
| 35 | + return client.ConfigureDriver(ctx, l.Instance, l.SSHLocalPort) |
| 36 | + } |
| 37 | + return nil |
| 38 | + }, func() { |
| 39 | + cancel() |
| 40 | + if l.usernetStop != nil { |
| 41 | + l.usernetStop() |
| 42 | + } |
| 43 | + }) |
| 44 | |
| 45 | // QEMU's UEFI boot order doesn't work well for Windows guest OS. |
| 46 | // We send a key periodically to proceed from "Press any key to boot from CD or DVD...". |
| 47 | @@ -834,7 +827,7 @@ |
| 48 | return nil |
| 49 | } |
| 50 | |
| 51 | -func (l *LimaQemuDriver) shutdownQEMU(ctx context.Context, timeout time.Duration, qCmd *exec.Cmd, qWaitCh <-chan error) error { |
| 52 | +func (l *LimaQemuDriver) shutdownQEMU(ctx context.Context, timeout time.Duration, qCmd *exec.Cmd, qWaitCh chan error) error { |
| 53 | // "power button" refers to ACPI on the most archs, except RISC-V |
| 54 | logrus.Info("Shutting down QEMU with the power button") |
| 55 | client := l.usernetClient |
| 56 | @@ -892,7 +885,7 @@ |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | -func (l *LimaQemuDriver) killQEMU(_ context.Context, _ time.Duration, qCmd *exec.Cmd, qWaitCh <-chan error) error { |
| 61 | +func (l *LimaQemuDriver) killQEMU(_ context.Context, _ time.Duration, qCmd *exec.Cmd, qWaitCh chan error) error { |
| 62 | var qWaitErr error |
| 63 | if qCmd.ProcessState == nil { |
| 64 | if killErr := qCmd.Process.Kill(); killErr != nil { |
| 65 | |
| 66 | new file mode 100644 |
| 67 | |
| 68 | |
| 69 | @@ -0,0 +1,30 @@ |
| 70 | +// SPDX-FileCopyrightText: Copyright The Lima Authors |
| 71 | +// SPDX-License-Identifier: Apache-2.0 |
| 72 | + |
| 73 | +package qemu |
| 74 | + |
| 75 | +import "sync" |
| 76 | + |
| 77 | +// watchQEMU reports process exit and network setup failures on the driver's |
| 78 | +// existing wait channel. afterExit cancels setup and releases the netstack. |
| 79 | +func watchQEMU(wait, configure func() error, afterExit func()) chan error { |
| 80 | + // Each producer can publish once even when Start's caller has stopped |
| 81 | + // receiving. Only the coordinator closes the channel, after both finish. |
| 82 | + errors := make(chan error, 2) |
| 83 | + var producers sync.WaitGroup |
| 84 | + producers.Add(2) |
| 85 | + go func() { |
| 86 | + defer producers.Done() |
| 87 | + err := wait() |
| 88 | + afterExit() |
| 89 | + errors <- err |
| 90 | + }() |
| 91 | + go func() { |
| 92 | + defer producers.Done() |
| 93 | + if err := configure(); err != nil { |
| 94 | + errors <- err |
| 95 | + } |
| 96 | + }() |
| 97 | + go func() { producers.Wait(); close(errors) }() |
| 98 | + return errors |
| 99 | +} |
| 100 | |
| 101 | new file mode 100644 |
| 102 | |
| 103 | |
| 104 | @@ -0,0 +1,61 @@ |
| 105 | +// SPDX-FileCopyrightText: Copyright The Lima Authors |
| 106 | +// SPDX-License-Identifier: Apache-2.0 |
| 107 | + |
| 108 | +package qemu |
| 109 | + |
| 110 | +import ( |
| 111 | + "errors" |
| 112 | + "testing" |
| 113 | + "time" |
| 114 | +) |
| 115 | + |
| 116 | +func TestQEMUProcessExitDuringNetworkConfiguration(t *testing.T) { |
| 117 | + processError := errors.New("WHPX: Failed to enable nested virtualization, hr=80370302") |
| 118 | + networkError := errors.New("network setup ended after QEMU exit") |
| 119 | + release := make(chan struct{}) |
| 120 | + cleaned := make(chan struct{}) |
| 121 | + results := watchQEMU(func() error { return processError }, func() error { <-release; return networkError }, func() { close(cleaned) }) |
| 122 | + select { |
| 123 | + case err := <-results: |
| 124 | + if !errors.Is(err, processError) { |
| 125 | + t.Fatalf("lost original process error: %v", err) |
| 126 | + } |
| 127 | + case <-time.After(time.Second): |
| 128 | + t.Fatal("process exit was not reported") |
| 129 | + } |
| 130 | + <-cleaned |
| 131 | + close(release) |
| 132 | + select { |
| 133 | + case err, ok := <-results: |
| 134 | + if !ok || !errors.Is(err, networkError) { |
| 135 | + t.Fatalf("closed before the network producer completed: %v %v", ok, err) |
| 136 | + } |
| 137 | + case <-time.After(time.Second): |
| 138 | + t.Fatal("late network setup failure blocked") |
| 139 | + } |
| 140 | + select { |
| 141 | + case _, ok := <-results: |
| 142 | + if ok { |
| 143 | + t.Fatal("unexpected extra event") |
| 144 | + } |
| 145 | + case <-time.After(time.Second): |
| 146 | + t.Fatal("finished producers did not close the wait channel") |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +func TestQEMUNetworkFailureDoesNotBlockProcessReaping(t *testing.T) { |
| 151 | + processDone := make(chan struct{}) |
| 152 | + reaped := make(chan struct{}) |
| 153 | + results := watchQEMU(func() error { <-processDone; return errors.New("killed") }, func() error { return errors.New("network failed") }, func() { close(reaped) }) |
| 154 | + // Stop consuming after the first startup error, just as a failed Start |
| 155 | + // caller can do. Both producers must still finish and reap the process. |
| 156 | + <-results |
| 157 | + close(processDone) |
| 158 | + select { |
| 159 | + case <-reaped: |
| 160 | + case <-time.After(time.Second): |
| 161 | + t.Fatal("process cleanup blocked behind an abandoned consumer") |
| 162 | + } |
| 163 | + for range results { |
| 164 | + } |
| 165 | +} |