-
Notifications
You must be signed in to change notification settings - Fork 4.7k
WebSocket: release pending-activity ref when close()/terminate() called during CONNECTING #29918
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Jarred-Sumner
merged 6 commits into
main
from
farm/54e22d98/websocket-connecting-close-leak
Apr 29, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c2e8098
WebSocket: release pending-activity ref when close()/terminate() call…
robobun e3ff74b
Merge origin/main
robobun 3ec2a84
Dispatch error event before close; drop explicit test timeout
robobun 1e44931
Move close-during-CONNECTING tests to their own file
robobun 81bd2a9
[autofix.ci] apply automated fixes
autofix-ci[bot] 8a5dc8c
Merge branch 'main' into farm/54e22d98/websocket-connecting-close-leak
Jarred-Sumner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| // close()/terminate() during CONNECTING must transition to CLOSED, fire | ||
| // error + close events, and release the pending-activity ref so the | ||
| // WebSocket can be garbage-collected. Previously the Zig upgrade client | ||
| // was cancelled without any completion callback reaching C++, leaving | ||
| // the socket stuck in CLOSING with hasPendingActivity() permanently true | ||
| // — never GC'd, close event never fired, process never exited. | ||
|
|
||
| import { heapStats } from "bun:jsc"; | ||
| import { describe, expect, it } from "bun:test"; | ||
| import { createServer, type AddressInfo } from "net"; | ||
|
|
||
| describe.each(["close", "terminate"] as const)("%s() during CONNECTING", method => { | ||
| it("fires error + close events and transitions to CLOSED", async () => { | ||
| // TCP server that accepts connections but never responds — keeps the | ||
| // WebSocket in CONNECTING until we abort it. | ||
| const listening = Promise.withResolvers<void>(); | ||
| const server = createServer(() => {}).listen(0, "127.0.0.1", listening.resolve); | ||
| await listening.promise; | ||
| try { | ||
| const port = (server.address() as AddressInfo).port; | ||
| const ws = new WebSocket(`ws://127.0.0.1:${port}`); | ||
| expect(ws.readyState).toBe(WebSocket.CONNECTING); | ||
|
|
||
| const events: { type: string; message?: string }[] = []; | ||
| ws.onerror = e => events.push({ type: e.type, message: (e as ErrorEvent).message }); | ||
| const closed = new Promise<CloseEvent>(resolve => { | ||
| ws.onclose = e => { | ||
| events.push({ type: e.type }); | ||
| resolve(e); | ||
| }; | ||
| }); | ||
|
|
||
| ws[method](); | ||
| expect(ws.readyState).toBe(WebSocket.CLOSING); | ||
|
|
||
| const ev = await closed; | ||
| // Spec "fail the WebSocket connection": error event then close event. | ||
| expect(events.map(e => e.type)).toEqual(["error", "close"]); | ||
| expect(events[0].message).toContain("closed before the connection is established"); | ||
| expect({ | ||
| readyState: ws.readyState, | ||
| code: ev.code, | ||
| wasClean: ev.wasClean, | ||
| }).toEqual({ | ||
| readyState: WebSocket.CLOSED, | ||
| code: 1006, | ||
| wasClean: false, | ||
| }); | ||
| } finally { | ||
| server.close(); | ||
| } | ||
| }); | ||
|
|
||
| it("does not leak", async () => { | ||
| function getWebSocketCount() { | ||
| Bun.gc(true); | ||
| return heapStats().objectTypeCounts?.WebSocket || 0; | ||
| } | ||
|
|
||
| const listening = Promise.withResolvers<void>(); | ||
| const server = createServer(() => {}).listen(0, "127.0.0.1", listening.resolve); | ||
| await listening.promise; | ||
| try { | ||
| const port = (server.address() as AddressInfo).port; | ||
| const before = getWebSocketCount(); | ||
|
|
||
| const closes: Promise<unknown>[] = []; | ||
| for (let i = 0; i < 64; i++) { | ||
| const ws = new WebSocket(`ws://127.0.0.1:${port}`); | ||
| closes.push(new Promise(resolve => (ws.onclose = resolve))); | ||
| ws[method](); | ||
| } | ||
| await Promise.all(closes); | ||
|
|
||
| // disablePendingActivity is posted as a separate task after the close | ||
| // event fires; drain those plus any pending socket-close callbacks | ||
| // before counting. Without the fix, all 64 instances remain alive. | ||
| let after: number; | ||
| for (let i = 0; i < 10; i++) { | ||
| await new Promise(resolve => setImmediate(resolve)); | ||
| after = getWebSocketCount(); | ||
| if (after - before <= 5) break; | ||
| } | ||
|
|
||
| expect(after! - before).toBeLessThanOrEqual(5); | ||
| } finally { | ||
| server.close(); | ||
| } | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.