Repository navigation
fix(iocp): send whole responses and close through the registry - #638
Open
edsontmarques wants to merge 1 commit into
Open
edsontmarques wants to merge 1 commit into
edsontmarques wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
TIocpRawResponsesent each piece of a response once and ignored how many bytes went out:WriteV(WSASend);A partial count truncated the response, and the connection was re-armed for keep-alive anyway.
TIocpStreamWriter.SendFullyalready looped, but on a failure it leftIsConnectedtrue, so an SSE handler kept writing.Two more defects on the same path:
FinalizeResponsecalledclosesocketdirectly, outside the registry. The context stayed inFConnectionswithProcessing = True, soCollectExpiredskipped it and onlyCloseAllfreed it. That could close the same, possibly reused, handle a second time.SendStream: the router builds an error response, andSendResponseappended its body to a response that was already delimited, on a connection that was then reused.Change
SendAll, which repeats until every byte is out.WriteVsends the rest of a partial gather through it.FRegistry.Close, and is never followed by a keep-alive re-arm.IsConnectedreads that state.SendFunction,GatherSendFunction), andTHorseProviderIOCP.ConnectionCountexposes the registry count. In production both functions are the Winsock ones.Tests
tests/iocp-httpruns 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):
seam-only.patchIsConnectedfalse)Connection: closeSendStreamseam-only.patchadds only the seam to the previous source, with no change in behaviour, so the negative control can be reproduced. Intests/run_compile_matrix.ps1on 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-Lengthfor empty streams and for 204/304.🤖 Generated with Claude Code