Skip to content

Commit bb95603

Browse files
Random-Liushentubot
authored andcommitted
runsc: fix panic for runsc wait on stopped container.
PiperOrigin-RevId: 203016694
1 parent 1cd0c31 commit bb95603

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

runsc/container/container.go

+9
Original file line numberDiff line numberDiff line change
@@ -352,20 +352,29 @@ func (c *Container) Pid() int {
352352
// Wait waits for the container to exit, and returns its WaitStatus.
353353
func (c *Container) Wait() (syscall.WaitStatus, error) {
354354
log.Debugf("Wait on container %q", c.ID)
355+
if c.Status == Stopped {
356+
return 0, fmt.Errorf("container is stopped")
357+
}
355358
return c.Sandbox.Wait(c.ID)
356359
}
357360

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

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

runsc/container/container_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,9 @@ func TestMultiContainerWait(t *testing.T) {
12111211
} else if es := ws.ExitStatus(); es != 0 {
12121212
t.Errorf("process %q exited with non-zero status %d", strings.Join(containers[1].Spec.Process.Args, " "), es)
12131213
}
1214+
if _, err := containers[1].Wait(); err == nil {
1215+
t.Errorf("wait for stopped process %q should fail", strings.Join(containers[1].Spec.Process.Args, " "))
1216+
}
12141217

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

0 commit comments

Comments
 (0)