Skip to content

Add cleanup IO process after nerdctl stop #4316

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
29 changes: 29 additions & 0 deletions cmd/nerdctl/container/container_stop_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package container
import (
"fmt"
"io"
"os"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -199,3 +200,31 @@ 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) {
t.Parallel()

base := testutil.NewBase(t)
testContainerName := testutil.Identifier(t)

// Stop the container after 2 seconds
go func() {
time.Sleep(2 * time.Second)
base.Cmd("stop", testContainerName).Run()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sequentially stopping the container makes things more deterministic. AssertOk here is necessary to ensure proper stop

}()

// Start a container that is automatically removed after it exits
base.Cmd("run", "--rm", "--name", testContainerName, testutil.NginxAlpineImage).AssertOK()

fifoDir := "/run/containerd/fifo"
entries, err := os.ReadDir(fifoDir)
assert.NilError(t, err, "failed to read fifo directory")

if len(entries) != 0 {
for _, entry := range entries {
t.Logf("Leaked FIFO file: %s", entry.Name())
}
}
// The FIFO directory should be empty after the container be stopped
assert.Assert(t, len(entries) == 0, "Expected no FIFO files under %s, but found %d", fifoDir, len(entries))
}
10 changes: 10 additions & 0 deletions pkg/containerutil/containerutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,16 @@ func Stop(ctx context.Context, container containerd.Container, timeout *time.Dur
return err
}

// Cleanup the IO after a successful Stop
io := task.IO()
if io != nil {
defer func() {
if cerr := io.Close(); cerr != nil {
log.G(ctx).Warnf("failed to close IO for container %s: %v", container.ID(), cerr)
}
}()
}

status, err := task.Status(ctx)
if err != nil {
return err
Expand Down
Loading