Skip to content

fix(iocp): send whole responses and close through the registry - #638

Open
edsontmarques wants to merge 1 commit into
HashLoad:masterfrom
edsontmarques:fix/iocp-send-whole-response
Open

edsontmarques wants to merge 1 commit into
HashLoad:masterfrom
edsontmarques:fix/iocp-send-whole-response

Conversation

@edsontmarques

Copy link
Copy Markdown

Problem

TIocpRawResponse sent each piece of a response once and ignored how many bytes went out:

  • WriteV (WSASend);
  • the headers;
  • the stream loop;
  • the body sent after the headers.

A partial count truncated the response, and the connection was re-armed for keep-alive anyway. TIocpStreamWriter.SendFully already looped, but on a failure it left IsConnected true, so an SSE handler kept writing.

Two more defects on the same path:

  • Connections without keep-alive: FinalizeResponse called closesocket directly, outside the registry. The context stayed in FConnections with Processing = True, so CollectExpired skipped it and only CloseAll freed it. That could close the same, possibly reused, handle a second time.
  • Exception after SendStream: the router builds an error response, and SendResponse appended its body to a response that was already delimited, on a connection that was then reused.

Change

  • Every send goes through SendAll, which repeats until every byte is out. WriteV sends the rest of a partial gather through it.
  • A failed send marks the response, closes the connection through FRegistry.Close, and is never followed by a keep-alive re-arm. IsConnected reads that state.
  • A response without keep-alive is also closed through the registry.
  • Once the headers are out, a body that shows up afterwards closes the connection instead of being appended, and the work item no longer sends a 500.
  • Test seam: the low-level sends are replaceable class functions (SendFunction, GatherSendFunction), and THorseProviderIOCP.ConnectionCount exposes the registry count. In production both functions are the Winsock ones.

Tests

tests/iocp-http runs the real IOCP provider on loopback and talks raw HTTP. It swaps the sends to force partial counts and failures, which a blocking loopback socket almost never produces on its own.

On Delphi 10.2 (Win32):

Check With this PR Previous source + seam-only.patch
CA1 partial send (1 byte per send: 1 MiB text, 1 MiB stream, SSE event) pass fail: response truncated
CA2 failed send (connection closed, registry count back, IsConnected false) pass fail: connection kept open
CA3 200 requests with Connection: close pass fail: 202 contexts in the registry, expected 2
CA4 exception after SendStream pass fail: bytes after the response
CA6 load, 32 keep-alive clients × 50 rounds × 2 responses of 8 MiB pass, 31 s measure only

seam-only.patch adds only the seam to the previous source, with no change in behaviour, so the negative control can be reproduced. In tests/run_compile_matrix.ps1 on Delphi 10.2, IOCP and IOCP+Radix compile. The HttpSys scenarios fail there for an unrelated, existing reason.

A follow-up PR, stacked on this one, will fix Content-Length for empty streams and for 204/304.

🤖 Generated with Claude Code

The IOCP response sent each piece once and ignored how many bytes went
out: WriteV (WSASend), the headers, the stream loop and the body sent
after the headers. A partial count truncated the response. The stream
writer's SendFully looped, but on a failure it left IsConnected true.

Every send now goes through SendAll, which repeats until every byte is
out; WriteV sends the rest of a partial gather through it. A failed
send marks the response, closes the connection through the registry
and is never followed by a keep-alive re-arm; IsConnected reads that
state. A response without keep-alive is also closed through the
registry, so its context no longer stays there until CloseAll.

Bytes that arrive after a streamed response (the error response the
router builds when the handler raises after SendStream) are no longer
appended to it: the connection is closed instead.

The low-level sends are replaceable class functions and the registry
count is readable, so tests/iocp-http forces partial sends and failures
on loopback. seam-only.patch adds just that seam to the previous source
for the negative control.

tests/iocp-http passes on Delphi 10.2 (CA1 to CA4, plus CA6 as a load
measure) and, against the previous source with only the seam applied,
fails CA1 to CA4.

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

This branch has not been deployed

No deployments
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.

2 participants