Remove recursion in waitForProcess

This commit is contained in:
Vitaliy Filippov 2021-07-19 17:17:38 +03:00
parent a7a8d4906e
commit a0cf78dcbc

View file

@ -4,6 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"math"
"os" "os"
"os/exec" "os/exec"
"strings" "strings"
@ -87,7 +88,7 @@ func FuseUnmount(path string) error {
return nil return nil
} }
glog.Infof("Found fuse pid %v of mount %s, checking if it still runs", process.Pid, path) glog.Infof("Found fuse pid %v of mount %s, checking if it still runs", process.Pid, path)
return waitForProcess(process, 1) return waitForProcess(process, 20)
} }
func waitForMount(path string, timeout time.Duration) error { func waitForMount(path string, timeout time.Duration) error {
@ -128,10 +129,8 @@ func findFuseMountProcess(path string) (*os.Process, error) {
return nil, nil return nil, nil
} }
func waitForProcess(p *os.Process, backoff int) error { func waitForProcess(p *os.Process, limit int) error {
if backoff == 20 { for backoff := 0; backoff < limit; backoff++ {
return fmt.Errorf("Timeout waiting for PID %v to end", p.Pid)
}
cmdLine, err := getCmdLine(p.Pid) cmdLine, err := getCmdLine(p.Pid)
if err != nil { if err != nil {
glog.Warningf("Error checking cmdline of PID %v, assuming it is dead: %s", p.Pid, err) glog.Warningf("Error checking cmdline of PID %v, assuming it is dead: %s", p.Pid, err)
@ -149,8 +148,9 @@ func waitForProcess(p *os.Process, backoff int) error {
return nil return nil
} }
glog.Infof("Fuse process with PID %v still active, waiting...", p.Pid) glog.Infof("Fuse process with PID %v still active, waiting...", p.Pid)
time.Sleep(time.Duration(backoff*100) * time.Millisecond) time.Sleep(time.Duration(math.Pow(1.5, float64(backoff))*100) * time.Millisecond)
return waitForProcess(p, backoff+1) }
return fmt.Errorf("Timeout waiting for PID %v to end", p.Pid)
} }
func getCmdLine(pid int) (string, error) { func getCmdLine(pid int) (string, error) {