Skip to content

Accept a TF card upload whose data arrives before the Mini checks its memory - #103

Merged
adamshiervani merged 3 commits into
mainfrom
fix-upload-admission-queued-body
Dec 4, 2026
Merged

adamshiervani merged 3 commits into
mainfrom
fix-upload-admission-queued-body

Conversation

@adamshiervani

@adamshiervani adamshiervani commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Problem

A TF card upload from the web page can fail with HTTP 503 Insufficient memory headroom while 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_sync asks 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

  • A memory request can name the part of its PSRAM that is allocated already, 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.
  • The upload handler reads the bytes queued on its socket with FIONREAD, capped at the window, and names them in its requests. CONFIG_LWIP_SO_RCVBUF=y turns FIONREAD on.

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 e34c1bb and of this branch:

Case Base This branch
Page upload with session and Tailscale on, 128 KiB to 16 MiB 8 of 8 refused 8 of 8 accepted
256 KiB queued, about 150 KB over the admission point 3 of 3 refused 3 of 3 accepted
64 KiB body, about 150 KB under the admission point 2 of 2 refused 2 of 2 refused
  • The new integration test upload_queued_body.test.mjs builds both memory states with held connections. Its first test fails on the base build and passes here.
  • The new host test test_queued_body_admission.py runs the policy code at the exact edge and fails without the policy change.
  • mounted_image, update_card_image, every upload_* file and the new test pass, 14 of 14. The host suite passes, 82 suites.
  • tools/combined_load/run.sh with the short list and Tailscale passes.
  • Wi-Fi Mini, bench build of this branch: the integration tests whose names contain upload or image pass, 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

  • No test builds the double count on the Wi-Fi board. Its window limits the queued body to 64 KiB.
  • The discard diagnostic handler has the same double count. It is off by default.
  • An upload is still refused when free PSRAM is under the admission point, for example at a Tailscale peak: its tasks peaked at 1,961.7 KiB of PSRAM in the walk.

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_bytes on jetkvm_memory_policy_request_t. The PSRAM floor check uses psram_bytes - psram_allocated_bytes for admission; the reservation still accounts for the full psram_bytes so 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=y enables the ioctl.

Tests: host static test_queued_body_admission.py (wired into run_static_test.sh) and integration upload_queued_body.test.mjs for 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

    • HTTP upload memory checks now account for request data already buffered by the connection, improving admission decisions when memory is limited.
    • Uploads that exceed available memory continue to be rejected with an insufficient-memory response.
  • Tests

    • Added coverage for uploads that fit within available memory and for requests that exceed the admission limit.

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

coderabbitai Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Changes: Queued upload admission

Layer / File(s) Summary
Allocated-byte policy and checks
components/jetkvm_memory_policy/include/jetkvm_memory_policy.h, components/jetkvm_memory_policy/jetkvm_memory_policy.c, components/jetkvm_memory_policy/tests/*
The request adds psram_allocated_bytes. Validation rejects values greater than psram_bytes, and admission subtracts allocated bytes while retaining the full reservation. Static tests check admission and reservation behavior.
HTTP socket-byte accounting
components/jetkvm_virtual_media_http/jetkvm_virtual_media_http.c, sdkconfig.defaults
Upload handling reads queued socket bytes with FIONREAD, caps the count, and supplies it to each upload reservation path. The defaults enable lwIP socket receive-buffer support.
Queued-body integration coverage
tools/integration/tests/upload_queued_body.test.mjs
Integration tests check that an upload succeeds above the admission margin and returns 503 with an insufficient-memory message below it.

Possibly related PRs

  • jetkvm/mini#67: Updates memory-policy reservation accounting for bytes already allocated to an open download connection, using a related policy contract.

Change: Bug fix

Merge Risk: Low · up to e91ae

TF card uploads whose data is already queued are no longer refused when PSRAM is available. In the uncommon case of very small network frames, the memory check may credit slightly more memory than is actually freed, so an upload could be admitted a little below the intended safety margin. Normal full-size uploads are unaffected, so this is low risk to merge.

Security architecture risk: Minimal · up to e91ae

TF card uploads can proceed when part of their receive window is already in memory. JetKVM still reserves the full window against other workloads and applies the original memory check when it cannot measure the queued bytes.

Security review details

Security Blast Radius

  • inferred — The changed decision affects availability on the Mini sharing the PSRAM pool. The upload can qualify when it previously would not, but the credit cannot reduce the active reservation counted against another owner.

Trust Boundaries and Controls

  • observed — A client can influence how many TCP bytes arrive before admission, but it does not supply the credit as a request field. The worker derives it from its own socket, and the policy validates the bound before subtraction.

Dependencies

  • inferred — The new credit relies on lwIP's socket count representing memory already deducted from the free PSRAM measured by the policy. The firmware defaults enable receive-buffer support and request preferential PSRAM allocation for lwIP; the inspected source does not establish allocator placement for all counted buffers on every board and build.

Data and control flow

  • observed — After claiming the upload, the worker measures the current request socket with FIONREAD, caps the result at the configured receive window, and passes it into each upload reservation shape before reading the body.

Resilience and Maintainability Implications

  • observed — A failed or empty socket measurement gives zero credit: the upload can still receive the original headroom refusal. Refusal closes the request without changing the file and cleans up its claim. A new request samples its own socket rather than inheriting credit.
  • observed — Allocation fallback releases the first reservation before re-admission and before any body read. Once streaming starts, the ingress lease retains the full receive-window charge until writer cleanup. Interrupted transfers use the existing owned abort path, retaining the incomplete file for resume rather than carrying credit into recovery.

Rollout and rollback

  • inferred — The improvement depends on the firmware build enabling lwIP receive-buffer support, not on a browser or operator setting. Rolling back to the merge-base behavior restores the full admission charge. The memory policy still decides whether to admit the upload, and the existing access checks still control whether it may finish.
Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 7af909a4-894c-4651-bb75-4955eb33566e
Commits

Reviewing files that changed from the base of the PR and between e34c1bb and e91ae4a.

Files selected for processing: 7

  • components/jetkvm_memory_policy/include/jetkvm_memory_policy.h
  • components/jetkvm_memory_policy/jetkvm_memory_policy.c
  • components/jetkvm_memory_policy/tests/run_static_test.sh
  • components/jetkvm_memory_policy/tests/test_queued_body_admission.py
  • components/jetkvm_virtual_media_http/jetkvm_virtual_media_http.c
  • sdkconfig.defaults
  • tools/integration/tests/upload_queued_body.test.mjs

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@adamshiervani
adamshiervani merged commit 22e35fc into main Dec 4, 2026
5 checks passed
@adamshiervani
adamshiervani deleted the fix-upload-admission-queued-body branch December 4, 2026 13:54
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.

1 participant