Skip to content

Commit 89da512

Browse files
authored
Merge pull request containerd#1377 from crosbymichael/exec-delete
Allow exec process to be deleted in created state
2 parents 853fd15 + bf82de3 commit 89da512

File tree

3 files changed

+86
-3
lines changed

3 files changed

+86
-3
lines changed

container_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,3 +1451,82 @@ func TestContainerExitedAtSet(t *testing.T) {
14511451
return
14521452
}
14531453
}
1454+
1455+
func TestDeleteContainerExecCreated(t *testing.T) {
1456+
t.Parallel()
1457+
1458+
client, err := newClient(t, address)
1459+
if err != nil {
1460+
t.Fatal(err)
1461+
}
1462+
defer client.Close()
1463+
1464+
var (
1465+
image Image
1466+
ctx, cancel = testContext()
1467+
id = t.Name()
1468+
)
1469+
defer cancel()
1470+
1471+
if runtime.GOOS != "windows" {
1472+
image, err = client.GetImage(ctx, testImage)
1473+
if err != nil {
1474+
t.Error(err)
1475+
return
1476+
}
1477+
}
1478+
1479+
spec, err := generateSpec(withImageConfig(ctx, image), withProcessArgs("sleep", "100"))
1480+
if err != nil {
1481+
t.Error(err)
1482+
return
1483+
}
1484+
container, err := client.NewContainer(ctx, id, WithSpec(spec), withNewSnapshot(id, image))
1485+
if err != nil {
1486+
t.Error(err)
1487+
return
1488+
}
1489+
defer container.Delete(ctx, WithSnapshotCleanup)
1490+
1491+
task, err := container.NewTask(ctx, empty())
1492+
if err != nil {
1493+
t.Error(err)
1494+
return
1495+
}
1496+
defer task.Delete(ctx)
1497+
1498+
finished := make(chan struct{}, 1)
1499+
go func() {
1500+
if _, err := task.Wait(ctx); err != nil {
1501+
t.Error(err)
1502+
}
1503+
close(finished)
1504+
}()
1505+
1506+
if err := task.Start(ctx); err != nil {
1507+
t.Error(err)
1508+
return
1509+
}
1510+
1511+
// start an exec process without running the original container process info
1512+
processSpec := spec.Process
1513+
withExecExitStatus(processSpec, 6)
1514+
execID := t.Name() + "_exec"
1515+
process, err := task.Exec(ctx, execID, processSpec, empty())
1516+
if err != nil {
1517+
t.Error(err)
1518+
return
1519+
}
1520+
deleteStatus, err := process.Delete(ctx)
1521+
if err != nil {
1522+
t.Error(err)
1523+
return
1524+
}
1525+
if deleteStatus != 0 {
1526+
t.Errorf("expected delete exit code 0 but received %d", deleteStatus)
1527+
}
1528+
if err := task.Kill(ctx, syscall.SIGKILL); err != nil {
1529+
t.Error(err)
1530+
}
1531+
<-finished
1532+
}

process.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ func (p *process) Delete(ctx context.Context, opts ...ProcessDeleteOpts) (uint32
152152
if err != nil {
153153
return UnknownExitStatus, err
154154
}
155-
if status.Status != Stopped {
155+
switch status.Status {
156+
case Running, Paused, Pausing:
156157
return UnknownExitStatus, errors.Wrapf(errdefs.ErrFailedPrecondition, "process must be stopped before deletion")
157158
}
158159
if p.io != nil {

windows/process.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ func (p *process) Status() runtime.Status {
5959
case <-p.exitCh:
6060
status = runtime.StoppedStatus
6161
default:
62+
if p.hcs == nil {
63+
return runtime.CreatedStatus
64+
}
6265
status = runtime.RunningStatus
6366
}
6467
return status
@@ -83,8 +86,8 @@ func (p *process) Pid() uint32 {
8386
}
8487

8588
func (p *process) ExitCode() (uint32, time.Time, error) {
86-
if p.Status() != runtime.StoppedStatus {
87-
return 255, time.Time{}, errors.Wrap(errdefs.ErrFailedPrecondition, "process is not stopped")
89+
if s := p.Status(); s != runtime.StoppedStatus && s != runtime.CreatedStatus {
90+
return 255, time.Time{}, errors.Wrapf(errdefs.ErrFailedPrecondition, "process is not stopped: %s", s)
8891
}
8992
return p.exitCode, p.exitTime, nil
9093
}

0 commit comments

Comments
 (0)