Skip to content

fix(arrow/flight): deliver response headers eagerly in streaming client middleware#801

Merged
zeroshade merged 2 commits intoapache:mainfrom
zeroshade:fix/gh-755-handshake-cookie-middleware
May 6, 2026
Merged

fix(arrow/flight): deliver response headers eagerly in streaming client middleware#801
zeroshade merged 2 commits intoapache:mainfrom
zeroshade:fix/gh-755-handshake-cookie-middleware

Conversation

@zeroshade
Copy link
Copy Markdown
Member

Rationale for this change

Fixes #755. The cookie middleware (NewClientCookieMiddleware) does not capture Set-Cookie headers returned in response to a streaming RPC like Handshake when the server also sends back a response payload.

ClientHeadersMiddleware.HeadersReceived was only invoked from finishFn, which fires when Recv() returns io.EOF or a non-io.EOF error. AuthenticateBasicToken calls Recv() exactly once; if the server sends a HandshakeResponse payload (common when the Handshake carries auth data or a session cookie), Recv() returns that message rather than io.EOF and finishFn never 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 like GetFlightInfo (unary RPCs capture headers synchronously via grpc.Header(&md)).

What changes are included in this PR?

  • clientStream.Header() now delivers response metadata to ClientHeadersMiddleware at-most-once (guarded by atomic.Bool.CompareAndSwap) the first time headers are successfully retrieved for a streaming RPC.
  • The existing finishFn path is unchanged so:
    • trailers are still captured when the stream completes, and
    • callers that never explicitly invoke Header() get the exact same behavior as before.
  • Added four regression tests in arrow/flight/handshake_cookie_test.go covering:
    1. Set-Cookie in Handshake response headers (via AuthenticateBasicToken)
    2. Set-Cookie in Handshake response trailers
    3. Set-Cookie + server-sent HandshakeResponse payload (the precise scenario reported in Cookie middleware does not work for do_handshake #755 — fails without this fix)
    4. Eager capture when 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.go reproduce 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 ClientHeadersMiddleware for streaming RPCs: HeadersReceived may now be invoked up to twice on a streaming RPC whose caller explicitly calls stream.Header() — once with just the response headers (from Header()), and again with headers+trailers joined (from the existing finishFn path at stream completion). This is backward compatible for the in-tree clientCookieMiddleware (cookie updates are keyed by name+path and idempotent). Callers that never explicitly call stream.Header() see no change in behavior.

…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
@zeroshade zeroshade requested review from amoeba, kou and lidavidm May 5, 2026 15:47
…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.
@zeroshade zeroshade merged commit cdc768e into apache:main May 6, 2026
23 checks passed
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.

Cookie middleware does not work for do_handshake

2 participants