Skip to content

Commit 2689504

Browse files
committed
Add cleanup IO process after nerdctl stop
1 parent 401800a commit 2689504

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

cmd/nerdctl/container/container_stop_linux_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package container
1919
import (
2020
"fmt"
2121
"io"
22+
"os"
2223
"strings"
2324
"testing"
2425
"time"
@@ -199,3 +200,31 @@ func TestStopWithTimeout(t *testing.T) {
199200
// The container should get the SIGKILL before the 10s default timeout
200201
assert.Assert(t, elapsed < 10*time.Second, "Container did not respect --timeout flag")
201202
}
203+
204+
func TestStopCleanupFIFOs(t *testing.T) {
205+
t.Parallel()
206+
207+
base := testutil.NewBase(t)
208+
testContainerName := testutil.Identifier(t)
209+
210+
// Stop the container after 2 seconds
211+
go func() {
212+
time.Sleep(2 * time.Second)
213+
base.Cmd("stop", testContainerName).Run()
214+
}()
215+
216+
// Start a container that is automatically removed after it exits
217+
base.Cmd("run", "--rm", "--name", testContainerName, testutil.NginxAlpineImage).AssertOK()
218+
219+
fifoDir := "/run/containerd/fifo"
220+
entries, err := os.ReadDir(fifoDir)
221+
assert.NilError(t, err, "failed to read fifo directory")
222+
223+
if len(entries) != 0 {
224+
for _, entry := range entries {
225+
t.Logf("Leaked FIFO file: %s", entry.Name())
226+
}
227+
}
228+
// The FIFO directory should be empty after the container be stopped
229+
assert.Assert(t, len(entries) == 0, "Expected no FIFO files under %s, but found %d", fifoDir, len(entries))
230+
}

pkg/containerutil/containerutil.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,16 @@ func Stop(ctx context.Context, container containerd.Container, timeout *time.Dur
385385
return err
386386
}
387387

388+
// Cleanup the IO after a successful Stop
389+
io := task.IO()
390+
if io != nil {
391+
defer func() {
392+
if cerr := io.Close(); cerr != nil {
393+
log.G(ctx).Warnf("failed to close IO for container %s: %v", container.ID(), cerr)
394+
}
395+
}()
396+
}
397+
388398
status, err := task.Status(ctx)
389399
if err != nil {
390400
return err

0 commit comments

Comments
 (0)