Skip to content

Commit c4a36e1

Browse files
committed
test:add ci for cleanup fifos
Signed-off-by: ningmingxiao <[email protected]>
1 parent bd89fee commit c4a36e1

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

cmd/nerdctl/container/container_stop_linux_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package container
1919
import (
2020
"fmt"
2121
"io"
22+
"runtime"
2223
"strings"
2324
"testing"
2425
"time"
@@ -199,3 +200,26 @@ 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+
func TestStopCleanupFIFOs(t *testing.T) {
204+
if rootlessutil.IsRootless() {
205+
t.Skip("/run/containerd/fifo/ doesn't exist on rootless")
206+
}
207+
if runtime.GOOS == "windows" {
208+
t.Skip("test is not compatible with windows")
209+
}
210+
testutil.DockerIncompatible(t)
211+
base := testutil.NewBase(t)
212+
testContainerName := testutil.Identifier(t)
213+
oldNumFifos, err := countFIFOFiles("/run/containerd/fifo/")
214+
assert.NilError(t, err)
215+
// Stop the container after 2 seconds
216+
go func() {
217+
time.Sleep(2 * time.Second)
218+
base.Cmd("stop", testContainerName).AssertOK()
219+
newNumFifos, err := countFIFOFiles("/run/containerd/fifo/")
220+
assert.NilError(t, err)
221+
assert.Equal(t, oldNumFifos, newNumFifos)
222+
}()
223+
// Start a container that is automatically removed after it exits
224+
base.Cmd("run", "--rm", "--name", testContainerName, testutil.NginxAlpineImage).AssertOK()
225+
}

pkg/containerutil/containerutil.go

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

379+
// Cleanup the IO after a successful Stop
380+
io := task.IO()
381+
if io != nil {
382+
defer func() {
383+
if cerr := io.Close(); cerr != nil {
384+
log.G(ctx).Warnf("failed to close IO for container %s: %v", container.ID(), cerr)
385+
}
386+
}()
387+
}
388+
379389
status, err := task.Status(ctx)
380390
if err != nil {
381391
return err

0 commit comments

Comments
 (0)