-
Notifications
You must be signed in to change notification settings - Fork 529
Add regression test for WebSocket close propagation through containers #6057
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
Open
ghostwriternr
wants to merge
2
commits into
cloudflare:main
Choose a base branch
from
ghostwriternr:repro/ws-close-eyeball
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+287
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
97 changes: 97 additions & 0 deletions
97
src/workerd/server/tests/container-client/websocket-close-propagation-client.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,97 @@ | ||
| // Copyright (c) 2025 Cloudflare, Inc. | ||
| // Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
| // https://opensource.org/licenses/Apache-2.0 | ||
|
|
||
| 'use strict'; | ||
|
|
||
| const assert = require('node:assert/strict'); | ||
|
|
||
| const url = process.argv[2]; | ||
| assert.ok(url, 'Usage: websocket-close-propagation-client.js <ws-url>'); | ||
|
|
||
| const CLOSE_TIMEOUT_MS = 5000; | ||
| const CONNECT_RETRY_DEADLINE_MS = Date.now() + 5000; | ||
|
|
||
| function connectOnce() { | ||
| return new Promise((resolve, reject) => { | ||
| const ws = new WebSocket(url); | ||
| let settled = false; | ||
|
|
||
| const timeout = setTimeout(() => { | ||
| if (!settled) { | ||
| settled = true; | ||
| reject( | ||
| new Error( | ||
| `Timed out after ${CLOSE_TIMEOUT_MS}ms waiting for close event` | ||
| ) | ||
| ); | ||
| } | ||
| }, CLOSE_TIMEOUT_MS); | ||
|
|
||
| function done(fn) { | ||
| if (!settled) { | ||
| settled = true; | ||
| clearTimeout(timeout); | ||
| fn(); | ||
| } | ||
| } | ||
|
|
||
| ws.addEventListener('open', () => { | ||
| ws.send('hello'); | ||
| }); | ||
|
|
||
| ws.addEventListener('message', (event) => { | ||
| if (event.data !== 'Echo: hello') { | ||
| done(() => reject(new Error(`Unexpected message: ${event.data}`))); | ||
| return; | ||
| } | ||
|
|
||
| // The echo response above proves data flows end-to-end. Initiate a | ||
| // clean close — the close event should propagate back to the client | ||
| // the same way data does. | ||
| ws.close(1000, 'client closing'); | ||
| }); | ||
|
|
||
| ws.addEventListener('close', (event) => { | ||
| done(() => { | ||
| if (event.code !== 1000) { | ||
| reject( | ||
| new Error( | ||
| `Expected close code 1000, got ${event.code} (reason: ${event.reason})` | ||
| ) | ||
| ); | ||
| } else { | ||
| resolve({ code: event.code, reason: event.reason }); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| ws.addEventListener('error', (event) => { | ||
| done(() => reject(new Error(event?.message ?? 'WebSocket error'))); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| (async () => { | ||
| for (;;) { | ||
| try { | ||
| const result = await connectOnce(); | ||
| console.log(`Closed with code=${result.code} reason=${result.reason}`); | ||
| process.exit(0); | ||
| } catch (err) { | ||
| const message = err?.message ?? String(err); | ||
| const isConnectError = | ||
| message.includes('ECONNREFUSED') || | ||
| message.includes('network error') || | ||
| message.includes('non-101'); | ||
|
|
||
| if (isConnectError && Date.now() < CONNECT_RETRY_DEADLINE_MS) { | ||
| await new Promise((r) => setTimeout(r, 100)); | ||
| continue; | ||
| } | ||
|
|
||
| console.error(message); | ||
| process.exit(1); | ||
| } | ||
| } | ||
| })(); | ||
60 changes: 60 additions & 0 deletions
60
src/workerd/server/tests/container-client/websocket-close-propagation-test.sh
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,60 @@ | ||
| #!/usr/bin/env bash | ||
| # Copyright (c) 2025 Cloudflare, Inc. | ||
| # Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
| # https://opensource.org/licenses/Apache-2.0 | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| WORKERD_BINARY="$1" | ||
| CLIENT_BINARY="$2" | ||
| TEMPLATE_CAPNP="$3" | ||
| WORKER_JS="$4" | ||
|
|
||
| TMPDIR="${TEST_TMPDIR:-$(mktemp -d)}" | ||
| RUNDIR="$TMPDIR/ws-close-propagation" | ||
| mkdir -p "$RUNDIR" | ||
|
|
||
| cleanup() { | ||
| if [[ -n "${WORKERD_PID:-}" ]]; then | ||
| kill "$WORKERD_PID" 2>/dev/null || true | ||
| wait "$WORKERD_PID" 2>/dev/null || true | ||
| fi | ||
| } | ||
| trap cleanup EXIT | ||
|
|
||
| cp "$WORKER_JS" "$RUNDIR/websocket-close-propagation-worker.js" | ||
|
|
||
| LOG="$RUNDIR/workerd.log" | ||
|
|
||
| for attempt in $(seq 1 30); do | ||
| PORT=$(( (RANDOM % 20000) + 30000 )) | ||
| sed -e "s/__PORT__/${PORT}/g" \ | ||
| "$TEMPLATE_CAPNP" > "$RUNDIR/config.capnp" | ||
|
|
||
| "$WORKERD_BINARY" serve "$RUNDIR/config.capnp" --experimental --verbose \ | ||
| --directory-path=TEST_TMPDIR="$TMPDIR" \ | ||
| >"$LOG" 2>&1 & | ||
| WORKERD_PID=$! | ||
|
|
||
| sleep 0.5 | ||
| if ! kill -0 "$WORKERD_PID" 2>/dev/null; then | ||
| wait "$WORKERD_PID" 2>/dev/null || true | ||
| unset WORKERD_PID | ||
| continue | ||
| fi | ||
|
|
||
| URL="ws://127.0.0.1:${PORT}/ws" | ||
| if "$CLIENT_BINARY" "$URL"; then | ||
| exit 0 | ||
| fi | ||
|
|
||
| echo "WebSocket close propagation test failed (${URL})" >&2 | ||
| echo "--- workerd log tail ---" >&2 | ||
| tail -200 "$LOG" >&2 || true | ||
| exit 1 | ||
| done | ||
|
|
||
| echo "failed to start workerd after repeated attempts" >&2 | ||
| echo "--- workerd log tail ---" >&2 | ||
| tail -200 "$LOG" >&2 || true | ||
| exit 1 |
65 changes: 65 additions & 0 deletions
65
src/workerd/server/tests/container-client/websocket-close-propagation-worker.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,65 @@ | ||
| // Copyright (c) 2025 Cloudflare, Inc. | ||
| // Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
| // https://opensource.org/licenses/Apache-2.0 | ||
|
|
||
| import { DurableObject } from 'cloudflare:workers'; | ||
| import { scheduler } from 'node:timers/promises'; | ||
|
|
||
| export default { | ||
| async fetch(request, env) { | ||
| const upgrade = request.headers.get('Upgrade'); | ||
| if (upgrade?.toLowerCase() !== 'websocket') { | ||
| return new Response('expected websocket', { status: 400 }); | ||
| } | ||
|
|
||
| const id = env.MY_CONTAINER.idFromName('repro'); | ||
| const stub = env.MY_CONTAINER.get(id); | ||
| return stub.fetch(request); | ||
| }, | ||
| }; | ||
|
|
||
| export class ContainerProxy extends DurableObject { | ||
| async fetch(request) { | ||
| const { container } = this.ctx; | ||
|
|
||
| if (!container.running) { | ||
| container.start({ | ||
| env: { WS_ENABLED: 'true' }, | ||
| enableInternet: true, | ||
| }); | ||
| } | ||
|
|
||
| // Proxy the websocket upgrade into the container and return it to the | ||
| // eyeball client. Close events should propagate back through the same | ||
| // path as data. | ||
| const maxRetries = 6; | ||
| for (let i = 1; i <= maxRetries; i++) { | ||
| try { | ||
| return await container.getTcpPort(8080).fetch('http://container/ws', { | ||
| headers: { | ||
| Upgrade: 'websocket', | ||
| Connection: 'Upgrade', | ||
| 'Sec-WebSocket-Key': 'x3JJHMbDL1EzLkh9GBhXDw==', | ||
| 'Sec-WebSocket-Version': '13', | ||
| }, | ||
| }); | ||
| } catch (e) { | ||
| if (!e.message.includes('container port not found')) { | ||
| throw e; | ||
| } | ||
| console.info( | ||
| `Retrying getTcpPort(8080) for the ${i} time due to an error ${e.message}` | ||
| ); | ||
| console.info(e); | ||
| if (i === maxRetries) { | ||
| console.error( | ||
| `Failed to connect to container for WebSocket. Retried ${i} times` | ||
| ); | ||
| throw e; | ||
| } | ||
| await scheduler.wait(1000); | ||
| } | ||
| } | ||
| throw new Error('unreachable'); | ||
| } | ||
| } |
36 changes: 36 additions & 0 deletions
36
src/workerd/server/tests/container-client/websocket-close-propagation.capnp.in
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,36 @@ | ||
| using Workerd = import "/workerd/workerd.capnp"; | ||
|
|
||
| const config :Workerd.Config = ( | ||
| services = [ | ||
| (name = "main", worker = .mainWorker), | ||
| (name = "internet", network = (allow = ["private"])), | ||
| (name = "TEST_TMPDIR", disk = (writable = true)), | ||
| ], | ||
|
|
||
| sockets = [ | ||
| ( name = "http", | ||
| address = "127.0.0.1:__PORT__", | ||
| http = (), | ||
| service = "main" | ||
| ), | ||
| ], | ||
| ); | ||
|
|
||
| const mainWorker :Workerd.Worker = ( | ||
| modules = [ | ||
| (name = "worker", esModule = embed "websocket-close-propagation-worker.js"), | ||
| ], | ||
| compatibilityDate = "2026-02-03", | ||
| compatibilityFlags = ["nodejs_compat", "experimental"], | ||
| containerEngine = (localDocker = (socketPath = "unix:/var/run/docker.sock")), | ||
| durableObjectNamespaces = [ | ||
| ( className = "ContainerProxy", | ||
| uniqueKey = "container-client-ws-close-propagation", | ||
| container = (imageName = "cloudflare/workerd/container-client-test") | ||
| ), | ||
| ], | ||
| durableObjectStorage = (localDisk = "TEST_TMPDIR"), | ||
| bindings = [ | ||
| (name = "MY_CONTAINER", durableObjectNamespace = "ContainerProxy"), | ||
| ], | ||
| ); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You always need to make sure to reciprocate the close:
That is likely the bug you have
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah this is a gap in my repro, but even with reciprocation, I've verified the close event doesn't propagate back to the client. And my earliest test for this on the sandbox-sdk was a server-initiated close scenario, which would still remain.
I can try to expand on the repro to add these cases if that'd be useful!