From b763b3992a2c4f16fc218e1920df5525dd75b114 Mon Sep 17 00:00:00 2001 From: Brielle Broder Date: Tue, 10 Jul 2018 14:57:20 -0700 Subject: Modified error message for clarity. Previously, error message only showed "" when child and pid were the same (since no error is returned by the Wait4 syscall in this case) which occurs when the process has incorrectly terminated. A new error message was added to improve clarity for such a case. Tests for this function were modified to reflect the improved distinction between process termination and error. PiperOrigin-RevId: 204018107 Change-Id: Ib38481c9590405e5bafcb6efe27fd49b3948910c --- runsc/specutils/specutils.go | 9 +++++++-- runsc/specutils/specutils_test.go | 7 +++++-- 2 files changed, 12 insertions(+), 4 deletions(-) (limited to 'runsc/specutils') diff --git a/runsc/specutils/specutils.go b/runsc/specutils/specutils.go index 34243e623..861e7fd70 100644 --- a/runsc/specutils/specutils.go +++ b/runsc/specutils/specutils.go @@ -324,9 +324,14 @@ func WaitForReady(pid int, timeout time.Duration, ready func() (bool, error)) er // Check if the process is still running. var ws syscall.WaitStatus var ru syscall.Rusage + + // If the process is alive, child is 0 because of the NOHANG option. + // If the process has terminated, child equals the process id. child, err := syscall.Wait4(pid, &ws, syscall.WNOHANG, &ru) - if err != nil || child == pid { - return fmt.Errorf("process (%d) is not running, err: %v", pid, err) + if err != nil { + return fmt.Errorf("error waiting for process: %v", err) + } else if child == pid { + return fmt.Errorf("process %d has terminated", pid) } // Process continues to run, backoff and retry. diff --git a/runsc/specutils/specutils_test.go b/runsc/specutils/specutils_test.go index 959be3af3..2dc5d90cc 100644 --- a/runsc/specutils/specutils_test.go +++ b/runsc/specutils/specutils_test.go @@ -76,8 +76,11 @@ func TestWaitForReadyNotRunning(t *testing.T) { err := WaitForReady(cmd.Process.Pid, 5*time.Second, func() (bool, error) { return false, nil }) - if !strings.Contains(err.Error(), "not running") { - t.Errorf("ProcessWaitReady got: %v, expected: not running", err) + if err != nil && !strings.Contains(err.Error(), "terminated") { + t.Errorf("ProcessWaitReady got: %v, expected: process terminated", err) + } + if err == nil { + t.Errorf("ProcessWaitReady incorrectly succeeded") } } -- cgit v1.2.3