Skip to content

Implement inbound UDP handler - #7130

Merged
jasnell merged 29 commits into
cloudflare:mainfrom
ThomasRubini:udp_handler
Sep 17, 2026
Merged

jasnell merged 29 commits into
cloudflare:mainfrom
ThomasRubini:udp_handler

Conversation

@ThomasRubini

@ThomasRubini ThomasRubini commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR adds support for workerd to handle inbound UDP "connections" and deliver them to a connect() handler.

It does 2 things:

To prevent one "UDP connection"'s slow reader from blocking others, we always read all UDP packets from the kernel, and store them in our own per-connection buffers (in Flow), dropping new packets for a connection when its buffer is full.

The UDP connect() handler has been implemented as a custom event because connect() on WorkerInterface required a kj::AsyncIoStreamwhich is byte-oriented rather than message-oriented like UDP requires

UDP sockets will read and write custom Datagram objects instead of Uint8Array, to avoid user confusion if they try to use them with code that expect stream-based sockets.

Example usage

export default {
  async connect(socket): Promise<void> {
		console.log("New connection");
		const reader = socket.readable.getReader();
		const writer = socket.writable.getWriter();

		const { remoteAddress } = await socket.opened;
		const greeting = new TextEncoder().encode(
			`hello from connect(), protocol: ${socket.protocol}, remoteAddress: ${remoteAddress ?? "unknown"}\n`,
		);
		await writer.write(socket.protocol === "udp" ? new Datagram(greeting) : greeting);

		const decoder = new TextDecoder('utf-8');
		try {
			for (;;) {
				const { value, done } = await reader.read();
				if (done) {
					break;
				}
				console.log("Received datagram: " +  decoder.decode(value.data));
				await writer.write(value);
			}
		} finally {
			await writer.close();
		}
	},
} satisfies ExportedHandler<Env>;

testable by adding this bit in a workerd config:

sockets = [
    ( name = "http", address = "*:8787", http = (), service = "main" ),
    ( name = "tcp", address = "*:5432", tcp = (), service = "main" ),
    ( name = "udp", address = "*:5599", udp = (idleTimeoutMs = 30000, maxPendingBytes = 262144), service = "main" ),
  ]

@github-actions

github-actions Bot commented Aug 26, 2026 •

Copy link
Copy Markdown

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

@ThomasRubini

Copy link
Copy Markdown
Contributor Author

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

Comment thread src/workerd/api/sockets.h
Comment thread src/workerd/api/sockets.h
Comment thread src/workerd/api/sockets.h Outdated
Comment thread src/workerd/api/sockets.c++ Outdated
Comment thread src/workerd/api/sockets.c++ Outdated
Comment thread src/workerd/api/sockets.c++ Outdated
Comment thread src/workerd/api/global-scope.c++

@dom96 dom96 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would ideally like to see this added to the sockets spec before it is implemented: https://sockets-api.proposal.wintertc.org/

It's also worth considering how our design compares to the direct-socket API. Ideally we shouldn't diverge from it unless it's necessary. It would be nice to reuse its UDPMessage + ReadableStream/WritableStream approach for example.

Comment thread src/cloudflare/internal/sockets.d.ts
Comment thread src/workerd/api/global-scope.c++
@ThomasRubini
ThomasRubini marked this pull request as ready for review August 27, 2026 20:12
@ThomasRubini
ThomasRubini requested review from a team as code owners August 27, 2026 20:12
@ThomasRubini
ThomasRubini requested a review from a team as a code owner September 1, 2026 13:48
Comment thread src/workerd/api/js-writable-stream.c++

@harrishancock harrishancock left a comment

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.

I'm still reading, but wanted to leave some initial comments.

Comment thread src/workerd/server/server.c++ Outdated
Comment thread src/workerd/server/server.c++ Outdated
Comment thread src/workerd/server/server.c++ Outdated
Comment thread src/workerd/server/server.c++ Outdated
Comment thread src/workerd/server/server.c++
Comment thread src/workerd/server/server.c++ Outdated
Comment thread src/workerd/server/tests/udp-datagram-drop/index.mjs
Comment thread src/workerd/server/tests/udp-datagram-drop/index.mjs
@ThomasRubini
ThomasRubini force-pushed the udp_handler branch 3 times, most recently from 9e9da20 to 3ad297f Compare September 16, 2026 00:09
ThomasRubini and others added 22 commits September 17, 2026 17:07
it should not have buffering, reflecting JsReadableStream::from()
This avoids an old flow unregistering its key after a new Flow has started using it
Assisted-by: OpenCode:gpt-5.6-sol
Writable datagram chunks remain mutable from JavaScript while an asynchronous send is pending. Snapshotting at the stream boundary prevents later mutation or detachment from changing the packet on the wire.

Assisted-by: OpenCode:gpt-5.6-sol
A truncated datagram has permanently lost its tail and cannot be delivered without changing its contents. Dropping it before flow lookup also prevents malformed packets from creating flows or extending their idle lifetime.

Assisted-by: OpenCode:gpt-5.6-sol
KJ currently binds only the first resolved address for datagram ports, unlike its stream listener implementation. The socket schema should not promise multi-address behavior that UDP cannot provide.

Assisted-by: OpenCode:gpt-5.6-sol
Windows handles it in a different way which will be implemented as part of a follow-up PR
@jasnell

jasnell commented Sep 17, 2026

Copy link
Copy Markdown
Collaborator

Internal pipeline is green. Merging

@jasnell
jasnell merged commit 50c2e90 into cloudflare:main Sep 17, 2026
44 of 47 checks passed
@ThomasRubini
ThomasRubini deleted the udp_handler branch September 17, 2026 22:25
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.

6 participants