Serialize appendEvent sends under the subscriber mutex, wrap worker/memory goroutines with recover, lock synchronous chat SendMessage, and escape KaTeX error HTML. Co-authored-by: Cursor <cursoragent@cursor.com>
49 lines
940 B
Go
49 lines
940 B
Go
package taskrun
|
|
|
|
import (
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"chat-agent-system/internal/stream"
|
|
)
|
|
|
|
func TestSubscribeUnsubscribeRace(t *testing.T) {
|
|
m := NewManager(nil, nil)
|
|
ar := &activeRun{
|
|
record: &RunRecord{RunID: "run-race-1", Status: StatusRunning},
|
|
subs: make(map[int]chan StoredEvent),
|
|
done: make(chan struct{}),
|
|
}
|
|
m.mu.Lock()
|
|
m.runs[ar.record.RunID] = ar
|
|
m.mu.Unlock()
|
|
|
|
var wg sync.WaitGroup
|
|
for i := 0; i < 50; i++ {
|
|
wg.Add(3)
|
|
go func() {
|
|
defer wg.Done()
|
|
ch, _, err := m.Subscribe(ar.record.RunID, 0)
|
|
if err != nil {
|
|
return
|
|
}
|
|
time.Sleep(time.Millisecond)
|
|
m.Unsubscribe(ar.record.RunID, ch)
|
|
}()
|
|
go func() {
|
|
defer wg.Done()
|
|
m.appendEvent(ar, stream.StreamEvent{Type: stream.EventDelta, Delta: "x"})
|
|
}()
|
|
go func() {
|
|
defer wg.Done()
|
|
ch, _, err := m.Subscribe(ar.record.RunID, 0)
|
|
if err != nil {
|
|
return
|
|
}
|
|
m.Unsubscribe(ar.record.RunID, ch)
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
}
|