diff --git a/.gitignore b/.gitignore index 017737ec..c8f67e6a 100644 --- a/.gitignore +++ b/.gitignore @@ -99,3 +99,4 @@ benchmarks/results/ # FPC test binaries (Linux builds have no extension, so *.exe misses them) tests/src/FPCHttpKeepAliveServer +tests/src/FPCHttpKeepaliveTest diff --git a/samples/tests/Horse.Indy.TestRoutes.pas b/samples/tests/Horse.Indy.TestRoutes.pas index 371a51dd..de312eac 100644 --- a/samples/tests/Horse.Indy.TestRoutes.pas +++ b/samples/tests/Horse.Indy.TestRoutes.pas @@ -406,6 +406,32 @@ procedure RegisterTestRoutes; end ); + // ── FIX-HTTPSYS-CORS-NIL regression: header set ONLY via RawWebResponse ─── + // Reproduces the http.sys AV: when a response header arrives only through + // Res.RawWebResponse.SetCustomHeader and AddHeader is NEVER called (exactly + // what Horse.CORS does), the shadow CustomHeaders dict stays nil while the + // raw header count is > 0 — so THttpSysRawResponse.SendResponse's for..in + // enumerated a nil dict and AV'd. Provider-agnostic: Indy/Epoll/IOCP always + // passed; this route is the guard for HttpSys. + THorse.Get('/raw/setheader-only', + procedure(Req: THorseRequest; Res: THorseResponse) + var + LRaw: TRawWebRes; + begin + LRaw := Res.RawWebResponse; + if not Assigned(LRaw) then + begin + Res.ContentType('application/json; charset=utf-8').Status(500) + .Send('{"error":"RawWebResponse is nil"}'); + Exit; + end; + // Deliberately NO Res.AddHeader anywhere — the shadow header dict must + // remain nil so the HttpSys nil-enumeration path is exercised. + LRaw.SetCustomHeader('X-Raw-Only', 'raw-only-ok'); + Res.Send('raw-only-ok'); + end + ); + // ── PATCH-REQ-9: double-read idempotency ───────────────────────────────── THorse.Post('/echo/body-twice', procedure(Req: THorseRequest; Res: THorseResponse) diff --git a/samples/tests/HorseNetHttpTestClient.dpr b/samples/tests/HorseNetHttpTestClient.dpr index 77dacbf6..81697d8b 100644 --- a/samples/tests/HorseNetHttpTestClient.dpr +++ b/samples/tests/HorseNetHttpTestClient.dpr @@ -802,6 +802,20 @@ begin (R.StatusCode = 200) and (R.Body = 'pong'), Format('%d / %s', [R.StatusCode, R.Body])); + // ── 37 ───────────────────────────────────────────────────────────────────── + // FIX-HTTPSYS-CORS-NIL: a header set ONLY via RawWebResponse.SetCustomHeader + // (no AddHeader) must not AV the HttpSys provider. Pre-fix: http.sys + // SendResponse enumerated a nil shadow dict -> AV / dropped connection. + // Post-fix: 200 + header. Passes on every other provider too. + Section('37 GET /raw/setheader-only (FIX-HTTPSYS-CORS-NIL - SetCustomHeader, no AddHeader)'); + DoReq(LClient, 'GET', BASE_URL + '/raw/setheader-only', nil, nil, R); + Check('status 200', R.StatusCode = 200, IntToStr(R.StatusCode)); + Check('body = "raw-only-ok"', R.Body = 'raw-only-ok', R.Body); + if Assigned(R.Response) then + Check('X-Raw-Only: raw-only-ok', + R.Response.HeaderValue['X-Raw-Only'] = 'raw-only-ok', + R.Response.HeaderValue['X-Raw-Only']); + finally LClient.Free; end; diff --git a/src/Horse.Provider.HttpSys.pas b/src/Horse.Provider.HttpSys.pas index 47eb059e..9719dffb 100644 --- a/src/Horse.Provider.HttpSys.pas +++ b/src/Horse.Provider.HttpSys.pas @@ -1521,6 +1521,11 @@ procedure THttpSysRawResponse.SendResponse(const ARes: THorseResponse); Inc(LHdrIdx); end; {$ELSE} + // FIX-HTTPSYS-CORS-NIL: the shadow CustomHeaders dict is nil when headers arrived only via + // RawWebResponse (e.g. Horse.CORS calls Res.RawWebResponse.SetCustomHeader, never AddHeader), + // yet the block is entered because LRawHeaderCount/LRepeatCount > 0 — so guard the for..in. + // (The FPC branch above is already guarded by LHeaderCount, which is 0 when the dict is nil.) + if LHeadersList <> nil then for LPair in LHeadersList do if not SameText(LPair.Key, 'Set-Cookie') then begin diff --git a/tests/src/FPCHttpKeepaliveTest.dpr b/tests/src/FPCHttpKeepaliveTest.dpr new file mode 100644 index 00000000..e26ea304 --- /dev/null +++ b/tests/src/FPCHttpKeepaliveTest.dpr @@ -0,0 +1,199 @@ +program FPCHttpKeepaliveTest; + +{ Regression test for the FPC keep-alive latency stall (HashLoad/horse#562, + fixed upstream in 3.3.4 by PR #563). + + ROOT CAUSE — Nagle interacting with the peer's delayed ACK, NOT a poll + interval. fphttpserver writes each response as two send() calls: the header + block, then the body. TCP_NODELAY was never set on the accepted socket, so + Nagle held the small second write until the peer ACKed the first, and the + peer — having nothing to send — waited out its ~40 ms delayed-ACK timer. + + Established by wire capture: the body segment leaves 11-14 us after the ACK + arrives, three times, while the ACK delay itself varies 42.5-43.9 ms. The + body tracks the ACK to microseconds across a varying delay, which only Nagle + produces. An earlier reading of this file blamed "a fixed ~40 ms select() + poll interval"; that was wrong, and this header used to state it as fact. + The server IS idle in select() for ~43 ms, but waiting for the next request, + which the client cannot send because it is still waiting for a body Nagle is + holding. Cause and effect inverted. + + THE FIX THIS GUARDS — Horse.Provider.FPC.HTTPApplication sets TCP_NODELAY on + each accepted socket via THorseNoDelaySocketHandler.Accept, wired through + OnGetSocketHandler, and enables KeepConnections together with a positive + KeepConnectionTimeout. Both halves are required: KeepConnections alone + leaves WaitUntil at 0, so the connection thread serves exactly one request + and exits. + + WHY KeepConnection := True BELOW IS LOAD-BEARING — the stall only appears on + a REUSED connection. TFPHTTPClient.KeepConnection defaults to False, so + without setting it every request gets a fresh socket, and Linux starts every + socket in quickack mode: the first ACK is immediate, Nagle never engages, and + this test passes whether or not the fix is present. That is precisely how it + was written before, and it made the test decorative. + + WHAT THIS CHECKS + 1. REQUEST_COUNT sequential GETs, all on ONE connection, each completing + within STALL_THRESHOLD_MS. The stall is ~44 ms when present and well + under 1 ms when fixed, so 35 ms separates the two cleanly while leaving + headroom over scheduling jitter. + 2. Every response is HTTP 200 with the body 'pong'. + 3. No hang — the process exits rather than blocking. + + Complements tests/fpc_keepalive_regression.py, which upstream added with the + fix. That one drives FPCHttpKeepAliveServer.dpr from Python; this one is + self-contained Pascal and runs where grpc_tools/Python are not installed. + + Build (from horse/tests/src/) — TRUNK, not the distro compiler. A bare `fpc` + finds 3.2.2 on Ubuntu and the version guard below rejects it: + + U=/usr/local/fpc-trunk/lib/fpc/3.3.1/units/x86_64-linux + mkdir -p /tmp/ka-test + /usr/local/fpc-trunk/bin/fpc -MDelphi -Sh -B -n \ + -Fu"$U/*" -Fu"$U" -Fu../../src \ + -FU/tmp/ka-test -FE/tmp/ka-test FPCHttpKeepaliveTest.dpr + /tmp/ka-test/FPCHttpKeepaliveTest + + The -n and explicit -Fu are not optional: without them trunk reads + /etc/fpc.cfg, finds the distro's 3.2.2 units and dies with + "PPU Invalid Version 207 expecting 208" — which reads as a missing unit but + is really a wrong-compiler unit. + + No HORSE_* define is needed: on FPC with no provider define, Horse.pas + selects Horse.Provider.FPC.HTTPApplication automatically. Indy never enters + the picture on FPC. Requires FPC >= 3.3.1 — the provider's keep-alive and + TCP_NODELAY code sits behind an $IF FPC_FULLVERSION >= 30301 guard, so on + 3.2.2 it is not merely untested, it is not compiled. + + (That guard is written without its braces on purpose: a directive's closing + brace would end this comment early and the prose after it would be compiled.) +} + +{$MODE DELPHI}{$H+} + +// Refuse to build on a compiler where the code under test does not exist. +// The provider's keep-alive and TCP_NODELAY handling sit behind a +// FPC_FULLVERSION >= 30301 guard, so on 3.2.2 the server never keeps a +// connection open. This test sets KeepConnection := True on the client, so +// the run then dies with "Error reading data from socket" on the third +// request - a socket error that says nothing about the real cause. Failing +// at compile time names it instead. +{$IF FPC_FULLVERSION < 30301} + {$FATAL This test requires FPC 3.3.1+. On 3.2.2 the provider's keep-alive and TCP_NODELAY code is not compiled at all, so there is nothing here to test.} +{$ENDIF} + +uses + {$IFDEF UNIX}cthreads,{$ENDIF} + SysUtils, Classes, fphttpclient, + Horse, Horse.Commons; + +const + TEST_PORT = 9901; + REQUEST_COUNT = 30; + STALL_THRESHOLD_MS = 35; { any single request above this = stall detected } + SERVER_STARTUP_MS = 1500; { allow fphttpserver to finish binding } + +procedure PingHandler(Req: THorseRequest; Res: THorseResponse; Next: TNextProc); +begin + Res.Send('pong'); +end; + +type + TServerThread = class(TThread) + protected + procedure Execute; override; + end; + +procedure TServerThread.Execute; +begin + FreeOnTerminate := True; + try + THorse.Listen(TEST_PORT, '127.0.0.1'); + except + on E: Exception do + Writeln('Server: ', E.Message); + end; +end; + +var + LClient: TFPHTTPClient; + LStart: QWord; + LElapsed, LMax, LTotal: QWord; + LBody: string; + LStatusCode: Integer; + LFailed: Boolean; + I: Integer; +begin + Writeln('FPC keep-alive latency regression test (horse#562)'); + Writeln(' requests : ', REQUEST_COUNT, ' (one reused connection)'); + Writeln(' stall threshold: ', STALL_THRESHOLD_MS, ' ms'); + + THorse.Get('/ping', PingHandler); + + TServerThread.Create(False); + Sleep(SERVER_STARTUP_MS); + + LClient := TFPHTTPClient.Create(nil); + { Load-bearing, not a tuning knob. Defaults to False, and with a fresh + connection per request Linux quickack keeps every first ACK immediate, so + Nagle never engages and this test passes with or without the fix. } + LClient.KeepConnection := True; + LFailed := False; + LMax := 0; + LTotal := 0; + try + for I := 1 to REQUEST_COUNT do + begin + LStart := GetTickCount64; + try + LBody := LClient.Get(Format('http://127.0.0.1:%d/ping', [TEST_PORT])); + LStatusCode := LClient.ResponseStatusCode; + except + on E: Exception do + begin + Writeln('FAIL: request ', I, ' raised ', E.ClassName, ': ', E.Message); + LFailed := True; + Continue; + end; + end; + + LElapsed := GetTickCount64 - LStart; + LTotal := LTotal + LElapsed; + if LElapsed > LMax then + LMax := LElapsed; + + if LStatusCode <> 200 then + begin + Writeln(Format('FAIL: request %2d status=%d body="%s"', [I, LStatusCode, LBody])); + LFailed := True; + end + else if LBody <> 'pong' then + begin + Writeln(Format('FAIL: request %2d body="%s" (expected "pong")', [I, LBody])); + LFailed := True; + end + else if LElapsed > STALL_THRESHOLD_MS then + begin + Writeln(Format('FAIL: request %2d %3d ms > %d ms -- Nagle/delayed-ACK stall detected', + [I, LElapsed, STALL_THRESHOLD_MS])); + LFailed := True; + end; + end; + finally + LClient.Free; + end; + + Writeln(Format(' max latency : %d ms', [LMax])); + Writeln(Format(' avg latency : %d ms', [LTotal div REQUEST_COUNT])); + + if LFailed then + begin + Writeln('RESULT: FAIL'); + ExitCode := 1; + end + else + begin + Writeln(Format('RESULT: OK (all %d requests <= %d ms)', [REQUEST_COUNT, LMax])); + ExitCode := 0; + end; +end.