Skip to content

REST response bodies: release on abort, and test an abandoned reply - #1119

Draft
JC-000 wants to merge 3 commits into
GideonZ:masterfrom
JC-000:fix/leak-httpd
Draft

JC-000 wants to merge 3 commits into
GideonZ:masterfrom
JC-000:fix/leak-httpd

Conversation

@JC-000

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

Copy link
Copy Markdown
Contributor

Fixes #1115

Depends on GideonZ/MicroHttpServer#GideonZ/MicroHttpServer#8. The gitlink bump in this PR points at that commit; merge that PR first; this PR will then be updated to point at the merged commit. Neither half works alone: the routes.h change by itself is inert, and the submodule change by itself makes stream_body read into a NULL buffer.

The HTTP server now calls a response BodyCB with (ctx, NULL, -1) when a connection ends before the reply was sent to the end, so the body source can release its context. Until now it never did, and every reply abandoned by its client leaked the body: about 1.1 KB for a JSON reply, up to 64 KB for GET /v1/machine:readmem?address=0&length=65536, the whole VCD for a measure, and a static file's FILE*. Any LAN client can do this with no login (no network password set): send the request, read nothing, close. The next client that landed on the same connection slot could also be sent the previous client's unsent body behind its own 404.

Measured loss per aborted readmem: C64 Ultimate 66,994 B, Ultimate 64 Elite about 66,400 B; about 79 aborted requests (extrapolated) exhaust a fresh heap (about 5.3 MB free) on either.

Change

  • software/api/routes.h stream_body: on len < 0, delete the StreamRamFile and return 0. It serves the JSON, HTML and attachment replies of ResponseWrapper.
  • software/httpd: bump to the MicroHttpServer commit that releases res.BodyCB on close, resets the response on accept and closes the FILE* in filestream_out.
  • target/pc/linux/httpdbody (new), run by make host_tests: a loopback test of the server with a client that abandons a reply (AddressSanitizer, leak detection), and a test of ResponseWrapper's three reply kinds being abandoned.

Tests (host, container)

  • Old httpd with the routes.h change, httpd_body_test: FAIL response_body_released_on_client_abort (3 response bodies still allocated), FAIL next_client_gets_only_its_own_reply (8192 bytes instead of a 45 byte 404), FAIL static_file_closed_on_client_abort (1 file descriptor held).
  • Master routes.h, routes_body_test: AddressSanitizer memcpy-param-overlap (NULL buffer).
  • This branch: httpd_body: 0 failed, routes_body: 0 failed.
  • C64 Ultimate hardware (real readmem, real client aborts, both changes): about 0 B (+-16 B over 20 aborted reads) against -66,994 B per abort before; a full read changes nothing.
  • Ultimate 64 Elite hardware: +1,048 B over 5 aborted reads against -331,364 B before.

The 64 KB readmem figure was measured end to end on both devices; the JSON-reply, measure and static-file costs come from reading the code. The stale-body bleed is covered by the host loopback test only.

Written with Claude Sonnet 5.5 in Claude Code; reviewed adversarially by separate Claude Opus 5.5 sessions before posting.

🤖 Generated with Claude Code

The HTTP server (software/httpd, separate repo) now calls a response
BodyCB with (ctx, NULL, -1) when a connection ends before the reply was
sent to the end, so the body source can release its context. Until now
it never did, and every reply abandoned by its client leaked the body:
about 1.1 KB for a JSON reply, up to 64 KB for
`GET /v1/machine:readmem?address=0&length=65536`, the whole VCD for a
measure, and a static file's FILE*. Any LAN client can do this with no
login: send the request, read nothing, close. The next client that landed
on the same connection slot could also be sent the previous client's
unsent body behind its own 404.

This commit:
 - api/routes.h stream_body: on len < 0, delete the StreamRamFile and
   return 0. It serves the JSON, HTML and attachment replies of
   ResponseWrapper. Without it the new abort call would read into a NULL
   buffer.
 - software/httpd: bump to the MicroHttpServer commit that releases
   res.BodyCB on close, resets the response on accept and closes the
   FILE* in filestream_out. That commit must be merged in the httpd
   repo first; the gitlink here points at it.
 - target/pc/linux/httpdbody (new): a loopback test of the server with a
   client that abandons a reply, built with AddressSanitizer and leak
   detection, plus a test of ResponseWrapper's three reply kinds being
   abandoned. `make host_tests` runs it.

With the old httpd the socket test fails its three cases (three live
response bodies after three abandoned replies; the second client gets
8192 bytes of the first client's body instead of a 45 byte 404; one file
descriptor held). routes_body_test fails on its own without the
routes.h change (AddressSanitizer: memcpy overlap, the NULL buffer).

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

Copy link
Copy Markdown
Collaborator

@JC-000 I am keen to benefit from your fix. Would you mind resolving thr merge conflicts?

Also, only @GideonZ is able to merge the PR against the other repo which this PR relies on.

JC-000 and others added 2 commits November 23, 2026 13:33
Conflict resolved: Makefile host_tests target. Master added the
software/userinterface/tests run, this branch added target/pc/linux/httpdbody;
both lines kept. software/httpd gitlink untouched by master, stays at the
MicroHttpServer commit of this PR.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The CI image has a library preloaded. An AddressSanitizer binary started
that way refuses to run ("ASan runtime does not come first in initial
library list"), so `make host_tests` failed at httpdbody before the test
ran. Start both test programs with `env -u LD_PRELOAD`, as the other
AddressSanitizer tests in the repository already do. Leak detection stays on.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@JC-000

JC-000 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@chrisgleissner The conflicts are resolved: master is merged in, and CI is green at 5ccc41c. The CI failure that showed first was not from the merge. The new httpd body tests are built with AddressSanitizer and the CI image sets LD_PRELOAD, so they now start with env -u LD_PRELOAD like the other sanitizer tests in build.yml.

The PR stays in draft only because its submodule pointer needs GideonZ/MicroHttpServer#8 merged first. Once that is in, I will add a commit that points at the merged commit and mark this ready.

Written with Claude Fable 5.2 in Claude Code.

@chrisgleissner

Copy link
Copy Markdown
Collaborator

@JC-000 That other PR is merged now. Could you please update this PR as needed, then respond here once it is ready to be merged? Thank you.

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.

REST responses leak their body when the client disconnects before the reply is sent; the next client can receive that body

2 participants