Skip to content

fix(autoresearch): read RP HTTP responses to completion, not to a deadline (#4173) - #4182

Closed
zackees wants to merge 1 commit into
masterfrom
fix/4173-http-content-length
Closed

zackees wants to merge 1 commit into
masterfrom
fix/4173-http-content-length

Conversation

@zackees

@zackees zackees commented Sep 7, 2026

Copy link
Copy Markdown
Member

Addresses the root cause found in #4173. Please do not merge on the timing improvement alone — see Validation gap below.

Root cause

runRpHttpRequestTest drained each response like this:

const uint32_t deadline_ms = millis() + 2000;
while (millis() - deadline_ms < 0 && (client.connected() || client.available()))

It exits on the 2 s deadline or on the peer closing. The request does send Connection: close, but client.connected() does not go false promptly enough to end the read — so the loop waits out the remaining deadline on every request, after the body has already arrived.

runNetClientTest issues 12 requests. That is ~2.2 s each and the 26.57 s measured on hardware, almost all of it idle. The caller allows max_wait=30.0 and the peer cycle makes two such calls, so a healthy first cycle has ~3.4 s of headroom.

That reframes #4173: not a resource exhausting at exactly cycle 7, but an operation marginal from cycle 1 that eventually loses a race it was always close to.

Change

Stop on completeness rather than silence:

  • parse headers, capture Content-Length, stop at the blank line
  • read exactly that many body bytes, then break without consulting connection state
  • keep the 2 s deadline as the ceiling for a server that never replies
  • keep draining past the 256-byte buffer so the socket is not abandoned mid-response

It also separates headers from body. response[256] previously held both, so a response with long headers could push the asserted fragment out of the window entirely — a failure mode independent of timing, and a plausible source of flaky content checks.

Measurements (RP2350W ↔ ESP32-C6 SoftAP)

per runNetClientTest cycles fully passing
before 26.57 s 1/1 measured
this change 8.1 – 20.4 s 6/9
focused re-run — 3/3 at 12/12 sub-tests

Validation gap — the reason this is not "ready"

I have not established the baseline pass rate. My only pre-change data point is a single ok=True cycle, which says nothing about how the unmodified build scores over nine. So I cannot currently tell whether 6/9 is:

  • equal correctness plus a ~2.3× speedup (worth landing), or
  • flakiness I introduced (should be rejected)

The comparison run was cut short by host memory pressure mid-deploy, and the board's flashed build became indeterminate, so I stopped rather than report a number I could not stand behind. Note the harness itself fails at cycle 7, so the baseline may well be flaky too.

What would settle it: flash unmodified master, run the same 9-cycle probe, compare pass rates. If baseline is also ~6/9, this is a clean win.

For context, an earlier attempt of mine — stopping on 150 ms peer idle — was 2.8× faster with ok=False on 7 of 8 cycles. I reverted it. That is why correctness, not speed, is the bar here.

Verification done

  • bash lint — clean
  • rp2350w builds; RPC smoke PASS on hardware
  • Both boards healthy throughout

🤖 Generated with Claude Code

https://claude.ai/code/session_01KkufoNxfnNRU9psT3R9F51

…dline (#4173)

`runRpHttpRequestTest` drained each response with

    const uint32_t deadline_ms = millis() + 2000;
    while (millis() - deadline_ms < 0 && (client.connected() || client.available()))

so it exited on the 2 s deadline or on the peer closing. The request does
ask for `Connection: close`, but `client.connected()` does not go false
promptly enough to end the read, so the loop waited out the remaining
deadline on every request after the body had already arrived.

`runNetClientTest` issues 12 requests, which is ~2.2 s each and the
26.57 s measured on hardware -- almost all of it idle. The caller allows
`max_wait=30.0` and the peer cycle makes two such calls, so the run had
~3.4 s of headroom on a healthy first cycle. That is the pressure behind
#4173: an operation marginal from the start rather than a resource that
exhausts itself at cycle 7.

Stop on completeness instead: parse the headers, capture Content-Length,
read exactly that many body bytes, then break without consulting the
connection state. The 2 s deadline stays as the ceiling for a server that
never replies, and the body is drained past the buffer so the socket is
not abandoned mid-response.

Also separates headers from body. `response[256]` held both, so a
response with long headers could push the asserted fragment out of the
window entirely -- a failure mode independent of timing.

Measured on hardware (RP2350W <-> ESP32-C6 SoftAP), 9 cycles:

    before:  26.57 s per runNetClientTest
    after:   8.1 - 20.4 s, 6/9 cycles fully passing
    focused: 3/3 cycles at 12/12 sub-tests

NOT yet validated against a baseline pass rate. The one pre-change data
point I have is a single ok=True cycle, which says nothing about how the
unmodified build scores over nine. The comparison run was cut short by
host memory pressure, and since the harness itself fails at cycle 7 the
baseline may be flaky too. Do not merge this on the timing alone.

bash lint clean; rp2350w builds and passes RPC smoke.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01KkufoNxfnNRU9psT3R9F51
@coderabbitai

coderabbitai Bot commented Sep 7, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@zackees

zackees commented Sep 7, 2026

Copy link
Copy Markdown
Member Author

Closing — the baseline comparison I flagged as missing has now been run, and it rejects this change.

Build Cycles passing HTTP time Spread
baseline (unmodified) 9/9 26.68 – 26.92 s < 0.25 s
this PR 6/9 8.1 – 20.4 s 12.3 s

Same probe, same C6 SoftAP, same nine cycles. The unmodified build is 100% reliable and near-deterministic — which makes sense in hindsight: waiting out a fixed 2 s per request is slow, but it is also completely insensitive to when data actually arrives.

So 6/9 is not "equal correctness at 2.3x the speed". It is flakiness I introduced, and this PR would trade a slow-but-correct path for a fast-but-unreliable one. Same verdict as my earlier 150 ms idle-stop attempt (2.8x faster, 1/8 passing), for the same underlying reason.

The 12.3 s spread is the tell. If Content-Length parsing were working on every response, timings would cluster. They do not, which suggests some responses on this path do not carry a usable Content-Length — chunked encoding, or a header my parse misses — and those fall through to the 2 s deadline while others return promptly. A correct implementation has to handle the chunked terminator as well, which is exactly the part I skipped.

The root-cause analysis stands and is worth keeping (recorded in #4173):

  • the drain loop exits on a 2 s deadline or peer close; Connection: close is requested but client.connected() does not go false promptly, so it waits out the deadline after the body has arrived
  • 12 requests x ~2.2 s = the 26.57 s measured, almost all idle
  • with max_wait=30.0 and two calls per peer cycle, a healthy cycle has ~3.2 s of headroom

One thing the baseline run also settles: at 26.8 s the content checks pass every time, so the cycle-7 failure in --net-peer is not these assertions failing. It is the timing budget, or the second (C6 -> RP) direction. That narrows #4173 further.

Not merging this. The header/body separation in it is independently correct and could be salvaged, but it should not ride along with the completion logic that is not right yet.

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

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant