Skip to content

Commit 87c6d21

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

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

cmd/nerdctl/container/container_stop_linux_test.go

Lines changed: 27 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,29 @@ 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+
go func() {
211+
time.Sleep(2 * time.Second)
212+
base.Cmd("stop", testContainerName).Run()
213+
}()
214+
215+
base.Cmd("run", "--rm", "--name", testContainerName, testutil.NginxAlpineImage).AssertOK()
216+
217+
fifoDir := "/run/containerd/fifo"
218+
219+
entries, err := os.ReadDir(fifoDir)
220+
assert.NilError(t, err, "failed to read fifo directory")
221+
222+
if len(entries) != 0 {
223+
for _, entry := range entries {
224+
t.Logf("Leaked FIFO file: %s", entry.Name())
225+
}
226+
}
227+
assert.Assert(t, len(entries) == 0, "Expected no FIFO files under %s, but found %d", fifoDir, len(entries))
228+
}

pkg/containerutil/containerutil.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,15 @@ func Stop(ctx context.Context, container containerd.Container, timeout *time.Dur
384384
}
385385
return err
386386
}
387+
// Cleanup the IO after a successful Stop
388+
io := task.IO()
389+
if io != nil {
390+
defer func() {
391+
if cerr := io.Close(); cerr != nil {
392+
log.G(ctx).Warnf("failed to close IO for container %s: %v", container.ID(), cerr)
393+
}
394+
}()
395+
}
387396

388397
status, err := task.Status(ctx)
389398
if err != nil {

0 commit comments

Comments
 (0)