Skip to content

Fix HttpRequest::send_request losing or aborting large requests - #1044

Closed
JC-000 wants to merge 3 commits into
GideonZ:masterfrom
JC-000:fix/1039-send-all
Closed

JC-000 wants to merge 3 commits into
GideonZ:masterfrom
JC-000:fix/1039-send-all

Conversation

@JC-000

@JC-000 JC-000 commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

Fixes #1039.

Problem

HttpRequest::send_request() sent the request in 128-byte chunks with send(..., MSG_DONTWAIT) and only checked r < 0. In lwIP a non-blocking send that only partly fits returns the short positive count (lwip_send -> netconn_write_partly, lwip 26a22151f), so the rest of the chunk was silently dropped. If nothing fits it returns -1/EWOULDBLOCK, which aborted the request (the UCI HTTP target answers 503 SERVICE UNAVAILABLE). Either way a request larger than the 16 KB send buffer, sent to a slow peer, was lost or truncated.

Fix

Send on the blocking socket with SO_SNDTIMEO (10 s; LWIP_SO_SNDTIMEO is already enabled) and loop until each chunk is fully written. The timeout applies to each send(), so a request fails after 10 s without progress; a slow peer that keeps accepting bytes keeps the UCI server task busy, as a slow upload does today. A send error or timeout returns -1, which HttpTarget::cmd_exchange already reports as 503. recv_response has no receive timeout; that is unchanged and out of scope. One function in one file.

Tests

Host: make -C target/pc/linux/test_http_target test-send-all (appended to the existing CI line). A loopback peer stalls before reading a 16 MiB request: all bytes must arrive in order; a reset connection and a peer that never reads (within the timeout) must fail the request. Master fails it (send_request returns -1 after 2.6 MB); the branch passes. Mutants that restore MSG_DONTWAIT, ignore send errors, or drop the timeout are killed. The other test_http_target targets still pass (Ubuntu 24.04, ASan/UBSan).

Hardware: the UCI HTTP target POSTs a 196,608-byte body to a throttled server (stalls 3 s, then drains) that counts the bytes; script and result files are on request. The RED images are bench builds carrying other unmerged work, whose http_request.cc and http_target.cc are identical to master; the GREEN image has this fix plus the same other work.

Device Image Bytes received / sent C64 status
U64E fdb59b059 (no fix) 66,195 / 196,608 503
U64E 218e36a52 (fix), 3 runs 196,608 / 196,608, in order 200
C64U 1cd28abb1 (no fix) 66,887 / 196,608 503
C64U 218e36a52 (fix), 3 runs 196,608 / 196,608, in order 200

Hardware reproduced the EWOULDBLOCK path; the short-count path is covered by the loop and by lwIP's code, not observed on hardware.

Written with Claude Sonnet 5.5 in Claude Code.

JC-000 and others added 3 commits November 1, 2026 08:49
HttpRequest::send_request() sent 128-byte chunks with MSG_DONTWAIT and
only checked for r < 0, so a short send silently dropped the rest of the
chunk, and a full send buffer (EWOULDBLOCK) aborted the request.

Send with a blocking socket and SO_SNDTIMEO, and loop until every byte
of each chunk is written. A timeout or any send error fails the request,
which the UCI HTTP target reports as 'not available'.

Fixes GideonZ#1039

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Add a loopback test (make test-send-all) with a peer that stalls before
reading, so the send buffer fills: all 16 MiB must arrive in order. A
reset connection and a peer that never reads must fail the request,
the latter within the send timeout.

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

JC-000 commented Nov 1, 2026

Copy link
Copy Markdown
Contributor Author

Closing in favour of #1043, which came first and also adds the receive timeout.

In case it helps the review of #1043: we measured the bug on hardware with the UCI HTTP target POSTing a 196,608-byte body to a throttled server. Unfixed, the U64E delivered 66,195 bytes and the C64U 66,887, both ending in 503. With a blocking send loop like the one in both PRs, both devices delivered all 196,608 bytes in order, 3 of 3 runs each. We can run the same check on #1043 on a later bench flash if that is useful.

Written with Claude Opus 5.5 in Claude Code.

@chrisgleissner

Copy link
Copy Markdown
Collaborator

Thanks @JC-000 Much appreciated you also investigated this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UCI http target silently drops bytes

2 participants