Repository navigation
Accept a TF card upload whose data arrives before the Mini checks its memory - #103
Conversation
A request's psram_allocated_bytes is the part of psram_bytes that free PSRAM no longer has. Admission charges psram_bytes less that part, so the part counts once. The reservation still holds all of psram_bytes, and other owners are judged against all of it.
The upload's admission runs when the HTTP server reads the request head. Body bytes that arrived before then wait in lwIP's receive queue, one PSRAM block per frame, so free PSRAM no longer has them, and the admission charged them again as part of the 1.25 MiB TCP window. With a browser session and Tailscale on, a bench build refused uploads from about 100 KiB of queued body, while the same upload with its body sent 1.5 s later was admitted. The handler reads the queued bytes with FIONREAD (CONFIG_LWIP_SO_RCVBUF) and names them as allocated in its request. The reservation still holds the whole window, because lwIP can fill it again once the body is read.
A request whose head is not complete holds the HTTP server task, so the upload's whole body and the bytes of up to 4 more connections wait in lwIP when the server reads the upload's head. Those connections take free PSRAM to a set margin over the upload's admission point: 128 KiB over it, the upload with 256 KiB queued is admitted; 128 KiB under it, an upload is refused.
|
No actionable comments were generated in the recent review. Walkthrough
Change: Bug fix Merge Risk:
|
Problem
A TF card upload from the web page can fail with HTTP 503
Insufficient memory headroomwhile the Mini has enough free PSRAM. On an Ethernet Mini with a browser session and Tailscale on, 8 of 8 uploads from 128 KiB to 16 MiB were refused. Release 0.5.23 refuses them too.Cause
upload_handler_syncasks for memory when the HTTP server has read the request head. It charges the whole TCP receive window, 1,310,720 B on Ethernet, against free PSRAM. The page sends the file in the same request, so part of the body is in lwIP's receive queue at that moment. Those frames are in PSRAM and are part of the window. Free PSRAM has lost them already, and the window charge takes them a second time.With a session and Tailscale on, a bench build has 113 to 131 KB of free PSRAM over the admission point. A queued body of 98,304 B was accepted and one of 106,496 B was refused.
Fix
psram_allocated_bytes. Admission charges the request less that part. The reservation still holds the whole request, so other owners are judged against the full window.FIONREAD, capped at the window, and names them in its requests.CONFIG_LWIP_SO_RCVBUF=yturnsFIONREADon.The credit is the payload byte count. The frames hold about 1.1 B of PSRAM per payload byte, so the check stays on the safe side of the floor. Lowering the reservation instead would under-charge other owners while the upload runs.
Testing
Ethernet Mini, bench builds of
e34c1bband of this branch:upload_queued_body.test.mjsbuilds both memory states with held connections. Its first test fails on the base build and passes here.test_queued_body_admission.pyruns the policy code at the exact edge and fails without the policy change.mounted_image,update_card_image, everyupload_*file and the new test pass, 14 of 14. The host suite passes, 82 suites.tools/combined_load/run.shwith theshortlist and Tailscale passes.uploadorimagepass, 70 of 70. The two new tests skip there, because the 64 KiB window of that board cannot hold their queued body.Not in this PR
Note
Medium Risk
Touches global PSRAM admission for HTTP storage uploads; incorrect credits could admit under pressure or still refuse valid uploads, though reservations remain full-window and validation caps allocated bytes.
Overview
Fixes false 503 Insufficient memory headroom on TF card uploads when the browser sends the body in the same POST and lwIP has already buffered part of it in PSRAM. Admission used to charge the full TCP receive window against free PSRAM even though queued frames had already consumed that memory (double count).
Memory policy adds
psram_allocated_bytesonjetkvm_memory_policy_request_t. The PSRAM floor check usespsram_bytes - psram_allocated_bytesfor admission; the reservation still accounts for the fullpsram_bytesso coexistence with other owners stays conservative.HTTP upload reads queued socket bytes via
FIONREAD(queued_request_bytes, capped at the HTTP TCP window) and passes them into primary and fallback upload memory requests.CONFIG_LWIP_SO_RCVBUF=yenables the ioctl.Tests: host static
test_queued_body_admission.py(wired intorun_static_test.sh) and integrationupload_queued_body.test.mjsfor admit vs refuse at the memory edge with a pre-queued body.Reviewed by Cursor Bugbot for commit e91ae4a. Bugbot is set up for automated code reviews on this repo. Configure here.
Summary by CodeRabbit
Bug Fixes
Tests