-
Notifications
You must be signed in to change notification settings - Fork 589
Fire close event for server WebSocket close, with allowHalfOpen opt-out. #6197
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
Merged
Changes from all commits
Commits
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
152 changes: 152 additions & 0 deletions
152
src/workerd/api/tests/websocket-allow-half-open-test.js
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,152 @@ | ||
| // Copyright (c) 2026 Cloudflare, Inc. | ||
| // Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
| // https://opensource.org/licenses/Apache-2.0 | ||
|
|
||
| import { strictEqual, doesNotThrow } from 'node:assert'; | ||
|
|
||
| // Test that when allowHalfOpen is false (default with compat flag), a server-initiated | ||
| // close sets readyState to CLOSED. | ||
| export const autoCloseReplyWhenNotHalfOpen = { | ||
| async test() { | ||
| const pair = new WebSocketPair(); | ||
| const [client, server] = Object.values(pair); | ||
|
|
||
| // accept() without options — allowHalfOpen defaults to false with the compat flag. | ||
| client.accept(); | ||
| server.accept(); | ||
|
|
||
| const closePromise = new Promise((resolve) => { | ||
| client.addEventListener('close', (event) => { | ||
| // When allowHalfOpen is false, the runtime auto-sends a close reply, | ||
| // so both closedIncoming and closedOutgoing are true — readyState should be CLOSED. | ||
| resolve({ | ||
| readyState: client.readyState, | ||
| code: event.code, | ||
| wasClean: event.wasClean, | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| // Server initiates close. | ||
| server.close(1000, 'server closing'); | ||
|
|
||
| const result = await closePromise; | ||
| strictEqual(result.readyState, WebSocket.CLOSED); | ||
| strictEqual(result.code, 1000); | ||
| strictEqual(result.wasClean, true); | ||
| }, | ||
| }; | ||
|
|
||
| // Test that calling close() inside the close event handler after the automatic close | ||
| // handshake is silently ignored (doesn't throw). This is the realistic pattern for | ||
| // users who are already manually replying to close frames today — when they update | ||
| // their compat date and get web_socket_auto_reply_to_close, their existing close() | ||
| // call must not break. | ||
| export const closeInsideHandlerAfterAutoCloseIsIgnored = { | ||
| async test() { | ||
| const pair = new WebSocketPair(); | ||
| const [client, server] = Object.values(pair); | ||
|
|
||
| client.accept(); | ||
| server.accept(); | ||
|
|
||
| const closePromise = new Promise((resolve) => { | ||
| client.addEventListener('close', (event) => { | ||
| // This is what existing user code typically looks like: replying to close | ||
| // inside the close handler. With the auto-reply already sent, closedOutgoing | ||
| // is true but the native state is still Accepted (tryReleaseNative hasn't | ||
| // run yet), so this exercises the closedOutgoing early-return in close(). | ||
| doesNotThrow(() => { | ||
| client.close(1000, 'manual reply inside handler'); | ||
| }); | ||
|
|
||
| resolve({ | ||
| readyState: client.readyState, | ||
| code: event.code, | ||
| wasClean: event.wasClean, | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| // Server initiates close; the runtime auto-replies because allowHalfOpen is false. | ||
| server.close(1000, 'server closing'); | ||
|
|
||
| const result = await closePromise; | ||
| strictEqual(result.readyState, WebSocket.CLOSED); | ||
| strictEqual(result.code, 1000); | ||
| strictEqual(result.wasClean, true); | ||
| }, | ||
| }; | ||
|
|
||
| // Same as above, but calling close() after the handler has returned and the native | ||
| // WebSocket has been released. This exercises the state.is<Released>() early-return | ||
| // in close(), which is a different code path from the closedOutgoing check above. | ||
| export const closeAfterAutoCloseAndReleaseIsIgnored = { | ||
| async test() { | ||
| const pair = new WebSocketPair(); | ||
| const [client, server] = Object.values(pair); | ||
|
|
||
| client.accept(); | ||
| server.accept(); | ||
|
|
||
| const closePromise = new Promise((resolve) => { | ||
| client.addEventListener('close', (event) => { | ||
| resolve({ | ||
| readyState: client.readyState, | ||
| code: event.code, | ||
| wasClean: event.wasClean, | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| // Server initiates close; the runtime auto-replies because allowHalfOpen is false. | ||
| server.close(1000, 'server closing'); | ||
|
|
||
| const result = await closePromise; | ||
| strictEqual(result.readyState, WebSocket.CLOSED); | ||
|
|
||
| // By now tryReleaseNative has run and the state is Released. | ||
| // close() should still be silently ignored. | ||
| doesNotThrow(() => { | ||
| client.close(1000, 'manual reply after release'); | ||
| }); | ||
|
|
||
| strictEqual(client.readyState, WebSocket.CLOSED); | ||
| }, | ||
| }; | ||
|
|
||
| // Test that when allowHalfOpen is true via accept(), a server-initiated close sets | ||
| // readyState to CLOSING. | ||
| export const halfOpenCloseKeepsClosingState = { | ||
| async test() { | ||
| const pair = new WebSocketPair(); | ||
| const [client, server] = Object.values(pair); | ||
|
|
||
| // Opt into half-open mode via accept() options. | ||
| client.accept({ allowHalfOpen: true }); | ||
| server.accept(); | ||
|
|
||
| const closePromise = new Promise((resolve) => { | ||
| client.addEventListener('close', (event) => { | ||
| // When allowHalfOpen is true, no auto-reply is sent, so only closedIncoming | ||
| // is true — readyState should be CLOSING. | ||
| resolve({ | ||
| readyState: client.readyState, | ||
| code: event.code, | ||
| wasClean: event.wasClean, | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| // Server initiates close. | ||
| server.close(1000, 'server closing'); | ||
|
|
||
| const result = await closePromise; | ||
| strictEqual(result.readyState, WebSocket.CLOSING); | ||
| strictEqual(result.code, 1000); | ||
| strictEqual(result.wasClean, true); | ||
|
|
||
| // The client must manually close to complete the handshake. | ||
| client.close(1000, 'client reply'); | ||
| }, | ||
| }; | ||
14 changes: 14 additions & 0 deletions
14
src/workerd/api/tests/websocket-allow-half-open-test.wd-test
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,14 @@ | ||
| using Workerd = import "/workerd/workerd.capnp"; | ||
|
|
||
| const unitTests :Workerd.Config = ( | ||
| services = [ | ||
| ( name = "websocket-allow-half-open-test", | ||
| worker = ( | ||
| modules = [ | ||
| (name = "worker", esModule = embed "websocket-allow-half-open-test.js") | ||
| ], | ||
| compatibilityFlags = ["nodejs_compat", "web_socket_auto_reply_to_close"] | ||
| ) | ||
| ), | ||
| ], | ||
| ); |
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
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.