Skip to content

Commit 23a73bb

Browse files
committed
test:add ci for cleanup fifos
Signed-off-by: ningmingxiao <[email protected]>
1 parent bd89fee commit 23a73bb

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

cmd/nerdctl/container/container_stop_linux_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,23 @@ func TestStopWithTimeout(t *testing.T) {
199199
// The container should get the SIGKILL before the 10s default timeout
200200
assert.Assert(t, elapsed < 10*time.Second, "Container did not respect --timeout flag")
201201
}
202+
func TestStopCleanupFIFOs(t *testing.T) {
203+
if rootlessutil.IsRootless() {
204+
t.Skip("/run/containerd/fifo/ doesn't exist on rootless")
205+
}
206+
testutil.DockerIncompatible(t)
207+
base := testutil.NewBase(t)
208+
testContainerName := testutil.Identifier(t)
209+
oldNumFifos, err := countFIFOFiles("/run/containerd/fifo/")
210+
assert.NilError(t, err)
211+
// Stop the container after 2 seconds
212+
go func() {
213+
time.Sleep(2 * time.Second)
214+
base.Cmd("stop", testContainerName).AssertOK()
215+
newNumFifos, err := countFIFOFiles("/run/containerd/fifo/")
216+
assert.NilError(t, err)
217+
assert.Equal(t, oldNumFifos, newNumFifos)
218+
}()
219+
// Start a container that is automatically removed after it exits
220+
base.Cmd("run", "--rm", "--name", testContainerName, testutil.NginxAlpineImage).AssertOK()
221+
}

pkg/containerutil/containerutil.go

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,12 @@ func Stop(ctx context.Context, container containerd.Container, timeout *time.Dur
385385

386386
switch status.Status {
387387
case containerd.Created, containerd.Stopped:
388+
// Cleanup the IO after a successful Stop
389+
if io := task.IO(); io != nil {
390+
if cerr := io.Close(); cerr != nil {
391+
log.G(ctx).Warnf("failed to close IO for container %s: %v", container.ID(), cerr)
392+
}
393+
}
388394
return nil
389395
case containerd.Paused, containerd.Pausing:
390396
paused = true
@@ -397,6 +403,13 @@ func Stop(ctx context.Context, container containerd.Container, timeout *time.Dur
397403
return err
398404
}
399405

406+
// signal will be sent once resume is finished
407+
if paused {
408+
if err := task.Resume(ctx); err != nil {
409+
log.G(ctx).Errorf("cannot unpause container %s: %s", container.ID(), err)
410+
return err
411+
}
412+
}
400413
if *timeout > 0 {
401414
sig, err := getSignal(signalValue, l)
402415
if err != nil {
@@ -407,20 +420,10 @@ func Stop(ctx context.Context, container containerd.Container, timeout *time.Dur
407420
return err
408421
}
409422

410-
// signal will be sent once resume is finished
411-
if paused {
412-
if err := task.Resume(ctx); err != nil {
413-
log.G(ctx).Warnf("Cannot unpause container %s: %s", container.ID(), err)
414-
} else {
415-
// no need to do it again when send sigkill signal
416-
paused = false
417-
}
418-
}
419-
420423
sigtermCtx, sigtermCtxCancel := context.WithTimeout(ctx, *timeout)
421424
defer sigtermCtxCancel()
422425

423-
err = waitContainerStop(sigtermCtx, exitCh, container.ID())
426+
err = waitContainerStop(sigtermCtx, task, exitCh, container.ID())
424427
if err == nil {
425428
return nil
426429
}
@@ -439,13 +442,7 @@ func Stop(ctx context.Context, container containerd.Container, timeout *time.Dur
439442
return err
440443
}
441444

442-
// signal will be sent once resume is finished
443-
if paused {
444-
if err := task.Resume(ctx); err != nil {
445-
log.G(ctx).Warnf("Cannot unpause container %s: %s", container.ID(), err)
446-
}
447-
}
448-
return waitContainerStop(ctx, exitCh, container.ID())
445+
return waitContainerStop(ctx, task, exitCh, container.ID())
449446
}
450447

451448
func getSignal(signalValue string, containerLabels map[string]string) (syscall.Signal, error) {
@@ -460,14 +457,20 @@ func getSignal(signalValue string, containerLabels map[string]string) (syscall.S
460457
return signal.ParseSignal("SIGTERM")
461458
}
462459

463-
func waitContainerStop(ctx context.Context, exitCh <-chan containerd.ExitStatus, id string) error {
460+
func waitContainerStop(ctx context.Context, task containerd.Task, exitCh <-chan containerd.ExitStatus, id string) error {
464461
select {
465462
case <-ctx.Done():
466463
if err := ctx.Err(); err != nil {
467464
return fmt.Errorf("wait container %v: %w", id, err)
468465
}
469466
return nil
470467
case status := <-exitCh:
468+
// Cleanup the IO after a successful Stop
469+
if io := task.IO(); io != nil {
470+
if cerr := io.Close(); cerr != nil {
471+
log.G(ctx).Warnf("failed to close IO for container %s: %v", id, cerr)
472+
}
473+
}
471474
return status.Error()
472475
}
473476
}

0 commit comments

Comments
 (0)