Skip to content

Commit e5455b2

Browse files
authored
Merge pull request #4443 from ningmingxiao/add_ci
test:add ci for cleanup fifos
2 parents 276a704 + 23a73bb commit e5455b2

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
@@ -383,6 +383,12 @@ func Stop(ctx context.Context, container containerd.Container, timeout *time.Dur
383383

384384
switch status.Status {
385385
case containerd.Created, containerd.Stopped:
386+
// Cleanup the IO after a successful Stop
387+
if io := task.IO(); io != nil {
388+
if cerr := io.Close(); cerr != nil {
389+
log.G(ctx).Warnf("failed to close IO for container %s: %v", container.ID(), cerr)
390+
}
391+
}
386392
return nil
387393
case containerd.Paused, containerd.Pausing:
388394
paused = true
@@ -395,6 +401,13 @@ func Stop(ctx context.Context, container containerd.Container, timeout *time.Dur
395401
return err
396402
}
397403

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

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

421-
err = waitContainerStop(sigtermCtx, exitCh, container.ID())
424+
err = waitContainerStop(sigtermCtx, task, exitCh, container.ID())
422425
if err == nil {
423426
return nil
424427
}
@@ -437,13 +440,7 @@ func Stop(ctx context.Context, container containerd.Container, timeout *time.Dur
437440
return err
438441
}
439442

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

449446
func getSignal(signalValue string, containerLabels map[string]string) (syscall.Signal, error) {
@@ -458,14 +455,20 @@ func getSignal(signalValue string, containerLabels map[string]string) (syscall.S
458455
return signal.ParseSignal("SIGTERM")
459456
}
460457

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

0 commit comments

Comments
 (0)