Skip to content

Commit 1d87452

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

File tree

2 files changed

+39
-23
lines changed

2 files changed

+39
-23
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: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -397,30 +397,25 @@ func Stop(ctx context.Context, container containerd.Container, timeout *time.Dur
397397
return err
398398
}
399399

400+
// signal will be sent once resume is finished
401+
if paused {
402+
if err := task.Resume(ctx); err != nil {
403+
log.G(ctx).Errorf("Cannot unpause container %s: %s", container.ID(), err)
404+
return err
405+
}
406+
}
400407
if *timeout > 0 {
401408
sig, err := getSignal(signalValue, l)
402409
if err != nil {
403410
return err
404411
}
405-
406412
if err := task.Kill(ctx, sig); err != nil {
407413
return err
408414
}
409-
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-
420415
sigtermCtx, sigtermCtxCancel := context.WithTimeout(ctx, *timeout)
421416
defer sigtermCtxCancel()
422417

423-
err = waitContainerStop(sigtermCtx, exitCh, container.ID())
418+
err = waitContainerStop(sigtermCtx, task, exitCh, container.ID())
424419
if err == nil {
425420
return nil
426421
}
@@ -434,18 +429,10 @@ func Stop(ctx context.Context, container containerd.Container, timeout *time.Dur
434429
if err != nil {
435430
return err
436431
}
437-
438432
if err := task.Kill(ctx, sig); err != nil {
439433
return err
440434
}
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-
}
447-
}
448-
return waitContainerStop(ctx, exitCh, container.ID())
435+
return waitContainerStop(ctx, task, exitCh, container.ID())
449436
}
450437

451438
func getSignal(signalValue string, containerLabels map[string]string) (syscall.Signal, error) {
@@ -460,14 +447,23 @@ func getSignal(signalValue string, containerLabels map[string]string) (syscall.S
460447
return signal.ParseSignal("SIGTERM")
461448
}
462449

463-
func waitContainerStop(ctx context.Context, exitCh <-chan containerd.ExitStatus, id string) error {
450+
func waitContainerStop(ctx context.Context, task containerd.Task, exitCh <-chan containerd.ExitStatus, id string) error {
464451
select {
465452
case <-ctx.Done():
466453
if err := ctx.Err(); err != nil {
467454
return fmt.Errorf("wait container %v: %w", id, err)
468455
}
469456
return nil
470457
case status := <-exitCh:
458+
// Cleanup the IO after a successful Stop
459+
io := task.IO()
460+
if task.IO() != nil {
461+
defer func() {
462+
if cerr := io.Close(); cerr != nil {
463+
log.G(ctx).Warnf("failed to close IO for container %s: %v", id, cerr)
464+
}
465+
}()
466+
}
471467
return status.Error()
472468
}
473469
}

0 commit comments

Comments
 (0)