Repository navigation
# Socket DMA service: read each message whole, never run a partial payload - #1076
Conversation
Each client socket has a 1 s receive timeout, so the service can see when it is switched off. The read loop treated that timeout as the end of the payload. A client that paused for a second mid-payload (WiFi retries, a flash erase, a slow card) got its program DMA'd and run with a tail that never arrived, and the rest of the payload was then read as commands. A pause of a second between two commands dropped the connection, and a header split across two segments was misread. Read every header and payload in full. A timeout is now a retry: a message is given up only after 30 s without progress, and a client may wait as long as it likes between commands, with TCP keepalive to reap a dead peer. A payload that does not arrive in full is never executed; the connection is closed. A payload larger than the buffer was truncated to 200000 bytes and run; it is now read and discarded so the stream stays in step. The service still notices being switched off within a second. The wire protocol is unchanged. The new host test target/pc/linux/socketdma runs socket_dma.cc against loopback sockets and stubs; it is part of make host_tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With a network password set, a client that connected and never authenticated could sit silent for ever and hold the only socket DMA slot; before the previous change it was dropped after 1 s. Such a client now gets the 30 s stall limit while it waits; only a client that may send commands may wait between them indefinitely. C64 never waits: a client that cannot send commands cannot keep the C64's DMA service from the next client for longer than the limit. Tests: an unauthenticated silent client is dropped at the limit; an authenticated one may idle past it; one header byte followed by silence is dropped at the limit; an upload that keeps coming is loaded whole even when it takes longer than the limit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
chrisgleissner
left a comment
There was a problem hiding this comment.
@JC-000 thanks for this. Reading each message whole is the right fix, and the host tests compile the real socket_dma.cc: 11 of 13 fail when I revert it to master.
Requesting changes on two points:
- Drain before auth. The over-size drain (up to 16 MB) runs before the authentication check. A client that has not authenticated can make the device read that much. Please stop before draining when the connection is unauthenticated and the command is not the authentication command.
- Hardware evidence. The hardware table in the description comes from a harness that is not in the PR. Please add it as an e2e suite registered in
run-tests, or describe how to reproduce it, so the u64 and C64U runs can be repeated.
I'll merge once these are done.
|
@JC-000 thank you for this fix. Reading each message whole is a real improvement, and the host tests that compile the real I will make the one change I asked for in my review (stop before the over-size drain when the connection is not yet authenticated), run the hardware checks on the Ultimate 64 and the U2+L, and push it as separate commits on your branch. Nothing is needed from you. If you have already started on it, please say so here and I'll hold off. |
…nZ#1076) Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Thanks for taking this over. One heads-up: a merge of master into the branch (5b6ebf0) went out 24 s after your comment, from a base update that was already running. It only brings the branch up to date and doesn't touch your review points. Pull before you push. I won't push anything more to this branch or #1071. Written with Claude Opus and Claude Sonnet in Claude Code. |
|
@JC-000 an update on this PR, which I'm finishing up. Thank you again for the fix. I pushed to your branch (
Verification of that commit:
|
chrisgleissner
left a comment
There was a problem hiding this comment.
@JC-000 thank you for this fix and for the real-code host tests. I have read the diff and the three-line addition I pushed, and I am merging.
Review: each command and length header is now read whole, a payload is never run partially, an over-size payload is skipped so the stream stays in sync, and a client that has not authenticated is dropped at the stall limit or, with the added lines, as soon as it sends anything but AUTHENTICATE. CI is green on the head.
Tests: the 14 host tests compile the real socket_dma.cc. With socket_dma.cc reverted, 11 of 13 original tests fail, and with only my three lines removed the new test fails.
Hardware (Ultimate 64 Elite and C64 Ultimate, network password set): an unauthenticated header for a 1 MB RUN_CRT with no payload held the connection for 30 to 31 s before the added lines and closes immediately with them, three runs each. An authenticated session still gets its AUTH reply and an IDENTIFY answer, and dma-round-trip passes on both.
Not tested: the U2+L. The addition is an early break that performCommand already did for the same condition, so I judged it unlikely to behave differently there.
Fixes #1051
Problem
The socket DMA service (TCP, used by the "DMA" network service and tools that push images/CRTs) can execute a command on a partial payload, and can lose stream framing after an over-size payload, so the next bytes are parsed as commands. A client that pauses mid-message (WiFi retries, a flash erase, a slow card) can have a half-received payload run as if complete. On an unauthenticated connection the receive loop also assumes every header
recv()returns the full 2 bytes.Cause, on current master:
SocketDMA::dmaThreadinsoftware/network/socket_dma.cc:481-514:recv(newsockfd, buf, 2, 0)and the code comments// assume n = 2. With the 1 sSO_RCVTIMEOa short read is not handled.readSocket(socket_dma.cc:353) returnsreceived > 0 ? received : n(the return at:361), i.e. a partial count if the stream stalls part-way;performCommandis then called with that partial length and runs the command on a truncated buffer.len32 = SOCKET_BUFFER_SIZE) but the surplus bytes are left in the stream and read next as a command/length pair.What changed and why
readSocketnow reads exactlylengthbytes or fails; it gives up a message only afterSOCKET_DMA_STALL_MS(30 s) without progress, returns -1 on a mid-message close, and enables TCP keepalive so dead peers are reaped. An over-size payload is drained and skipped cleanly instead of being clamped. All header reads go through the same bounded reader.Products. runs on the Ultimate 64, Ultimate 64 Elite II, C64 Ultimate and the Ultimate-II cartridges (U2+, U2+L). Tested on the Ultimate 64 Elite and the C64 Ultimate.
How I tested it
Host: macOS, with the device-free tests run in an Ubuntu 24.04 container (g++ 13.3). The hardware runs used the test harness from the same machine.
Host tests (device-free, run before and after the change):
dmaThread:make -B -C target/pc/linux/socketdma testsocket_dma.ccand.h)Hardware.
RED = master, GREEN = fix, both products via the test harness.
Host suite
target/pc/linux/socketdma: 2/13 on master → 13/13 with the fix; three mutants killed.Standard profile (
./run-tests --profile standard), red = master, green = a build carrying this landing set of fixes (this change included), report frompython3 tools/e2e_report.py:iec-partition-config: it stops at a key-send step because the upstream test's REST backend has noF2key alias (rest_backend.py:556), a test-side limitation, not an app difference.iec-partition-config, here at a "menu screen unavailable" step that also fails on master, so it is not from these changes.Soaks pass on both devices: mount-cache-leak, listener-soak, network-connection.
Untested
Written with Claude Opus 5.5 in Claude Code.