Repository navigation
Keep a UDP socket open when it receives an empty datagram - #1004
Conversation
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>
|
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? |
|
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 |
|
Thanks for explaining @JC-000 . Yeah, makes a lot of sense. This will help strengthen the foundation that new features can be built on. |
|
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. |
Problem
NetworkTarget::read_sockettreatsrecv()returning 0 as "connection closed by host" for every socket: it untracks the handle andlwip_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
01,CONNECTION CLOSED BY HOST12,SEND ERROR: 9(EBADF)12,ERROR ON CLOSE: 9A 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 status00,OK. That is distinct from "nothing pending", which stays02,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-openin theuci-net-targetsuite: send a 0-byte datagram to an open UDP handle; READ reports it as empty with00,OK, the next READ is02,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.empty-datagram-keeps-udp-openfails on both the REST and native routes. READ gets01,CONNECTION CLOSED BY HOSTand CLOSE gets12,ERROR ON CLOSE: 9. The other 10 scenarios pass on both routes.net_target_test: OK (143 checks, 482s), every scenario on both routes.#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