Skip to content

MINOR: Block instead of spinning while waiting for Flight handshake messages - #1506

Merged
jbonofre merged 2 commits into
apache:mainfrom
Arawoof06:handshake-busy-wait
Nov 26, 2026
Merged

jbonofre merged 2 commits into
apache:mainfrom
Arawoof06:handshake-busy-wait

Conversation

@Arawoof06

Copy link
Copy Markdown
Contributor

What's Changed

ServerAuthWrapper gives the legacy ServerAuthHandler an iterator whose next() spins on LinkedBlockingQueue.poll() until the client sends a handshake message or closes the stream. ServerAuthInterceptor lets Handshake through without a token, so on a server configured with a legacy handler such as BasicServerAuthHandler, an unauthenticated client can open Handshake streams, send nothing, and keep one executor thread per stream at full CPU for as long as the stream stays open. ClientAuthWrapper spins the same way on the client while waiting for the server's reply.

next() now blocks on take(). onCompleted/onError enqueue an end-of-stream marker so a waiting reader wakes up and throws the same exceptions as before, and hasNext() ignores the marker. Valid handshakes are unaffected.

Measured against a FlightServer with BasicServerAuthHandler, with 4 Handshake streams opened and left idle for 3 s:

before: wall=3.04s, server executor CPU=8.09s
after:  wall=3.03s, server executor CPU=0.01s

TestBasicAuth.handshakeBlocksWhileWaitingForClient opens an idle Handshake and checks that the server thread is WAITING. It fails without the fix (expected: <WAITING> but was: <RUNNABLE>). The flight-core tests pass with the fix.

…essages

The legacy handshake iterators busy-waited on the message queue, so an
idle Handshake stream kept a server executor thread at full CPU before
authentication. Block on the queue and wake the reader with an
end-of-stream marker instead.
final byte[] bytes;
try {
bytes = messages.take();
} catch (InterruptedException e) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When take() is interrupted, this exception exits doClientAuth before the outbound handshake stream is completed or cancelled. The server can remain blocked waiting for a client message.
I would signal an error on the request stream before propagating the interruption.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. next() now calls sender.onError(e) before it rethrows, so the request stream is errored and the server sees onError and stops waiting on the queue.

Added TestBasicAuth.interruptedClientHandshakeEndsServerStream for it: it waits until the server is parked in ServerAuthWrapper.next, interrupts the client mid-handshake, and checks the server thread leaves next(). Without the onError call it fails with the server still blocked.

@jbonofre jbonofre added the bug-fix PRs that fix a big. label Nov 19, 2026
@apache apache deleted a comment from github-actions Bot Nov 19, 2026
@github-actions github-actions Bot added this to the 20.0.0 milestone Nov 19, 2026
@lidavidm lidavidm removed this from the 20.0.0 milestone Nov 21, 2026
…nterrupted

An interrupt in next() left doClientAuth without completing or
cancelling the outbound stream, so the server could stay blocked waiting
for a client message. Signal the error on the request stream before
propagating the interruption.
@github-actions github-actions Bot added this to the 20.1.0 milestone Nov 26, 2026

@jbonofre jbonofre left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update @Arawoof06! LGTM!

@jbonofre
jbonofre merged commit aa4f576 into apache:main Nov 26, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug-fix PRs that fix a big.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants