Skip to content

test(server): make TestAttachedServer_DeleteSessionStopsEventStream more robust#2845

Merged
dgageot merged 1 commit into
docker:mainfrom
dgageot:board/b5d337a483cb78b7
May 21, 2026
Merged

test(server): make TestAttachedServer_DeleteSessionStopsEventStream more robust#2845
dgageot merged 1 commit into
docker:mainfrom
dgageot:board/b5d337a483cb78b7

Conversation

@dgageot
Copy link
Copy Markdown
Member

@dgageot dgageot commented May 21, 2026

Problem

TestAttachedServer_DeleteSessionStopsEventStream was occasionally failing on CI with:

event source ctx was not cancelled when session was deleted

(timing out at exactly 2.00s on the select at server_attached_test.go:148.)

Root cause

The test was racing against the SSE handler:

  1. The test issues GET /api/sessions/:id/events.
  2. http.Client.Do returns as soon as the server flushes the SSE response headers in Server.sessionEventsbefore that handler reaches sm.StreamEventseventSources.Load(...).
  3. The test then immediately calls sm.DeleteSession, which removes the entry from eventSources.
  4. If DeleteSession won the race, StreamEvents returned false without 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:

  • Add a sourceStarted channel that is closed at the top of the registered EventSource closure.
  • Wait on <-sourceStarted before calling sm.DeleteSession, guaranteeing the SSE handler has actually invoked the source.
  • Bump the two cancellation/wait timeouts from 2 s to 10 s for slow CI runners.

Validation

  • go test -race -run TestAttachedServer_DeleteSessionStopsEventStream -count=50 ./pkg/server/... — pass
  • go test -race -run TestAttachedServer_DeleteSessionStopsEventStream -count=200 ./pkg/server/... — pass
  • go test -race -count=1 ./pkg/server/... — pass
  • task lint — 0 issues

@dgageot dgageot requested a review from a team as a code owner May 21, 2026 07:25
Copy link
Copy Markdown

@docker-agent docker-agent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 of StreamEvents makes this a non-issue in practice.
  • time.After in the select creates 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.

@dgageot dgageot force-pushed the board/b5d337a483cb78b7 branch from b52448b to b270f9b Compare May 21, 2026 07:53
@dgageot dgageot merged commit 8a0e041 into docker:main May 21, 2026
5 checks passed
@aheritier aheritier added area/testing Test infrastructure, CI/CD, test runners, evaluation area/api For features/issues/fixes related to the usage of the cagent API kind/test Test-only changes labels May 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/api For features/issues/fixes related to the usage of the cagent API area/testing Test infrastructure, CI/CD, test runners, evaluation kind/test Test-only changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants