Skip to content
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
20 changes: 19 additions & 1 deletion internal/core/bash_streaming_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"os/exec"
"strings"
"sync"
"testing"
"time"

Expand All @@ -28,12 +29,29 @@ func runStreaming(t *testing.T, command string) (fantasy.ToolResponse, []string)
go func() {
ctx := context.Background()
cmd := exec.CommandContext(ctx, "bash", "-c", command)

// executeBashStreaming drains stdout and stderr in two separate
// goroutines, so the callback is invoked concurrently. The production
// code guards its own chunk slices with a mutex; the callback we pass
// must do the same or the append races.
var mu sync.Mutex
var chunks []string
cb := func(_, _, chunk string, _ bool) {
mu.Lock()
chunks = append(chunks, chunk)
mu.Unlock()
}

resp, err := executeBashStreaming(ctx, bashCall(command, 0), cmd, cb, "")
done <- result{resp, chunks, err}

// executeBashStreaming has joined both stream goroutines by the time it
// returns, so no further callback can fire. Take the lock anyway to
// publish the final slice under the same mutex that guarded the writes.
mu.Lock()
snapshot := append([]string(nil), chunks...)
mu.Unlock()

done <- result{resp, snapshot, err}
}()

select {
Expand Down
Loading