fix(arrow/flight): deliver response headers eagerly in streaming client middleware#801
Merged
zeroshade merged 2 commits intoapache:mainfrom May 6, 2026
Conversation
…nt middleware For streaming RPCs like Handshake, `ClientHeadersMiddleware.HeadersReceived` was only invoked from `finishFn`, which fires when `Recv()` returns `io.EOF` or an error. `AuthenticateBasicToken` calls `Recv()` only once; if the server returns a `HandshakeResponse` payload (common when the Handshake response carries auth data or a session cookie), `Recv()` returns that message rather than `io.EOF` and the middleware never observes the response headers. As a result the cookie middleware fails to capture `Set-Cookie` from Handshake, and subsequent RPCs are sent without the session cookie. This change makes `clientStream.Header()` invoke `HeadersReceived` at-most-once (guarded by an atomic flag) with the response metadata the first time headers are successfully retrieved. The existing `finishFn` path is preserved so cookies arriving in trailers are still captured. Fixes apache#755
…mentNoSchema MockServer.DoPutPreparedStatementQuery has an early-return branch for ExpectedPreparedStatementSchema != nil that was introduced after the GH-35328 flake fix landed. The original drain loop (see ff339b7) was not copied into the new branch, so the same io.EOF race - where the server returns and closes its side of the stream before the client finishes writing the parameter record batch - still occurs on the code path used by TestPreparedStatementNoSchema. The race is timing-sensitive and manifests most often on ARM64 Debian Go 1.25 with '-asan -tags assert,test,noasm' (see CI run 25386542292), but has been observed on main CI runs dating back to at least April 2026 (runs 24804648950 and 24366361129). Adds the same `for r.Next() {}` drain to the early-return branch. The comment explicitly notes this is NOT redundant with the drain below; the two guard different success paths.
lidavidm
approved these changes
May 6, 2026
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.
Rationale for this change
Fixes #755. The cookie middleware (
NewClientCookieMiddleware) does not captureSet-Cookieheaders returned in response to a streaming RPC likeHandshakewhen the server also sends back a response payload.ClientHeadersMiddleware.HeadersReceivedwas only invoked fromfinishFn, which fires whenRecv()returnsio.EOFor a non-io.EOFerror.AuthenticateBasicTokencallsRecv()exactly once; if the server sends aHandshakeResponsepayload (common when the Handshake carries auth data or a session cookie),Recv()returns that message rather thanio.EOFandfinishFnnever fires. The cookie middleware never sees the response headers, so the session cookie is dropped and subsequent RPCs go out without it, even though the user reports cookies ARE delivered on other endpoints likeGetFlightInfo(unary RPCs capture headers synchronously viagrpc.Header(&md)).What changes are included in this PR?
clientStream.Header()now delivers response metadata toClientHeadersMiddlewareat-most-once (guarded byatomic.Bool.CompareAndSwap) the first time headers are successfully retrieved for a streaming RPC.finishFnpath is unchanged so:Header()get the exact same behavior as before.arrow/flight/handshake_cookie_test.gocovering:Set-Cookiein Handshake response headers (viaAuthenticateBasicToken)Set-Cookiein Handshake response trailersSet-Cookie+ server-sentHandshakeResponsepayload (the precise scenario reported in Cookie middleware does not work for do_handshake #755 — fails without this fix)stream.Header()is inspected before draining the stream (also fails without this fix)Are these changes tested?
Yes. The four new tests in
arrow/flight/handshake_cookie_test.goreproduce the regression. Tests 3 and 4 fail without the fix and pass with it. The existing middleware/cookie tests continue to pass, including with-race.Are there any user-facing changes?
Minor behavioral refinement of
ClientHeadersMiddlewarefor streaming RPCs:HeadersReceivedmay now be invoked up to twice on a streaming RPC whose caller explicitly callsstream.Header()— once with just the response headers (fromHeader()), and again with headers+trailers joined (from the existingfinishFnpath at stream completion). This is backward compatible for the in-treeclientCookieMiddleware(cookie updates are keyed byname+pathand idempotent). Callers that never explicitly callstream.Header()see no change in behavior.