Skip to content

Commit 0e37003

Browse files
committed
chore: fix e2e race condition
1 parent 87d36ab commit 0e37003

1 file changed

Lines changed: 26 additions & 21 deletions

File tree

e2e/echo.go

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,10 @@ func runEchoAgent(scriptPath string) {
8989
if entry.ThinkDurationMS > 0 {
9090
redrawTerminal(messages, true)
9191
spinnerCtx, spinnerCancel := context.WithCancel(ctx)
92-
go runSpinner(spinnerCtx)
92+
spinnerDone := runSpinner(spinnerCtx)
9393
time.Sleep(time.Duration(entry.ThinkDurationMS) * time.Millisecond)
94-
if spinnerCancel != nil {
95-
spinnerCancel()
96-
}
94+
spinnerCancel()
95+
<-spinnerDone
9796
}
9897

9998
messages = append(messages, st.ConversationMessage{
@@ -133,9 +132,10 @@ func runEchoAgent(scriptPath string) {
133132
if entry.ThinkDurationMS > 0 {
134133
redrawTerminal(messages, true)
135134
spinnerCtx, spinnerCancel := context.WithCancel(ctx)
136-
go runSpinner(spinnerCtx)
135+
spinnerDone := runSpinner(spinnerCtx)
137136
time.Sleep(time.Duration(entry.ThinkDurationMS) * time.Millisecond)
138137
spinnerCancel()
138+
<-spinnerDone
139139
}
140140

141141
messages = append(messages, st.ConversationMessage{
@@ -190,21 +190,26 @@ func cleanTerminalInput(input string) string {
190190
return strings.TrimSpace(input)
191191
}
192192

193-
func runSpinner(ctx context.Context) {
194-
spinnerChars := []string{"|", "/", "-", "\\"}
195-
ticker := time.NewTicker(200 * time.Millisecond)
196-
defer ticker.Stop()
197-
i := 0
198-
199-
for {
200-
select {
201-
case <-ticker.C:
202-
fmt.Printf("\rThinking %s", spinnerChars[i%len(spinnerChars)])
203-
i++
204-
case <-ctx.Done():
205-
// Clear spinner on cancellation
206-
fmt.Print("\r" + strings.Repeat(" ", 20) + "\r")
207-
return
193+
func runSpinner(ctx context.Context) <-chan struct{} {
194+
done := make(chan struct{})
195+
go func() {
196+
defer close(done)
197+
spinnerChars := []string{"|", "/", "-", "\\"}
198+
ticker := time.NewTicker(200 * time.Millisecond)
199+
defer ticker.Stop()
200+
i := 0
201+
202+
for {
203+
select {
204+
case <-ticker.C:
205+
fmt.Printf("\rThinking %s", spinnerChars[i%len(spinnerChars)])
206+
i++
207+
case <-ctx.Done():
208+
// Clear spinner on cancellation
209+
fmt.Print("\r" + strings.Repeat(" ", 20) + "\r")
210+
return
211+
}
208212
}
209-
}
213+
}()
214+
return done
210215
}

0 commit comments

Comments
 (0)