Skip to content

Send the whole HTTP request, and give up on a stalled server (#1039) - #1043

Merged
chrisgleissner merged 2 commits into
GideonZ:masterfrom
enver-haase:fix/http-send-all
Nov 11, 2026
Merged

chrisgleissner merged 2 commits into
GideonZ:masterfrom
enver-haase:fix/http-send-all

Conversation

@enver-haase

@enver-haase enver-haase commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1039.

HttpRequest::send_request gave each 128-byte chunk to one non-blocking send() and only treated a negative result as a failure. send() may take part of a chunk and return the count, so the rest was dropped, and the request still counted as sent. With the send buffer full, it failed outright.

Change

  • send_all() (software/network/send_all.h) calls send() until the chunk is sent. It waits while the server isn't reading, and fails when send() fails or sends nothing.
  • connect_to_server sets SO_SNDTIMEO and SO_RCVTIMEO to HTTP_REQUEST_TIMEOUT_MS (30 s). Without them, a blocking send to a server that stops reading would block the UCI task for good. A server that never answers already did that in recv(). Now both fail after 30 s without progress. The limit applies to each call: lwIP returns a partial count when the time runs out, so a stalled send can take up to twice as long to fail.
  • Behaviour change: SO_RCVTIMEO also limits the wait for the first response byte. A server that sends nothing for 30 s, for example a slow API or one that processes a large upload before replying, now ends the exchange with an error, where it used to wait forever.

Production code: http_request.cc +12/−1, http_request.h +6, send_all.h +33 (about half comments). The rest is tests.

Tests

result
software/io/network/tests: send-all (a model of lwip_send: partial counts, full buffer, timeout) 5 passed
send-all-red: the same suite against the old loop 4 of 5 fail, as required
target/pc/linux/test_http_target: test-slow-server (timeout set to 2 s): 4 MB to a loopback server with a 4K receive buffer that waits before reading; a server that never reads; a server that never answers 13 checks, 0 failures. On master the first fails (send_request returns −1). Without SO_SNDTIMEO or SO_RCVTIMEO the second or third fails.
Existing HTTP target, assembly and network suites green
U2+L firmware, full build with the RISC-V toolchain builds and links

test-slow-server is added to the existing CI step. send-all runs in the existing make -C software/io/network/tests step.

Not in this PR

  • A common helper for the send loop. The tree now has three copies of the same loop: send_all in ftpd.cc (returns FTPTransferResult), FTPClient::send_all in ftp_client.cc (returns 0/−1), and this one. I'd suggest a follow-up in which both FTP sides use send_all.h. I left it out to keep this PR focused, as asked in UCI http target silently drops bytes #1039. Happy to do it if you agree.
  • A server that answers early (for example with 413) and then closes still gives "Failed to send request". Its answer is never read. That was already the case before.
  • connect() and DNS lookups have no timeout of their own.

HttpRequest::send_request handed each 128-byte chunk to one non-blocking
send() and took only a negative result as a failure. send() may take part
of a chunk and return the count, so the rest was dropped and the request
still counted as sent; with the send buffer full it failed outright.

send_all() sends until the chunk is taken, waiting while the server is not
reading, and reports a failure when send() fails or takes nothing.
connect_to_server now sets SO_SNDTIMEO and SO_RCVTIMEO to
HTTP_REQUEST_TIMEOUT_MS (30 s). A server that stops reading or never
answers now fails the exchange after 30 s without progress, instead of
blocking the task for good. The limit is per call: lwIP returns a partial
count when it runs out, so a send can take up to twice as long to fail.

Tests: software/io/network/tests (send-all with a model of lwip_send, and
send-all-red against the old loop), and test-slow-server in
target/pc/linux/test_http_target with the timeout at 2 s: 4 MB through
send_request to a loopback server with a 4K receive buffer that waits
before reading (fails on master), to one that never reads, and a request
whose server never answers (both fail without the timeouts).

Fixes GideonZ#1039
@JC-000

JC-000 commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

@chrisgleissner @barryw I think #1039 may need a deeper fix than either this PR or the one I closed (#1044); otherwise we are just moving the pain around.

The whole exchange runs on the single UCI Server task (command_intf.cc:59): HttpTarget calls connect_to_server, send_request and recv_response straight from parse_command (http_target.cc:885-896). While any of them waits, nothing else on the UCI moves:

  • no other UCI command runs, on any target, so a C64 program cannot do disk or network I/O in the meantime;
  • an abort from the C64 (CMD_ABORT_DATA, command_intf.cc:136) is not seen until the call returns;
  • after a C64 reset, the per-target c64_reset() cleanup (command_intf.cc:126-134) waits as well, so the next program inherits the stall.

Master already blocks there: DNS and connect() wait as long as lwIP takes, and recv() can wait forever. This PR bounds recv(), which helps, but it also makes send() blocking, with up to 30 s per call and no overall limit for a server that trickles bytes. That adds a new way for one slow server to freeze the C64's UCI.

The timeout length is not really the problem. A printer, the server in #1039, can legitimately stop reading for a while, so a short timeout would break the case being fixed. The problem is doing network waits on the UCI Server task at all. Any fix that stays on that task has to wait somewhere to deliver every byte, so it trades lost bytes for a frozen UCI. The exchange belongs on its own task: the HTTP command returns at once, the C64 polls for the result, and abort or reset cancels it. That is a larger change than #1039, but without it the fix here swaps silently lost bytes for a frozen UCI. Happy to open an issue for it if you agree.

Written with Claude Opus 5.5 in Claude Code.

@chrisgleissner

chrisgleissner commented Nov 1, 2026 •

Copy link
Copy Markdown
Collaborator

Hi @enver-haase and @JC-000, could you please discuss how to combine your respective fixes in order to ensure the best possible final fix?

Many thanks

@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 1, 2026
@JC-000

JC-000 commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

@chrisgleissner There is nothing left of #1044 to combine: its fix was the same send loop as this PR. I support merging #1043 as it is for #1039. It stops the silent byte loss on send, and it also bounds recv(), which on master has no timeout and can wait forever for a server that never answers. The 30 s limit per call, on progress rather than on the whole exchange, is right for the printer case.

The structural problem is separate and bigger, so I'll file it as its own issue once we have shown it on hardware. Briefly:

  • The hardware allows one UCI command in flight. While a target does network I/O inline on the UCI Server task, that task also cannot act on a C64 abort or a C64 reset (command_intf.cc:121-143). After a reset, the next program's first UCI command waits until the old exchange ends.
  • The fix I have in mind keeps the 0x31/0x32 bytes and behaviour exactly as they are. The target hands the exchange to a worker task, and the UCI task holds the C64 in BUSY as today but handles abort and reset at once. A result is delivered only if it still belongs to the pending command. This PR's send_all and timeouts become that worker's inner loop, so the two changes stack rather than conflict.
  • A correction to my earlier comment: "the HTTP command returns at once, the C64 polls" is wrong for the existing commands. With them the C64 still waits for its own request. Letting a program do other UCI I/O while a request is pending would need new commands, and that is Gideon's call.

Written with Claude Opus 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.

Merge-ready from my side. I found no defect in the change.

What I checked

  • lwIP semantics. With SO_SNDTIMEO set, lwip_send blocks and returns a partial count when the time runs out, or -1/EWOULDBLOCK if nothing was written (api_lib.c netconn_write_vectors_partly, api_msg.c do_writemore). LWIP_SO_SNDRCVTIMEO_NONSTANDARD is 0, so struct timeval is the right argument type. send_all handles both cases.
  • Host tests, PR head. send-all: 5 pass. send-all-red: 4 fail as intended. test-slow-server: 13 checks, 0 failures.
  • Host red/green per production change, reverting one at a time in http_request.cc. Old send(..., MSG_DONTWAIT) loop: the slow-receiver checks fail and the stalled-receiver check fails after 19 ms. Without SO_SNDTIMEO: the stalled-receiver check fails after 20 s. Without SO_RCVTIMEO: the silent-server check fails after 20 s. Each defect is covered by its own check.
  • send_all_test.cpp tests send_all.h against a model of lwip_send, and send_all_legacy.h is a copy of the old loop, so send-all-red does not exercise http_request.cc. test-slow-server is the check that fails when the production code is reverted, and it did in the runs above.

Hardware (Ultimate 64 Elite, UCI HTTP target, 6502 agent as UCI client, the host as the HTTP server)

firmware server result
3.15a (old send loop) 4 KB receive window, 2 s before first read exchange fails in 0.2 s, 503 SERVICE UNAVAILABLE, server receives no bytes
this PR, built from a0d06bb2e same 32768-byte body arrives complete and in order, 200 OK
this PR accepts, never reads (256 KB body) exchange ends after 62 s (two 30 s send waits, as described in the PR)
this PR reads everything, never answers connection dropped after 30.7 s

So both timeouts take effect on lwIP, not only on Linux sockets.

Only the Ultimate 64 was tested on hardware. The changed files are shared by all targets. The CI build job passed, and I built the Ultimate 64 firmware from this commit locally.

Points for the maintainer, not blockers

  • SO_RCVTIMEO applies to the wait for the first byte of the response. A server that needs more than 30 s to start answering, for example one that processes a large upload before replying, now fails where it used to wait indefinitely. This is a deliberate part of the change and the PR describes it. It is worth knowing when it is merged.
  • The 30 s limit applies to each send(). A server that accepts a few bytes every 29 s keeps the UCI task occupied. That is the structural issue JC-000 raised, and it needs the worker-task change. It is a separate follow-up.

The duplicate send loops in ftpd.cc and ftp_client.cc are better handled in the follow-up the author proposes.

struct timeval tv;
tv.tv_sec = HTTP_REQUEST_TIMEOUT_MS / 1000;
tv.tv_usec = (HTTP_REQUEST_TIMEOUT_MS % 1000) * 1000;
setsockopt(socket_fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv));

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.

