Skip to content

Commit bb5af87

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

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

runtime/service_integ_test.go

Lines changed: 30 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,15 @@ 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+
stdoutW.Close()
967+
stderrW.Close()
968+
969+
wg.Wait()
970+
971+
require.NoError(t, stdoutErr, "error copying stdout")
972+
require.NoError(t, stderrErr, "error copying stderr")
973+
974+
stderrOutput := stderrBuf.String()
951975
if len(stderrOutput) != 0 {
952976
fmt.Printf("stderr output from container %s: %s", c.ID(), stderrOutput)
953977
}
@@ -962,7 +986,7 @@ func startAndWaitTask(ctx context.Context, t testing.TB, c containerd.Container)
962986
"context cancelled while waiting for container %s to exit, err: %v", c.ID(), ctx.Err())
963987
}
964988

965-
return stdout.String()
989+
return stdoutBuf.String()
966990
}
967991

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

0 commit comments

Comments
 (0)