Skip to content

Commit b74268f

Browse files
ningmingxiaok8s-infra-cherrypick-robot
authored andcommitted
bugfix:close container io when runtime create failed
Signed-off-by: ningmingxiao <[email protected]>
1 parent c787fb9 commit b74268f

2 files changed

Lines changed: 69 additions & 1 deletion

File tree

cmd/containerd-shim-runc-v2/process/init.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func New(id string, runtime *runc.Runc, stdio stdio.Stdio) *Init {
107107
}
108108

109109
// Create the process with the provided config
110-
func (p *Init) Create(ctx context.Context, r *CreateConfig) error {
110+
func (p *Init) Create(ctx context.Context, r *CreateConfig) (retError error) {
111111
var (
112112
err error
113113
socket *runc.Socket
@@ -125,6 +125,11 @@ func (p *Init) Create(ctx context.Context, r *CreateConfig) error {
125125
return fmt.Errorf("failed to create init process I/O: %w", err)
126126
}
127127
p.io = pio
128+
defer func() {
129+
if retError != nil && p.io != nil {
130+
p.io.Close()
131+
}
132+
}()
128133
}
129134
if r.Checkpoint != "" {
130135
return p.createCheckpointedState(r, pidFile)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package integration
18+
19+
import (
20+
"bytes"
21+
"fmt"
22+
"os"
23+
"os/exec"
24+
"strings"
25+
"testing"
26+
27+
"github.com/containerd/containerd/v2/integration/images"
28+
"github.com/stretchr/testify/assert"
29+
"github.com/stretchr/testify/require"
30+
)
31+
32+
func TestContainerIOLeakAfterStartFailed(t *testing.T) {
33+
if f := os.Getenv("RUNC_FLAVOR"); f != "" && f != "runc" {
34+
t.Skip("test requires runc")
35+
}
36+
t.Log("Create a sandbox")
37+
sb, sbConfig := PodSandboxConfigWithCleanup(t, "sandbox", "container-io-leak-after-start-failed")
38+
testImage := images.Get(images.BusyBox)
39+
EnsureImageExists(t, testImage)
40+
41+
t.Log("Create a container")
42+
cnConfig := ContainerConfig(
43+
"containerIOLeakTest",
44+
testImage,
45+
WithCommand("something-that-doesnt-exist"),
46+
)
47+
t.Log("Create the container")
48+
cn, err := runtimeService.CreateContainer(sb, cnConfig, sbConfig)
49+
require.NoError(t, err)
50+
pid := getShimPid(t, sb)
51+
require.Error(t, runtimeService.StartContainer(cn))
52+
assert.Equal(t, 0, numPipe(pid))
53+
}
54+
55+
func numPipe(shimPid int) int {
56+
cmd := exec.Command("sh", "-c", fmt.Sprintf("lsof -p %d | grep pipe", shimPid))
57+
var stdout bytes.Buffer
58+
cmd.Stdout = &stdout
59+
if err := cmd.Run(); err != nil {
60+
return 0
61+
}
62+
return strings.Count(stdout.String(), "\n")
63+
}

0 commit comments

Comments
 (0)