mirror of
				https://github.com/kata-containers/kata-containers.git
				synced 2025-11-04 11:50:15 +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:
		@@ -345,37 +345,40 @@ func WaitLocalProcess(pid int, timeoutSecs uint, initialSignal syscall.Signal, l
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	pidRunning := true
 | 
						pidRunning := true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						secs := time.Duration(timeoutSecs)
 | 
				
			||||||
 | 
						timeout := time.After(secs * time.Second)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Wait for the VM process to terminate
 | 
						// Wait for the VM process to terminate
 | 
				
			||||||
	startTime := time.Now()
 | 
					outer:
 | 
				
			||||||
 | 
					 | 
				
			||||||
	for {
 | 
						for {
 | 
				
			||||||
		var _status syscall.WaitStatus
 | 
							select {
 | 
				
			||||||
		var _rusage syscall.Rusage
 | 
							case <-time.After(50 * time.Millisecond):
 | 
				
			||||||
		var waitedPid int
 | 
								// Check if the process is running periodically to avoid a busy loop
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// "A watched pot never boils" and an unwaited-for process never appears to die!
 | 
								var _status syscall.WaitStatus
 | 
				
			||||||
		waitedPid, err = syscall.Wait4(pid, &_status, syscall.WNOHANG, &_rusage)
 | 
								var _rusage syscall.Rusage
 | 
				
			||||||
 | 
								var waitedPid int
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// "A watched pot never boils" and an unwaited-for process never appears to die!
 | 
				
			||||||
 | 
								waitedPid, err = syscall.Wait4(pid, &_status, syscall.WNOHANG, &_rusage)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								if waitedPid == pid && err == nil {
 | 
				
			||||||
 | 
									pidRunning = false
 | 
				
			||||||
 | 
									break outer
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								if err = syscall.Kill(pid, syscall.Signal(0)); err != nil {
 | 
				
			||||||
 | 
									pidRunning = false
 | 
				
			||||||
 | 
									break outer
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if waitedPid == pid && err == nil {
 | 
					 | 
				
			||||||
			pidRunning = false
 | 
					 | 
				
			||||||
			break
 | 
								break
 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if err = syscall.Kill(pid, syscall.Signal(0)); err != nil {
 | 
					 | 
				
			||||||
			pidRunning = false
 | 
					 | 
				
			||||||
			break
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if time.Since(startTime).Seconds() >= float64(timeoutSecs) {
 | 
					 | 
				
			||||||
			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 {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user