Skip to content
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

ci: fix a race in TestExecIn and TestExecInTTY #4445

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
65 changes: 50 additions & 15 deletions libcontainer/integration/execin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,37 @@ func TestExecIn(t *testing.T) {
// Execute a first process in the container
stdinR, stdinW, err := os.Pipe()
ok(t, err)
defer func() {
_ = stdinR.Close()
_ = stdinW.Close()
}()
stdoutR, stdoutW, err := os.Pipe()
ok(t, err)
defer func() {
_ = stdoutR.Close()
_ = stdoutW.Close()
Comment on lines +40 to +41
Copy link
Member

Choose a reason for hiding this comment

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

can't we just defer the close, instead of a function? We are ignoring the error anyways.

I mean it for all the cases, not just this.

}()

ch := waitStdOut(stdoutR)
process := &libcontainer.Process{
Cwd: "/",
Args: []string{"cat"},
Env: standardEnvironment,
Stdin: stdinR,
Init: true,
Cwd: "/",
Args: []string{"cat", "/proc/self/cmdline", "-"},
Env: standardEnvironment,
Stdin: stdinR,
Stdout: stdoutW,
Init: true,
}
err = container.Run(process)
_ = stdinR.Close()
defer stdinW.Close() //nolint: errcheck
defer func() {
_, _ = stdinW.Write([]byte("hello"))
Copy link
Member

Choose a reason for hiding this comment

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

why do we write this? To stop the go routine? Can you add a comment or the message written can be self-explanatory instead of this hello?

If it's that, is it needed? Won't the close of the pipe already free the goroutine?

_ = stdinW.Close()
if _, err := process.Wait(); err != nil {
t.Log(err)
}
}()
ok(t, err)

err = <-ch
ok(t, err)

buffers := newStdBuffers()
Expand All @@ -55,8 +76,6 @@ func TestExecIn(t *testing.T) {
err = container.Run(ps)
ok(t, err)
waitProcess(ps, t)
_ = stdinW.Close()
waitProcess(process, t)

out := buffers.Stdout.String()
if !strings.Contains(out, "cat") || !strings.Contains(out, "ps") {
Expand Down Expand Up @@ -242,23 +261,39 @@ func TestExecInTTY(t *testing.T) {
// Execute a first process in the container
stdinR, stdinW, err := os.Pipe()
ok(t, err)
defer func() {
_ = stdinR.Close()
_ = stdinW.Close()
}()
stdoutR, stdoutW, err := os.Pipe()
ok(t, err)
defer func() {
_ = stdoutR.Close()
_ = stdoutW.Close()
}()

ch := waitStdOut(stdoutR)
process := &libcontainer.Process{
Cwd: "/",
Args: []string{"cat"},
Env: standardEnvironment,
Stdin: stdinR,
Init: true,
Cwd: "/",
Args: []string{"cat", "/proc/self/cmdline", "-"},
Env: standardEnvironment,
Stdin: stdinR,
Stdout: stdoutW,
Init: true,
}
err = container.Run(process)
_ = stdinR.Close()
defer func() {
_, _ = stdinW.Write([]byte("hello"))
_ = stdinW.Close()
if _, err := process.Wait(); err != nil {
t.Log(err)
}
}()
ok(t, err)

err = <-ch
ok(t, err)

ps := &libcontainer.Process{
Cwd: "/",
Args: []string{"ps"},
Expand Down
22 changes: 22 additions & 0 deletions libcontainer/integration/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,25 @@ func runContainerOk(t *testing.T, config *configs.Config, args ...string) *stdBu
func destroyContainer(container *libcontainer.Container) {
_ = container.Destroy()
}

func waitStdOut(stdout *os.File) chan error {
ch := make(chan error, 1)
buf := make([]byte, 1)
go func() {
defer close(ch)

for {
n, err := stdout.Read(buf)
if err != nil {
ch <- err
return
}

if n >= 0 {
ch <- nil
return
}
}
}()
return ch
}
Loading