Skip to content

UCI network target: run commands on a worker task, answer quick ones at once, retry a send while the link has no buffer - #1073

Merged
chrisgleissner merged 19 commits into
GideonZ:masterfrom
JC-000:fix/uci-network-target-worker
Nov 12, 2026
Merged

chrisgleissner merged 19 commits into
GideonZ:masterfrom
JC-000:fix/uci-network-target-worker

Conversation

@JC-000

@JC-000 JC-000 commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1049

Problem

UCI target 3 (the network target) runs each command on the UCI Server task. A write to a stalled peer, a DNS lookup or a connect keeps that task, so an abort or a C64 reset waits for it, and commands that need no socket (IDENTIFY, GET_IPADDR, GET_INTERFACE_COUNT, GET_NETADDR) wait behind it. Once the WiFi send stops waiting for a free buffer (#1071), a datagram sent while the link is busy fails at once with ENOMEM instead of waiting.

What changed and why

The network target's commands run on their own task; the UCI Server task hands the command over and the target replies later through the deferral added in #1072. Commands that need no socket are answered at the UCI Server task's own level.

A cancel (abort or reset) is seen between 100 ms slices for connect, receive and the ENOMEM retry. A send slice lasts about 1 s on lwIP. A DNS lookup (gethostbyname_r) is not sliced and can hold the worker for the full DNS timeout, so an abort during an OPEN by name waits for it.

A write that meets ENOMEM is retried a tick later, for up to one second without progress, then reports the error as before; an abort stops it within a tick. This matters once the WiFi send no longer waits for a buffer (#1071); on master the WiFi link waits up to 5 s itself.

As on master, a zero-length UDP WRITE is still sent, a partial send followed by a hard error answers SEND ERROR, and a program's own CLOSE keeps lwIP's normal close, so no unsent data is dropped. New: sockets closed for a reset or abort use the short send timeout, so the next program does not wait for them. A READ of 769 to 1023 bytes is tested at the reply block boundaries.

Stacked on #1072. This branch contains #1072's commits; review #1073's own commits on top of them. When #1072 changes, it is merged into this branch.

Products. Runs on the Ultimate 64, Ultimate 64 Elite II, C64 Ultimate and the Ultimate-II cartridges (U2+, U2+L). Run on hardware 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):

  • network target worker: make -B -C software/io/network/tests network-target
    • with the change: 296 checks, 0 failures. New checks: zero-length UDP write, partial send then SEND ERROR, close timeout per path, abort under a lasting buffer shortage, abort within half a slice
    • without it: with network_target.cc/.h from the base: the test hangs and is stopped at 60 s (an aborted write never frees the worker)
  • everything in UCI: let a target reply later from another task, so abort and reset release the C64 at once #1072 (UCI deferral, HTTP target suites, send-all, test_assembly, iecdrive): all pass with this change on top

Hardware. Firmware loaded into RAM over JTAG, never flashed. Ultimate 64 Elite and C64 Ultimate (Ultimate FPGA 126, C64 core 1.50), C64 at 1 MHz, NTSC, turbo off, Command Interface enabled. Red is upstream master 8299768, the merge base. Each cell is Ultimate 64 Elite / C64 Ultimate.

Leg master 8299768 #1072 0c0b1da #1073 29dbccf
Abort during a stalled HTTP exchange, until the C64 is idle 27965 / 27978 ms 0 / 0 ms 0 / 0 ms
C64 reset during it, until the next program's IDENTIFY answers (includes a fixed 3 s boot wait) 27997 / 28045 ms 3289 / 3298 ms 3283 / 3295 ms
Abort while the server's answer is late (answer at 4 s, abort at 1 s) 3019 / 3024 ms 0 / 0 ms 0 / 0 ms

A READ of 512 to 1472 bytes across reply-block boundaries returns the whole datagram on master and on both heads; it is a regression check, not a fix shown here.

The network-target E2E suite (tests/e2e/io/command_interface/net_target_test.py) passed on the Ultimate 64 Elite at 29dbccf: 143 checks, 0 failures.

Untested

Written with Claude Opus and Claude Sonnet in Claude Code.

JC-000 and others added 7 commits November 7, 2026 14:24
The slice helpers that let the HTTP worker see a cancel (a sliced connect,
sends that wait one SO_SNDTIMEO slice at a time, and the count of time
without progress) move from http_request.cc into socket_slices.h, so that
the network target can use them too. SliceWait takes the no-progress
limit as an argument, 0 for none; HttpRequest passes
HTTP_REQUEST_TIMEOUT_MS as before. A failed sliced connect now leaves the
socket's error in errno. No change in behaviour otherwise.

Toward the C64 never waiting on the CPU: one implementation of bounded,
cancellable socket waits for every task that owns a socket for the UCI.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Target 3 resolved names, connected, sent and closed sockets on the UCI
Server task. A WRITE to a peer that stopped reading blocked it for good, a
connect to a host that does not answer for ~18.5 s, and a C64 reset closed
the sockets there, through the TCP/IP thread, behind the core lock that a
WiFi send can hold for seconds. Abort and reset waited for all of it.

Every target-3 command now runs, in order, on one worker task ("UCI
Network", PRIO_NETSERVICE), which owns the target's sockets: the UCI
Server task copies the command into one of four preallocated jobs, defers
it (command_defer) and queues it. At a C64 reset the UCI Server task only
cancels the command and counts the reset; the worker closes the sockets
before it runs anything else. A reset wakes an idle worker; a busy one
checks after its command. An aborted command that is still queued is not
started; one that runs stops at its next slice: connects and sends now
wait in 100 ms slices (socket_slices.h), with no limit on time without
progress, as before. A socket opened by an aborted OPEN is closed, since
the C64 never learnt its number. Replies are byte-identical.

Toward the C64 never waiting on the CPU: abort and C64 reset no longer
wait for the network on the UCI Server task, for any target, and no
target-3 socket is touched by two tasks.

Test: software/io/network/tests/network_target_test.cpp (make
network-target) runs the target against loopback peers: the replies byte
for byte (IDENTIFY, an address, a UDP write, a 1472-byte read over two
blocks, close), an abort and a reset during a WRITE to a peer that stopped
reading (the UCI side is free at once, the next command answers within
about a slice, the aborted WRITE never reports, and after a reset the old
socket is gone), and the peer seeing the end of the stream after a reset.
Before this change the stalled WRITE blocks its caller.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A game's network notes, measured on fw 3.15, say a READ of 769 to 1023
bytes can hang the Ultimate. On that firmware (before GideonZ#806) a READ of 894
bytes with 894 bytes waiting gave a reply block of 896, which the gateware
never ends: its pointer stops on the last byte with DATA_AV still set, so
a client reading until DATA_AV clears never stops. Lengths above 894 were
refused with 82,PARAMETER(S) OUT OF RANGE, and nothing singled out 769.
Since GideonZ#806 every block is at most 895 bytes and READ takes up to 1472.

The read-lengths case of network_target_test pins that: for 512, 768,
769, 893, 894, 895, 896, 1023, 1024 and 1472 bytes it reads a datagram of
that size and checks the blocks (at most two, none above 895), the count
and the bytes. Against the target before GideonZ#806 it fails at 894 (a block of
896) and refuses 895 and up.

Toward the C64 never waiting on the CPU: no reply block that a client
cannot finish reading.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The UCI Server lowers itself to PRIO_HW_SERVICE for every parse_command,
because target work can be long. For a command that takes no lock, file or
socket, that only makes it wait behind the services. A target can now name
such commands with is_quick(); the server runs them at PRIO_C64_SERVICE.
Nothing is quick unless the target says so.

Toward the C64 never waiting on the CPU: a short answer never waits for
FTP, REST or telnet.

Test: the priority case of uci_deferral_test runs a quick command at the
server's level.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… OPEN

IDENTIFY, GET_INTERFACE_COUNT, GET_IPADDR and GET_NETADDR went through the
target's worker like the socket commands, so they waited behind whatever it
was doing: up to 14 s behind the name lookup of an aborted OPEN. They touch
no socket, so they are now answered on the UCI Server task, as quick
commands, from a reply buffer of their own. Socket commands keep their
order on the worker.

An OPEN that finished just before the C64 aborted it made a socket the C64
never learnt of, which stayed open until the next C64 reset. The UCI
Server task now hands its number to the worker and wakes it, and the
worker closes it before anything else.

Toward the C64 never waiting on the CPU: no answer that needs no socket
waits for the network, and an abort leaves no socket behind.

Tests (network_target_test): IDENTIFY and GET_IPADDR answered at once while
the worker is held in a name lookup (211 ms and more before), the socket
of an OPEN aborted after it finished closed within a slice (never before),
and the time a READ of a waiting datagram takes through the worker on the
host (about 0.1 ms).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Since the WiFi send stopped waiting for a free buffer, a datagram
sent while the link is busy fails at once with ENOMEM, and the C64 got
"12,SEND ERROR: 12" where its WRITE used to wait. The worker now tries
again a tick later, as the blocking send waited, for up to
NET_SEND_NOMEM_MS (1 s) without progress, then reports the error as
before. An abort or a C64 reset stops it within a tick. READ and WRITE
keep their order on the worker.

Toward the C64 never waiting on the CPU: the wait is on the worker, bounded
and cancellable; the UCI Server task never waits for a buffer.

Test: network_target_test "nomem" (send() wrapped to fail with ENOMEM): a
30-tick shortage still sends the datagram once and reports OK, a lasting
one reports the error after the limit, and an abort frees the worker at
once. Before this change the first case fails at once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The nomem case timed a lasting shortage against 1000 ms + 500 ms. The
limit counts ticks, and a loaded host oversleeps each one, so the run
took about 1580 ms on a loaded host and failed. The case now counts the
refused send() calls, one per tick, against the limit, and keeps only a
floor and a loose ceiling (4x) on the time.

Toward the C64 never waiting on the CPU: test only; the bound it checks is
unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chrisgleissner chrisgleissner added bug Existing behavior is incorrect, broken, or regressed; includes supported documentation defects. uci C64-side Ultimate Command Interface, command targets, and Ultimate DOS commands; REST is network. network Networking and remote services: REST, Socket DMA, FTP, Telnet, and online access. labels Nov 10, 2026

@chrisgleissner chrisgleissner 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.

@JC-000 thanks for this. The branch conflicts with master and the CI build fails. It stays behind #1072, so please update it once #1072 is sorted.

… (base update for GideonZ#1073)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@JC-000 JC-000 changed the title # UCI network target: run commands on a worker task, answer quick ones at once, retry a send while the link has no buffer UCI network target: run commands on a worker task, answer quick ones at once, retry a send while the link has no buffer Nov 10, 2026
JC-000 and others added 10 commits November 10, 2026 09:19
…sts do

The CI runner preloads a library, so the ASan-built test stopped with
"ASan runtime does not come first in initial library list".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Merges the updated fix/uci-abort-reset-deferral (890ef6d), which carries
upstream master with GideonZ#1043, GideonZ#1064, GideonZ#1065 and GideonZ#1068 merged in.

Conflict: software/io/network/tests/Makefile, only in the target order
(this branch had send-all after sys-arch-red, upstream before it). Kept
upstream's order and added the network-target target and variables.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… send, 20 s close

An empty UDP WRITE is sent again (send_sliced sends nothing for 0 bytes).
A partial send that ends in a hard error replies SEND ERROR, as before.
SO_SNDTIMEO is cleared before close, since lwIP uses it as the FIN buffer
timeout. Timing checks in the host test are widened.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…-target3-executor

SliceWait (socket_slices.h) now measures the idle time with a clock (0 still
means no limit) and has detach(); http_request.h keeps the one
HTTP_REQUEST_TIMEOUT_MS default; the HTTP connect restarts the timer after DNS.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… a program's reset does not wait out a close

Tests: the ENOMEM retry must stop on the abort while the shortage lasts; the abort
call itself is limited to half a send slice; the close of a C64's CLOSE sees no send
timeout, the close for a reset or an aborted OPEN keeps the slice.
close_all_sockets and the cancel-driven closes keep SO_SNDTIMEO, so a socket that finds no
buffer for its FIN does not hold the worker for lwIP's 20 s. http_request.h: say what
the cancel latency is.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@JC-000

JC-000 commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

#1072 is updated and merged in, with master. The CI build passes.

Fixes since your review, each with a test that fails without it: a zero-length UDP WRITE was no longer sent; a partial send followed by an error answered OK instead of SEND ERROR; and a socket's close could reset the connection after 100 ms and drop unsent data. A program's own CLOSE now closes as on master, and only the sockets closed for a reset or abort close quickly.

The hardware results for both PRs are in the description.

Written with Claude Opus and Claude Sonnet in Claude Code.

@JC-000

JC-000 commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor Author

Master is merged in (29bf5ca), including #1072, so this PR now carries only the network target worker change. The network-target, UCI deferral, HTTP target and assembly host tests pass, and the U64 and U64-II firmware builds succeed at the new head.

Written with Claude Sonnet 5.5 in Claude Code.

@chrisgleissner chrisgleissner 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.

@JC-000 thanks, approving head 29bf5cabd. Its tree is the same as the local merge of this PR into master 1d6678660 that I tested (b80161fb1).

Code review. I read the whole production diff (network_target.cc/.h, socket_slices.h, and the quick-command hook in command_intf.cc and command_target.h) and found no defects. The worker runs with the same stack size and priority as the UCI Server task that ran these commands before. Job ownership passes between the UCI Server task and the worker under a critical section. A socket made by an OPEN that was aborted is closed by the worker.

Host red/green. At head, the network-target test passes with 296 checks and 0 failures, and the UCI deferral test passes with 90 checks. With network_target.cc/.h reverted to master, the network-target test hangs and is stopped at 60 s.

Hardware red/green. I used a small probe built on your #1072 routines. The C64 pushes an OPEN TCP on target 3 to 192.0.2.1:80, an address that never answers. After 1 s it writes ABORT and times how long the interface takes to become idle. Then IDENTIFY on the control target must answer on the first try. Red is master (4f6cbdb34, the same tree as 1d6678660). Green is master with this PR. Each cell gives the ABORT-to-idle time for 3 runs.

Device Red Green
Ultimate 64 Elite (JTAG RAM) 16564, 16656, 16610 ms 0, 0, 0 ms
C64 Ultimate (JTAG RAM) 16757, 16598, 16597 ms 0, 0, 0 ms
U2+L (flashed) 6bcee6d12 (master plus #1072): 16502, 16796, 16791 ms CI build b7a6f5f32 of this PR: 0, 0, 0 ms

IDENTIFY answered CONTROL TARGET V1.1 on the first try in every run. On master, the abort waits until lwIP gives up on the connect.

On the green builds, the uci-net-target, uci-targets and uci-net-route-precondition suites pass on the Ultimate 64 Elite and the C64 Ultimate, and the device health check passes. ./build-tool -s u64 u64ii u2 u2pl builds u64 and u64ii. u2 and u2pl link ultimate.elf and then stop at packaging, because the cached FPGA files are missing locally. CI builds and packages all targets.

@chrisgleissner
chrisgleissner merged commit 9f0535a into GideonZ:master Nov 12, 2026
1 check passed
@chrisgleissner

Copy link
Copy Markdown
Collaborator

@JC-000 merged, thank you. The worker change made the target 3 abort instant on all three devices.

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. uci C64-side Ultimate Command Interface, command targets, and Ultimate DOS commands; REST is network.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A UCI abort or C64 reset keeps the C64 waiting ~28 s while the target cleans up (and a stalled HTTP server blocks the UCI task)

2 participants