Skip to content

Commit

Permalink
runsc: fix panic for runsc wait on stopped container.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 203016694
Change-Id: Ic51ef754aa6d7d1b3b35491aff96a63d7992e122
  • Loading branch information
Random-Liu authored and shentubot committed Jul 2, 2018
1 parent fa64c2a commit 126296c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
9 changes: 9 additions & 0 deletions runsc/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,20 +352,29 @@ func (c *Container) Pid() int {
// Wait waits for the container to exit, and returns its WaitStatus.
func (c *Container) Wait() (syscall.WaitStatus, error) {
log.Debugf("Wait on container %q", c.ID)
if c.Status == Stopped {
return 0, fmt.Errorf("container is stopped")
}
return c.Sandbox.Wait(c.ID)
}

// WaitRootPID waits for process 'pid' in the sandbox's PID namespace and
// returns its WaitStatus.
func (c *Container) WaitRootPID(pid int32) (syscall.WaitStatus, error) {
log.Debugf("Wait on pid %d in sandbox %q", pid, c.Sandbox.ID)
if c.Status == Stopped {
return 0, fmt.Errorf("container is stopped")
}
return c.Sandbox.WaitPID(pid, c.Sandbox.ID)
}

// WaitPID waits for process 'pid' in the container's PID namespace and returns
// its WaitStatus.
func (c *Container) WaitPID(pid int32) (syscall.WaitStatus, error) {
log.Debugf("Wait on pid %d in container %q", pid, c.ID)
if c.Status == Stopped {
return 0, fmt.Errorf("container is stopped")
}
return c.Sandbox.WaitPID(pid, c.ID)
}

Expand Down
6 changes: 6 additions & 0 deletions runsc/container/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1211,6 +1211,9 @@ func TestMultiContainerWait(t *testing.T) {
} else if es := ws.ExitStatus(); es != 0 {
t.Errorf("process %q exited with non-zero status %d", strings.Join(containers[1].Spec.Process.Args, " "), es)
}
if _, err := containers[1].Wait(); err == nil {
t.Errorf("wait for stopped process %q should fail", strings.Join(containers[1].Spec.Process.Args, " "))
}

// After Wait returns, ensure that the root container is running and
// the child has finished.
Expand All @@ -1231,6 +1234,9 @@ func TestMultiContainerWait(t *testing.T) {
} else if es := ws.ExitStatus(); es != 0 {
t.Errorf("PID %d exited with non-zero status %d", pid, es)
}
if _, err := containers[0].WaitPID(pid); err == nil {
t.Errorf("wait for stopped PID %d should fail", pid)
}
}()
}

Expand Down

0 comments on commit 126296c

Please sign in to comment.