Skip to content

Commit d3e04d1

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

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
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: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,6 @@ func Stop(ctx context.Context, container containerd.Container, timeout *time.Dur
402402
if err != nil {
403403
return err
404404
}
405-
406405
if err := task.Kill(ctx, sig); err != nil {
407406
return err
408407
}
@@ -411,9 +410,7 @@ func Stop(ctx context.Context, container containerd.Container, timeout *time.Dur
411410
if paused {
412411
if err := task.Resume(ctx); err != nil {
413412
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
413+
return err
417414
}
418415
}
419416

@@ -434,16 +431,17 @@ func Stop(ctx context.Context, container containerd.Container, timeout *time.Dur
434431
if err != nil {
435432
return err
436433
}
437-
438434
if err := task.Kill(ctx, sig); err != nil {
439435
return err
440436
}
441-
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-
}
437+
// Cleanup the IO after a successful Stop
438+
io := task.IO()
439+
if task.IO() != nil {
440+
defer func() {
441+
if cerr := io.Close(); cerr != nil {
442+
log.G(ctx).Warnf("failed to close IO for container %s: %v", container.ID(), cerr)
443+
}
444+
}()
447445
}
448446
return waitContainerStop(ctx, exitCh, container.ID())
449447
}

0 commit comments

Comments
 (0)