Skip to content

fix(test): Stop TestServerHandlesLoadsOfPendingTasks deadlocking - #76

Merged
keelerm84 merged 1 commit into
mainfrom
mk/SDK-3120/pending-tasks-deadlock
Sep 16, 2026
Merged

keelerm84 merged 1 commit into
mainfrom
mk/SDK-3120/pending-tasks-deadlock

Conversation

@keelerm84

@keelerm84 keelerm84 commented Sep 16, 2026 •

Copy link
Copy Markdown
Member

Summary

TestServerHandlesLoadsOfPendingTasks deadlocks intermittently and fails the build with panic: test timed out. On main at 90772ec it reproduced at iteration 6 of 20 running the test alone, and at iteration 4 of 20 running the full suite.

The test opened a subscriber with http.Get, discarded the response, and never closed the body or the Server. The SSE handler kept that connection open indefinitely, so the deferred httptest.Server.Close() blocked in its wait-for-outstanding-requests loop. The goroutine dump puts the test at server_test.go:76 inside net/http/httptest.(*Server).Close. Whether it finished depended on whether the runtime had released the connection by then, which is what made it intermittent.

Closing the subscriber cancels the request context, so the handler returns and httptest can finish. TestServerHandlerReceivesPublishedEvents right below it already does exactly this.

The sync.WaitGroup goes away with it. http.Get returns once the handler flushes the SSE response headers, so nothing was needed to hold the connection open across the publish loop -- the WaitGroup only delayed the goroutine's exit, and the goroutine was not doing anything after the Get.

Defer order is load-bearing and called out in a comment: the subscriber disconnects, then the Server stops, then httptest waits for the handler. Each step is what lets the next one finish.

Verification

  • The fixed test: 60 consecutive runs, no failures.
  • The full suite: 12 consecutive runs, no failures, plus -race.
  • Before the fix, the same loops failed within 20 runs.

Origin

Found while verifying #75. That change touches only the client's error-response path, and this flake reproduces on main without it, so the two are independent.


Note

Overview
Fixes an intermittent test timeout in TestServerHandlesLoadsOfPendingTasks by tearing down the SSE subscriber and server in a defined order instead of leaving an open http.Get connection.

The test now keeps the subscriber connected during the 1000 PublishComment calls (response body held open until defer resp.Body.Close()), adds defer server.Close(), and drops the sync.WaitGroup + background goroutine pattern that did not actually coordinate shutdown. Comments document why defer order matters: close the body first so the handler exits, then stop the Server, then let httptest finish waiting.

Reviewed by Cursor Bugbot for commit bc26350. Bugbot is set up for automated code reviews on this repo. Configure here.

The test opened a subscriber, discarded the response, and never closed
the body or the Server. The SSE handler kept that connection open, so
the deferred httptest.Server.Close blocked in its wait for outstanding
requests. Whether it finished depended on whether the runtime had
released the connection by then, so the build failed about once in five
runs with a test timeout.

The subscriber now closes, which cancels the request context and lets
the handler return. This is what the neighbouring test already does.

The WaitGroup is gone with it. http.Get returns once the handler flushes
the SSE response headers, so nothing was needed to hold the connection
open while the comments published.
@keelerm84
keelerm84 marked this pull request as ready for review September 16, 2026 16:56
@keelerm84
keelerm84 requested a review from a team as a code owner September 16, 2026 16:56
@keelerm84
keelerm84 merged commit d51d9b0 into main Sep 16, 2026
10 checks passed
@keelerm84
keelerm84 deleted the mk/SDK-3120/pending-tasks-deadlock branch September 16, 2026 19:29
keelerm84 pushed a commit that referenced this pull request Oct 7, 2026
🤖 I have created a release *beep* *boop*
---


##
[1.14.1](v1.14.0...v1.14.1)
(2026-10-07)


### Bug Fixes

* Keep the write error in the chain of Encoder errors
([#81](#81))
([49bd77d](49bd77d))
* **test:** Stop TestServerHandlesLoadsOfPendingTasks deadlocking
([#76](#76))
([d51d9b0](d51d9b0))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants