Repository navigation
Send the whole HTTP request, and give up on a stalled server (#1039) - #1043
Conversation
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
|
@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 (
Master already blocks there: DNS and 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. |
|
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 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 The structural problem is separate and bigger, so I'll file it as its own issue once we have shown it on hardware. Briefly:
Written with Claude Opus 5.5 in Claude Code. |
chrisgleissner
left a comment
There was a problem hiding this comment.
Merge-ready from my side. I found no defect in the change.
What I checked
- lwIP semantics. With
SO_SNDTIMEOset,lwip_sendblocks and returns a partial count when the time runs out, or -1/EWOULDBLOCKif nothing was written (api_lib.cnetconn_write_vectors_partly,api_msg.cdo_writemore).LWIP_SO_SNDRCVTIMEO_NONSTANDARDis 0, sostruct timevalis the right argument type.send_allhandles 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. Oldsend(..., MSG_DONTWAIT)loop: the slow-receiver checks fail and the stalled-receiver check fails after 19 ms. WithoutSO_SNDTIMEO: the stalled-receiver check fails after 20 s. WithoutSO_RCVTIMEO: the silent-server check fails after 20 s. Each defect is covered by its own check. send_all_test.cpptestssend_all.hagainst a model oflwip_send, andsend_all_legacy.his a copy of the old loop, sosend-all-reddoes not exercisehttp_request.cc.test-slow-serveris 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_RCVTIMEOapplies 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)); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Thanks for checking it on hardware. Your 62 s fits the two 30 s waits the comment describes.
|
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 |
|
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
left a comment
There was a problem hiding this comment.
@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:
- Merge conflict. #1067 changed
software/io/network/tests/Makefileand the branch no longer merges. Please rebase on master. - Description.
SO_RCVTIMEOof 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.
|
@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 |
|
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. |
|
Test result for the batch this PR is part of (#1043, #1064, #1065, #1066, #1068, #1070). Firmware: upstream master
|
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>
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>
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>
Fixes #1039.
HttpRequest::send_requestgave each 128-byte chunk to one non-blockingsend()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) callssend()until the chunk is sent. It waits while the server isn't reading, and fails whensend()fails or sends nothing.connect_to_serversetsSO_SNDTIMEOandSO_RCVTIMEOtoHTTP_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 inrecv(). 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.SO_RCVTIMEOalso 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
software/io/network/tests:send-all(a model oflwip_send: partial counts, full buffer, timeout)send-all-red: the same suite against the old looptarget/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 answersmasterthe first fails (send_requestreturns −1). WithoutSO_SNDTIMEOorSO_RCVTIMEOthe second or third fails.test-slow-serveris added to the existing CI step.send-allruns in the existingmake -C software/io/network/testsstep.Not in this PR
send_allinftpd.cc(returnsFTPTransferResult),FTPClient::send_allinftp_client.cc(returns 0/−1), and this one. I'd suggest a follow-up in which both FTP sides usesend_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.connect()and DNS lookups have no timeout of their own.