test(server): make TestAttachedServer_DeleteSessionStopsEventStream more robust#2845
Merged
Merged
Conversation
docker-agent
left a comment
There was a problem hiding this comment.
Assessment: 🟢 APPROVE
The fix correctly addresses the race condition in TestAttachedServer_DeleteSessionStopsEventStream. By introducing a sourceStarted synchronization channel, the test now ensures the SSE handler has actually invoked the event source before DeleteSession is called — eliminating the window where deletion could win the race and leave the source context never cancelled.
The 2 s → 10 s timeout bumps are also appropriate for slow CI runners.
No bugs were found in the changed code. The drafter noted two low-severity observations (neither constitutes a real bug):
close(sourceStarted)would panic on a double-invocation of the source closure, but the single-invocation contract ofStreamEventsmakes this a non-issue in practice.time.Afterin theselectcreates a timer that is not explicitly stopped, which is a minor resource concern only relevant in very long-running test suites.
Both are informational only and do not warrant changes.
b52448b to
b270f9b
Compare
trungutt
approved these changes
May 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
TestAttachedServer_DeleteSessionStopsEventStreamwas occasionally failing on CI with:(timing out at exactly 2.00s on the
selectatserver_attached_test.go:148.)Root cause
The test was racing against the SSE handler:
GET /api/sessions/:id/events.http.Client.Doreturns as soon as the server flushes the SSE response headers inServer.sessionEvents— before that handler reachessm.StreamEvents→eventSources.Load(...).sm.DeleteSession, which removes the entry fromeventSources.DeleteSessionwon the race,StreamEventsreturnedfalsewithout ever invoking the registered source closure, so its<-ctx.Done(); close(sourceCtxDone)body never ran and the 2 s timeout fired.Fix
pkg/server/server_attached_test.go:sourceStartedchannel that is closed at the top of the registeredEventSourceclosure.<-sourceStartedbefore callingsm.DeleteSession, guaranteeing the SSE handler has actually invoked the source.Validation
go test -race -run TestAttachedServer_DeleteSessionStopsEventStream -count=50 ./pkg/server/...— passgo test -race -run TestAttachedServer_DeleteSessionStopsEventStream -count=200 ./pkg/server/...— passgo test -race -count=1 ./pkg/server/...— passtask lint— 0 issues