Skip to content

Commit

Permalink
fix data race of stdout
Browse files Browse the repository at this point in the history
Signed-off-by: Swagat Bora <[email protected]>
  • Loading branch information
swagatbora90 committed Oct 18, 2024
1 parent a12e0c5 commit 19823b8
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions runtime/service_integ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -926,10 +926,27 @@ vdf 254:80 0 512B 0 | 214 244 216 245 215 177 177 177`
}

func startAndWaitTask(ctx context.Context, t testing.TB, c containerd.Container) string {
var stdout bytes.Buffer
var stderr bytes.Buffer
stdoutR, stdoutW := io.Pipe()
stderrR, stderrW := io.Pipe()

var stdoutBuf, stderrBuf bytes.Buffer
var stdoutErr, stderrErr error
var wg sync.WaitGroup

wg.Add(2)
go func() {
defer wg.Done()
_, stdoutErr = io.Copy(&stdoutBuf, stdoutR)
stdoutR.Close()
}()

task, err := c.NewTask(ctx, cio.NewCreator(cio.WithStreams(nil, &stdout, &stderr)))
go func() {
defer wg.Done()
_, stderrErr = io.Copy(&stderrBuf, stderrR)
stderrR.Close()
}()

task, err := c.NewTask(ctx, cio.NewCreator(cio.WithStreams(nil, stdoutW, stderrW)))
require.NoError(t, err, "failed to create task for container %s", c.ID())

exitCh, err := task.Wait(ctx)
Expand All @@ -946,8 +963,12 @@ func startAndWaitTask(ctx context.Context, t testing.TB, c containerd.Container)
assert.NoError(t, exitStatus.Error(), "failed to retrieve exitStatus")
assert.Equal(t, uint32(0), exitStatus.ExitCode())

// Print stderr to help with debugging
stderrOutput := stderr.String()
wg.Wait()

require.NoError(t, stdoutErr, "error copying stdout")
require.NoError(t, stderrErr, "error copying stderr")

stderrOutput := stderrBuf.String()
if len(stderrOutput) != 0 {
fmt.Printf("stderr output from container %s: %s", c.ID(), stderrOutput)
}
Expand All @@ -962,7 +983,7 @@ func startAndWaitTask(ctx context.Context, t testing.TB, c containerd.Container)
"context cancelled while waiting for container %s to exit, err: %v", c.ID(), ctx.Err())
}

return stdout.String()
return stdoutBuf.String()
}

func testCreateContainerWithSameName(t *testing.T, vmID string) {
Expand Down

0 comments on commit 19823b8

Please sign in to comment.