Skip to content

Add experimental outbound UDP to cloudflare:sockets - #8140

Open
deregtd wants to merge 1 commit into
cloudflare:mainfrom
deregtd:ddr/outbound-udp
Open

deregtd wants to merge 1 commit into
cloudflare:mainfrom
deregtd:ddr/outbound-udp

Conversation

@deregtd

@deregtd deregtd commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

User stories affected or enabled by this change

Workers and Durable Objects can send arbitrary outbound UDP datagrams to a fixed destination, including applications that never receive a reply:

const socket = connect(
  { hostname: 'metrics.example.com', port: 8125 },
  { protocol: 'udp' },
);
const encoder = new TextEncoder();
const writer = socket.writable.getWriter();
try {
  await writer.write(encoder.encode('requests:1|c'));
} finally {
  writer.releaseLock();
  await socket.close();
}

This is experimental and does not enable outbound UDP in Cloudflare deployments. Embedders supply their own routing and rollout policy.

APIs/interfaces created, changed, or removed

  • connect(address, { protocol: 'udp' }), gated by the experimental outbound_udp_sockets compatibility flag. Omitting protocol keeps TCP behavior. On older compatibility dates, this opt-in also enables the JavaScript stream constructors needed by the value streams.
  • UDP uses value-mode streams: writes accept any ArrayBufferView (TypedArray or DataView), and reads return Uint8Array. One chunk is one datagram, including an empty view. Slices send only their visible raw bytes, without element or byte-order conversion. With this flag, ingress UDP uses the same shapes.
  • The socket exposes protocol, opened, closed and close(). UDP rejects TCP-only options and TLS upgrades.
  • The embedding interface gains DatagramChannel and an openUdp custom event/RPC path.
  • Standalone workerd enforces a 65507-byte IPv4 payload ceiling; embedders can enforce a smaller transport limit. A rejected sink write errors the writable stream under ordinary Streams semantics, so later writes also reject.

Architecture choices and reasoning

Reuse the ingress socket shape and existing stream abstractions so callers do not need a new Datagram wrapper. Preserve packet boundaries instead of adapting UDP to a byte stream. Copy payloads while the isolate is accessible, honor Durable Object output gates on every send, and propagate transport errors across RPC. Socket teardown waits for an already-pending writable close. Releasing the reverse RPC capability without EOF fails pending receives with a session-disconnected error; orderly EOF still completes normally.

The standalone implementation uses an ephemeral native UDP socket and applies the configured network address restrictions before sending. This does not introduce multicast, broadcast, per-packet destinations, hibernation or delivery acknowledgments.

Validation covers standalone and RPC UDP, both stream implementations, ingress peer addresses, payload boundaries, output gates, teardown and transport failures. The rebased fixtures use the dated UDP ingress flag and the consolidated ingress test targets. Native Linux validation passed all 21 selected regression targets five runs each, including downstream sandbox integration. Production clang-tidy and formatting checks pass. Hosted CI passes, including the full platform matrix, type snapshots and coverage.

CI fixture fixes preserve workloads and behavioral assertions through completion barriers, CPU reservations and deterministic cleanup. The REPL signal fixture suppresses host core-dump services while retaining its ten-second deadline and terminal-restoration assertions. Current main supplies the Bazel version and repository-cache fix. Fork jobs that fall back to local execution disable the disk cache when that workflow never persists it, avoiding a duplicate copy of coverage outputs.

See docs/outbound-udp.md for semantics, errors and reproduction commands. Current results are in the workerd checks.

@github-actions

github-actions Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@deregtd

deregtd commented Nov 2, 2026

Copy link
Copy Markdown
Member Author

I have read the CLA Document and I hereby sign the CLA

github-actions Bot added a commit that referenced this pull request Nov 2, 2026
@deregtd deregtd changed the title Add experimental connected UDP to cloudflare:sockets Add experimental outbound UDP to cloudflare:sockets Nov 2, 2026
@codecov-commenter

codecov-commenter commented Nov 2, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 47.07602% with 362 lines in your changes missing coverage. Please review.
✅ Project coverage is 70.38%. Comparing base (8035f89) to head (ed69796).

