Skip to content

test:add ci for cleanup fifos #4443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions cmd/nerdctl/container/container_stop_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,23 @@ func TestStopWithTimeout(t *testing.T) {
// The container should get the SIGKILL before the 10s default timeout
assert.Assert(t, elapsed < 10*time.Second, "Container did not respect --timeout flag")
}
func TestStopCleanupFIFOs(t *testing.T) {
if rootlessutil.IsRootless() {
t.Skip("/run/containerd/fifo/ doesn't exist on rootless")
}
testutil.DockerIncompatible(t)
base := testutil.NewBase(t)
testContainerName := testutil.Identifier(t)
oldNumFifos, err := countFIFOFiles("/run/containerd/fifo/")
assert.NilError(t, err)
// Stop the container after 2 seconds
go func() {
time.Sleep(2 * time.Second)
base.Cmd("stop", testContainerName).AssertOK()
newNumFifos, err := countFIFOFiles("/run/containerd/fifo/")
assert.NilError(t, err)
assert.Equal(t, oldNumFifos, newNumFifos)
}()
// Start a container that is automatically removed after it exits
base.Cmd("run", "--rm", "--name", testContainerName, testutil.NginxAlpineImage).AssertOK()
}
39 changes: 16 additions & 23 deletions pkg/containerutil/containerutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,30 +397,25 @@ func Stop(ctx context.Context, container containerd.Container, timeout *time.Dur
return err
}

// signal will be sent once resume is finished
if paused {
if err := task.Resume(ctx); err != nil {
log.G(ctx).Errorf("Cannot unpause container %s: %s", container.ID(), err)
return err
}
}
if *timeout > 0 {
sig, err := getSignal(signalValue, l)
if err != nil {
return err
}

if err := task.Kill(ctx, sig); err != nil {
return err
}

// signal will be sent once resume is finished
if paused {
if err := task.Resume(ctx); err != nil {
log.G(ctx).Warnf("Cannot unpause container %s: %s", container.ID(), err)
} else {
// no need to do it again when send sigkill signal
paused = false
}
}

sigtermCtx, sigtermCtxCancel := context.WithTimeout(ctx, *timeout)
defer sigtermCtxCancel()

err = waitContainerStop(sigtermCtx, exitCh, container.ID())
err = waitContainerStop(sigtermCtx, task, exitCh, container.ID())
if err == nil {
return nil
}
Expand All @@ -434,18 +429,10 @@ func Stop(ctx context.Context, container containerd.Container, timeout *time.Dur
if err != nil {
return err
}

if err := task.Kill(ctx, sig); err != nil {
return err
}

// signal will be sent once resume is finished
if paused {
if err := task.Resume(ctx); err != nil {
log.G(ctx).Warnf("Cannot unpause container %s: %s", container.ID(), err)
}
}
return waitContainerStop(ctx, exitCh, container.ID())
return waitContainerStop(ctx, task, exitCh, container.ID())
}

func getSignal(signalValue string, containerLabels map[string]string) (syscall.Signal, error) {
Expand All @@ -460,14 +447,20 @@ func getSignal(signalValue string, containerLabels map[string]string) (syscall.S
return signal.ParseSignal("SIGTERM")
}

func waitContainerStop(ctx context.Context, exitCh <-chan containerd.ExitStatus, id string) error {
func waitContainerStop(ctx context.Context, task containerd.Task, exitCh <-chan containerd.ExitStatus, id string) error {
select {
case <-ctx.Done():
if err := ctx.Err(); err != nil {
return fmt.Errorf("wait container %v: %w", id, err)
}
return nil
case status := <-exitCh:
// Cleanup the IO after a successful Stop
if io := task.IO(); io != nil {
if cerr := io.Close(); cerr != nil {
log.G(ctx).Warnf("failed to close IO for container %s: %v", id, cerr)
}
}
return status.Error()
}
}
Expand Down
Loading