Checked on an Ultimate 64 with this build: a server that reads the whole request and never answers closes the exchange 30.7 s after the connection was accepted, so SO_RCVTIMEO works on lwIP with a struct timeval.

This also limits the wait for the first response byte to 30 s. A server that processes a large upload before replying now fails where it used to wait forever. I think that is acceptable, but Gideon should know.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for measuring it on the U64. Agreed, and it is now in the description as a behaviour change, so Gideon sees it there.

*/
static inline int send_all(int socket_fd, const char *data, int len)
{
while (len > 0) {

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.

Confirmed against lwIP and on hardware. With a server that never reads, the first send() returns a partial count after 30 s and the next fails with nothing written. The UCI exchange ended 62 s after the connection was accepted. This matches the "up to twice as long" statement in the comment above.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for checking it on hardware. Your 62 s fits the two 30 s waits the comment describes.

@chrisgleissner

Copy link
Copy Markdown
Collaborator

Hi @enver-haase ,

hope you are well. I reviewed your PR and found a few minor points that could be enhanced. I left comments. Is this something that you'd consider tackling?

Thanks

@JC-000

JC-000 commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Following up on the structural point from earlier in this thread: it is now filed as #1049, with the fix in #1072 (and the network-target part in #1073).

Both are stacked on this PR rather than replacing it. #1072 uses this PR's send loop and timeouts as they are, and moves the whole HTTP exchange off the UCI Server task onto a worker task of its own, so a slow or stalled server no longer keeps the C64 waiting. The timeouts here stay the right bound on how long a dead server can hold one exchange.

#1072 and #1073 carry a copy of this PR's commit so they build today, and stay drafts until this one is merged. So the natural order is this PR first, then #1072, then #1073.

Written with Claude Opus 5.5 in Claude Code.

@chrisgleissner
chrisgleissner self-requested a review November 10, 2026 12:30

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

@enver-haase thanks for this. I checked the lwIP socket semantics: with SO_SNDTIMEO set, a blocking send returns the partial count, send_all loops correctly, and the slow-server host test fails on each of the three defects when I revert them one at a time.

Requesting changes on two points:

  1. Merge conflict. #1067 changed software/io/network/tests/Makefile and the branch no longer merges. Please rebase on master.
  2. Description. SO_RCVTIMEO of 30 s also ends an exchange with a server that sends nothing for 30 s, for example a slow API. Please say so in the description, since this is a behaviour change.

Once rebased I'm happy to merge.

@enver-haase

Copy link
Copy Markdown
Contributor Author

@chrisgleissner thanks for the review. A rebase would rewrite the commits under review, so I merged master into the branch instead, as CONTRIBUTING.md asks. The conflict in software/io/network/tests/Makefile was the new ref-pool and sys-arch tests next to send-all; both are kept. The description now states the behaviour change for SO_RCVTIMEO. The network and HTTP target suites and the U2+L firmware build pass locally.

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

Thanks. Master is merged, the SO_RCVTIMEO behaviour change is in the description and CI is green. Approving. #1072 and #1073 are stacked on this one and can follow once it is in.

@chrisgleissner chrisgleissner self-assigned this Nov 11, 2026
@chrisgleissner

Copy link
Copy Markdown
Collaborator

I'm assigning this to myself. I'm investigating whether it is merge ready by running the standard E2E profile locally on an Ultimate 64 Elite and an Ultimate II+L, with this PR combined with the other approved bug fixes, and comparing against master. I'll report the result here.

@chrisgleissner

Copy link
Copy Markdown
Collaborator

Test result for the batch this PR is part of (#1043, #1064, #1065, #1066, #1068, #1070).

Firmware: upstream master b57d894a0 plus these six PRs (build 5c1492908), compared with plain master.
Devices: Ultimate 64 Elite (JTAG, from RAM) and Ultimate II+L in a C64 Ultimate (flashed). Standard E2E profile, overlay mode.

Run Ultimate 64 Elite Ultimate II+L in C64 Ultimate
Batch build, full standard profile 67 of 72 suites passed 39 of 48 suites passed
Failed suites rerun after installing the missing host Python modules (tests/requirements.txt) all 5 pass 6 of 8 fail
Master, same suites all 5 pass the same 6 of 8 fail

@chrisgleissner
chrisgleissner merged commit ff0190f into GideonZ:master Nov 11, 2026
1 check passed
JC-000 added a commit to JC-000/1541ultimate that referenced this pull request Nov 12, 2026
Brings in GideonZ#1043 (send the whole HTTP request, give up on a stalled
server), GideonZ#1064, GideonZ#1065 and GideonZ#1068, which this change is stacked on.

Conflicts, all from this branch carrying the pre-merge form of GideonZ#1043:
- build.yml, test_http_target.cc, http_request.cc, send_all.h,
  send_all_test.cpp, test_http_target/Makefile: kept this branch's side,
  which is upstream's plus this PR's own change.
- io/network/tests/Makefile: took upstream's (only target order differed).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
JC-000 added a commit to JC-000/1541ultimate that referenced this pull request Nov 12, 2026
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>
chrisgleissner pushed a commit that referenced this pull request Nov 12, 2026
DO_EXCHANGE_OBJ and DO_EXCHANGE_RAW looked up the host, connected, sent the
request and read the response on the UCI Server task. While a server stalled,
an abort or a C64 reset waited with it: up to 14 s for DNS, ~18.5 s for a
connect to a host that does not answer, and 30 s per stalled send or receive
since #1043.

The target now renders the request on the UCI Server task as before, defers
the command and hands a job to a worker task, which it starts on the first
exchange. The job holds copies of all it needs: the request, host, port, a
copy of the header slot for the OBJ reply, and its own buffer for the raw
response header, which the receive wrote into the target's status buffer
before. Only the worker touches the job's socket, and it closes the socket
before it hands the job back. The reply is built on the UCI Server task from
the job, with the code that built it before, so the C64 sees the same bytes:
BUSY, then the same data and status, and 503 SERVICE UNAVAILABLE on failure.

An abort or a C64 reset cancels the job and frees the interface at once. The
worker waits on the network in 100 ms slices and checks for a cancel between
them: a connect is non-blocking with select(), and sends and receives use
100 ms SO_SNDTIMEO and SO_RCVTIMEO. #1043's limit stays a limit on time
without progress, now added up over the slices: a server that keeps moving,
like the printer in #1039, may take as long as it needs. send_part() is
#1043's send_all() loop, and also says how much went before a slice ran out.
DNS cannot be stopped; only the next exchange waits behind it.

The worker frees a job that was cancelled before it finished; a finished job
that was not delivered is freed on the UCI Server task. A cancelled job that
is still queued is not started. Four jobs can wait; a fifth exchange gets 503
at once.

Tests: test-uci-worker in target/pc/linux/test_http_target, with the timeout
at 2 s, aborts an exchange while the server stalls the response and while it
stops reading a 4 MB request: the UCI Server task is free in under 10 ms (2 s
before), the abort takes under 1 ms, the next exchange completes within one
slice of the abort, and the aborted exchange never reports a result. A server
that answers in pieces for longer than the timeout still completes. The
existing HTTP target tests now run each command as the UCI Server does,
including the reply to a deferred one. send_all_test covers send_part.

Toward the C64 never waiting on the CPU: no network wait is left on the
UCI Server task for target 6. The worker is network work at
PRIO_NETSERVICE and alone owns its socket; its 100 ms slices only decide
how soon it is free for the next exchange, never when the C64 is released.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

UCI http target silently drops bytes

3 participants