Files with missing lines Patch % Lines
src/workerd/api/sockets.c++ 39.82% 128 Missing and 11 partials ⚠️
src/workerd/server/tests/udp-egress/index.mjs 0.00% 102 Missing ⚠️
src/workerd/server/server/services/udp.rs 0.00% 47 Missing ⚠️
...c/workerd/server/factory/worker-factory-listen.c++ 0.00% 21 Missing ⚠️
src/workerd/api/sockets-test.c++ 90.38% 0 Missing and 20 partials ⚠️
src/workerd/api/sockets.h 27.77% 11 Missing and 2 partials ⚠️
src/workerd/server/server/bridge.rs 0.00% 5 Missing ⚠️
src/workerd/server/tests/udp-connect/index.mjs 0.00% 5 Missing ⚠️
src/workerd/server/factory/worker-factory-rpc.c++ 0.00% 3 Missing ⚠️
src/workerd/api/streams/standard.c++ 83.33% 1 Missing and 1 partial ⚠️
... and 3 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8140      +/-   ##
==========================================
- Coverage   70.43%   70.38%   -0.06%     
==========================================
  Files         908      911       +3     
  Lines      234829   235470     +641     
  Branches    34011    34089      +78     
==========================================
+ Hits       165398   165728     +330     
- Misses      49238    49518     +280     
- Partials    20193    20224      +31     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@deregtd
deregtd marked this pull request as ready for review November 17, 2026 04:53
@deregtd
deregtd requested review from a team as code owners November 17, 2026 04:53
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-17T05:08:00.066775Z f507821 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@clanky-the-clanker

Copy link
Copy Markdown

This pull request was not reviewed automatically because its author does not have write access to this repository. A maintainer with write access can start a review by commenting clanky review (or clanker review or @clanky-the-clanker review), optionally followed by instructions (e.g. clanky review focus on the retry logic).

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f50782170b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/workerd/api/sockets.c++ Resolved
Comment thread src/workerd/io/compatibility-date.capnp Resolved
Comment thread docs/outbound-udp.md Resolved
#ifdef V8_COMPRESS_POINTERS
isolate->external_pointer_table().SetUpFromReadOnlyArtifacts(
isolate->heap()->read_only_external_pointer_space(), artifacts);
diff --git a/src/heap/read-only-spaces.cc b/src/heap/read-only-spaces.cc

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Context for the V8 review: the isolated guard commit d31a683 fixes a release-tool build failure from -Wunused-function. Both callers are inside DEBUG guards, so the helper's definition now has the same guard. This does not change the checksum calculation or snapshot format. Leaving this thread open for V8-owner review.

Comment thread src/workerd/api/sockets.c++ Resolved
@jasnell

jasnell commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

Overall this is looking good but let's get v8 team review of the patches. Will do a final review after they weigh in.

@deregtd
deregtd requested a review from a team as a code owner November 19, 2026 03:46
@deregtd
deregtd force-pushed the ddr/outbound-udp branch 7 times, most recently from a5a727e to ad12698 Compare November 23, 2026 21:17
@deregtd
deregtd force-pushed the ddr/outbound-udp branch 20 times, most recently from fb911ec to 17c5473 Compare November 30, 2026 21:24
Expose connect(address, { protocol: "udp" }) behind the experimental
outbound_udp_sockets compatibility flag. Use binary value streams
with one chunk per datagram and preserve packet boundaries. Keep TCP
behavior unchanged when protocol is omitted.

Support standalone and RPC transports, visible typed-array bytes,
output gates, bounded writes, teardown and transport error propagation.
A rejected sink write follows ordinary WritableStream error semantics.
Include API and type documentation and UDP integration coverage for
both stream implementations, using the dated UDP ingress flag.

Preserve existing CI workloads and assertions while synchronizing
asynchronous fixture cleanup and reserving CPU for runtime threads.
Serialize Miniflare SDK tests to avoid its port-probe race. Keep the
WPT empty streaming-upload fixture from reusing a connection whose
request body the sidecar does not consume. Retain other keep-alive
coverage. Suppress host core-dump services in the REPL signal fixture
without changing its deadline or terminal-restoration assertions.

Avoid retaining a duplicate disk cache when fork CI falls back from
bazelflare to local execution without saving that cache. This keeps
the full coverage workload within the runner disk budget.
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.

4 participants