mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-06-26 15:32:30 +00:00
utils: Make WaitLocalProcess safer
Rather than relying on the system clock, use a channel timeout to avoid problems if the system time changed. Signed-off-by: James O. D. Hunt <james.o.hunt@intel.com>
This commit is contained in:
parent
9256e590dc
commit
de2631e711
@ -345,10 +345,16 @@ func WaitLocalProcess(pid int, timeoutSecs uint, initialSignal syscall.Signal, l
|
|||||||
|
|
||||||
pidRunning := true
|
pidRunning := true
|
||||||
|
|
||||||
// Wait for the VM process to terminate
|
secs := time.Duration(timeoutSecs)
|
||||||
startTime := time.Now()
|
timeout := time.After(secs * time.Second)
|
||||||
|
|
||||||
|
// Wait for the VM process to terminate
|
||||||
|
outer:
|
||||||
for {
|
for {
|
||||||
|
select {
|
||||||
|
case <-time.After(50 * time.Millisecond):
|
||||||
|
// Check if the process is running periodically to avoid a busy loop
|
||||||
|
|
||||||
var _status syscall.WaitStatus
|
var _status syscall.WaitStatus
|
||||||
var _rusage syscall.Rusage
|
var _rusage syscall.Rusage
|
||||||
var waitedPid int
|
var waitedPid int
|
||||||
@ -358,24 +364,21 @@ func WaitLocalProcess(pid int, timeoutSecs uint, initialSignal syscall.Signal, l
|
|||||||
|
|
||||||
if waitedPid == pid && err == nil {
|
if waitedPid == pid && err == nil {
|
||||||
pidRunning = false
|
pidRunning = false
|
||||||
break
|
break outer
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = syscall.Kill(pid, syscall.Signal(0)); err != nil {
|
if err = syscall.Kill(pid, syscall.Signal(0)); err != nil {
|
||||||
pidRunning = false
|
pidRunning = false
|
||||||
break
|
break outer
|
||||||
}
|
}
|
||||||
|
|
||||||
if time.Since(startTime).Seconds() >= float64(timeoutSecs) {
|
break
|
||||||
pidRunning = true
|
|
||||||
|
|
||||||
|
case <-timeout:
|
||||||
logger.Warnf("process %v still running after waiting %ds", pid, timeoutSecs)
|
logger.Warnf("process %v still running after waiting %ds", pid, timeoutSecs)
|
||||||
|
|
||||||
break
|
break outer
|
||||||
}
|
}
|
||||||
|
|
||||||
// Brief pause to avoid a busy loop
|
|
||||||
time.Sleep(time.Duration(50) * time.Millisecond)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if pidRunning {
|
if pidRunning {
|
||||||
|
Loading…
Reference in New Issue
Block a user