mirror of
https://github.com/containers/podman.git
synced 2026-02-05 15:45:08 +01:00
Fix race condition in CleanupGVProxy when reading gvproxy PID file
When startVM fails quickly, CleanupGVProxy may attempt to read the gvproxy.pid file before gvproxy has written it, causing cleanup to fail. This commit adds retry logic that waits up to 2 seconds for the PID file to appear. Signed-off-by: lstocchi <lstocchi@redhat.com>
This commit is contained in:
@@ -5,13 +5,37 @@ import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/containers/podman/v6/pkg/machine/define"
|
||||
)
|
||||
|
||||
const (
|
||||
pidFileWaitTimeout = 2 * time.Second
|
||||
pidFileCheckInterval = 50 * time.Millisecond
|
||||
)
|
||||
|
||||
func readPIDFileWithRetry(f define.VMFile) ([]byte, error) {
|
||||
deadline := time.Now().Add(pidFileWaitTimeout)
|
||||
|
||||
for time.Now().Before(deadline) {
|
||||
gvPid, err := f.Read()
|
||||
if err == nil {
|
||||
return gvPid, nil
|
||||
}
|
||||
if !errors.Is(err, fs.ErrNotExist) {
|
||||
return nil, err
|
||||
}
|
||||
time.Sleep(pidFileCheckInterval)
|
||||
}
|
||||
|
||||
// Final attempt after timeout
|
||||
return f.Read()
|
||||
}
|
||||
|
||||
// CleanupGVProxy reads the --pid-file for gvproxy attempts to stop it
|
||||
func CleanupGVProxy(f define.VMFile) error {
|
||||
gvPid, err := f.Read()
|
||||
gvPid, err := readPIDFileWithRetry(f)
|
||||
if err != nil {
|
||||
// The file will also be removed by gvproxy when it exits so
|
||||
// we need to account for the race and can just ignore it here.
|
||||
|
||||
Reference in New Issue
Block a user