Skip to content

test: Fix data race in loadtest/reconnectingpty #5431

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

Merged
merged 1 commit into from
Dec 15, 2022
Merged
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
23 changes: 22 additions & 1 deletion loadtest/reconnectingpty/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ func (r *Runner) Run(ctx context.Context, _ string, logs io.Writer) error {
}

copyCtx, copyCancel := context.WithTimeout(ctx, time.Duration(copyTimeout))
defer copyCancel()
matched, err := copyContext(copyCtx, copyOutput, conn, r.cfg.ExpectOutput)
copyCancel()
if r.cfg.ExpectTimeout {
if err == nil {
return xerrors.Errorf("expected timeout, but the command exited successfully")
Expand All @@ -107,11 +107,27 @@ func copyContext(ctx context.Context, dst io.Writer, src io.Reader, expectOutput
copyErr = make(chan error)
matched = expectOutput == ""
)

// Guard goroutine for loop body to ensure reading `matched` is safe on
// context cancellation and that `dst` won't be written to after we
// return from this function.
processing := make(chan struct{}, 1)
processing <- struct{}{}

go func() {
defer close(processing)
defer close(copyErr)

scanner := bufio.NewScanner(src)
for scanner.Scan() {
select {
case <-processing:
default:
}
if ctx.Err() != nil {
return
}

if expectOutput != "" && strings.Contains(scanner.Text(), expectOutput) {
matched = true
}
Expand All @@ -121,6 +137,7 @@ func copyContext(ctx context.Context, dst io.Writer, src io.Reader, expectOutput
copyErr <- xerrors.Errorf("write to logs: %w", err)
return
}
processing <- struct{}{}
}
if scanner.Err() != nil {
copyErr <- xerrors.Errorf("read from reconnecting PTY: %w", scanner.Err())
Expand All @@ -130,6 +147,10 @@ func copyContext(ctx context.Context, dst io.Writer, src io.Reader, expectOutput

select {
case <-ctx.Done():
select {
case <-processing:
case <-copyErr:
}
return matched, ctx.Err()
case err := <-copyErr:
return matched, err
Expand Down