Skip to content

Keep a UDP socket open when it receives an empty datagram - #1004

Merged
chrisgleissner merged 1 commit into
GideonZ:masterfrom
JC-000:fix/udp-zero-datagram
Oct 17, 2026
Merged

chrisgleissner merged 1 commit into
GideonZ:masterfrom
JC-000:fix/udp-zero-datagram

Conversation

@JC-000

@JC-000 JC-000 commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

Problem

NetworkTarget::read_socket treats recv() returning 0 as "connection closed by host" for every socket: it untracks the handle and lwip_close()s it. That is right for TCP. On UDP a 0-byte datagram is valid data, so one empty packet from the peer (or anyone spoofing its address) closes the C64 program's UDP handle.

Reproduced on a U64E: open a UDP handle, send it a 0-byte datagram, then

  • READ answers 01,CONNECTION CLOSED BY HOST
  • WRITE answers 12,SEND ERROR: 9 (EBADF)
  • CLOSE answers 12,ERROR ON CLOSE: 9

A 1-byte datagram as control leaves the handle open and everything works.

Fix

Only a stream socket treats 0 as closed. The type is read with getsockopt(SO_TYPE) at the point of use, so no state is added to the socket table and it holds for any socket the target owns.

An empty datagram reads as a zero-length reply (header 00 00, no payload) with status 00,OK. That is distinct from "nothing pending", which stays 02,NO DATA: 11. On UDP a zero length no longer means the socket was closed; the status says which. TCP is unchanged. A client that already handles a short read needs no change.

Tests

New E2E scenario empty-datagram-keeps-udp-open in the uci-net-target suite: send a 0-byte datagram to an open UDP handle; READ reports it as empty with 00,OK, the next READ is 02,NO DATA, a following datagram is delivered, WRITE reaches the peer and CLOSE succeeds. ruff is clean.

Hardware: U64E, core 1.50. Host: macOS; device-free checks also on the ubuntu:24.04 rig. U2+ is untested.
./run-tests uci-net-target (DEEP profile) on both images.

#835 is in both images only because it's on all my bench images until it ships; it isn't part of this branch.

No issue filed: small fix, per CONTRIBUTING.

cc @enver-haase (software/io/network), @chrisgleissner (tests/, author of #806)

Written with Claude Opus 5.5 in Claude Code, reviewed by a second session.

🤖 Generated with Claude Code

recv() returning 0 means the peer closed only on a stream socket. READ_SOCKET
applied it to every socket, so one 0-byte datagram untracked and closed a
plain UDP handle: the next READ said the connection was closed by the host,
and WRITE and CLOSE then failed with EBADF.

Only a stream socket treats 0 as closed now. An empty datagram reads as a
zero-length reply with status 00,OK, which is distinct from 02,NO DATA.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@chrisgleissner

Copy link
Copy Markdown
Collaborator

Hi @JC-000, nice fix, and no doubt it is a bug since empty udp packets are very common in specific scenarios.

However, I am wondering whether you encountered any realistic scenario where this bug surfaced for the U64. Just curious. Of course, this is entirely irrelevant. Even if it is rare, it is still a bug.

So thanks again for having raised this PR. It is fine to be merged from my perspective.

Any thoughts @GideonZ?

@JC-000

JC-000 commented Oct 17, 2026

Copy link
Copy Markdown
Contributor Author

Thanks! No, we didn't hit it in the wild. It turned up in a code review while we were designing UDP/DTLS support for the network target, and then we reproduced it on a U64E.

It does matter for where we'd like to take this. We've been considering NAT traversal for UDP, so that C64 games can talk point to point. That relies on exactly this kind of traffic: hole punching, and empty keepalive datagrams to hold the NAT mapping open. As it stands, a single empty datagram closes the socket. It can come from a peer's keepalive or from anyone else on the LAN. The next READ then reports 01,CONNECTION CLOSED BY HOST for a connectionless socket, so that kind of networking can't work until this is fixed.

@chrisgleissner

Copy link
Copy Markdown
Collaborator

Thanks for explaining @JC-000 . Yeah, makes a lot of sense. This will help strengthen the foundation that new features can be built on.

@chrisgleissner chrisgleissner added bug Existing behavior is incorrect, broken, or regressed; includes supported documentation defects. network Networking and remote services: REST, Socket DMA, FTP, Telnet, and online access. labels Oct 17, 2026
@chrisgleissner

Copy link
Copy Markdown
Collaborator

Reviewed dc1c298 for substantive correctness issues. I traced READ_SOCKET through the pinned lwIP revision (26a22151), checking empty UDP datagram consumption, SO_TYPE support, TCP EOF handling, zero-length reply framing, and the new scenario’s assertions and failure propagation. I also ran Ruff on the changed Python test; it passed.

No substantive issues found. This was a source review plus lint validation; I did not independently rerun the hardware tests.

@chrisgleissner
chrisgleissner merged commit 79b7d64 into GideonZ:master Oct 17, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Existing behavior is incorrect, broken, or regressed; includes supported documentation defects. network Networking and remote services: REST, Socket DMA, FTP, Telnet, and online access.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants