Skip to content

Commit 19823b8

Browse files
committed
fix data race of stdout
Signed-off-by: Swagat Bora <[email protected]>
1 parent a12e0c5 commit 19823b8

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

runtime/service_integ_test.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -926,10 +926,27 @@ vdf 254:80 0 512B 0 | 214 244 216 245 215 177 177 177`
926926
}
927927

928928
func startAndWaitTask(ctx context.Context, t testing.TB, c containerd.Container) string {
929-
var stdout bytes.Buffer
930-
var stderr bytes.Buffer
929+
stdoutR, stdoutW := io.Pipe()
930+
stderrR, stderrW := io.Pipe()
931+
932+
var stdoutBuf, stderrBuf bytes.Buffer
933+
var stdoutErr, stderrErr error
934+
var wg sync.WaitGroup
935+
936+
wg.Add(2)
937+
go func() {
938+
defer wg.Done()
939+
_, stdoutErr = io.Copy(&stdoutBuf, stdoutR)
940+
stdoutR.Close()
941+
}()
931942

932-
task, err := c.NewTask(ctx, cio.NewCreator(cio.WithStreams(nil, &stdout, &stderr)))
943+
go func() {
944+
defer wg.Done()
945+
_, stderrErr = io.Copy(&stderrBuf, stderrR)
946+
stderrR.Close()
947+
}()
948+
949+
task, err := c.NewTask(ctx, cio.NewCreator(cio.WithStreams(nil, stdoutW, stderrW)))
933950
require.NoError(t, err, "failed to create task for container %s", c.ID())
934951

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

949-
// Print stderr to help with debugging
950-
stderrOutput := stderr.String()
966+
wg.Wait()
967+
968+
require.NoError(t, stdoutErr, "error copying stdout")
969+
require.NoError(t, stderrErr, "error copying stderr")
970+
971+
stderrOutput := stderrBuf.String()
951972
if len(stderrOutput) != 0 {
952973
fmt.Printf("stderr output from container %s: %s", c.ID(), stderrOutput)
953974
}
@@ -962,7 +983,7 @@ func startAndWaitTask(ctx context.Context, t testing.TB, c containerd.Container)
962983
"context cancelled while waiting for container %s to exit, err: %v", c.ID(), ctx.Err())
963984
}
964985

965-
return stdout.String()
986+
return stdoutBuf.String()
966987
}
967988

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

0 commit comments

Comments
 (0)