netty: Allow network errors to override graceful shutdown status#12822
Open
themechbro wants to merge 2 commits into
Open
netty: Allow network errors to override graceful shutdown status#12822themechbro wants to merge 2 commits into
themechbro wants to merge 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR aims to ensure that when a Netty client transport is gracefully shut down and a subsequent real network failure occurs, the later failure is not masked by the earlier graceful shutdown status—so active streams can surface the underlying infrastructure error (per #12812).
Changes:
- Adds a “status upgrade” path in
ClientTransportLifecycleManager#notifyShutdown()to replace an already-cached graceful shutdown status with a later status that has aThrowablecause. - Adds a new unit test attempting to reproduce “graceful shutdown followed by network drop” behavior in
NettyClientTransportTest.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| netty/src/main/java/io/grpc/netty/ClientTransportLifecycleManager.java | Adds logic intended to upgrade cached shutdown status when a later shutdown has a cause |
| netty/src/test/java/io/grpc/netty/NettyClientTransportTest.java | Adds a test intended to validate that network errors override graceful shutdown status |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+72
to
+79
| // Check if the incoming error is just the routine channel closure exception | ||
| boolean isClosedChannel = s.getCause() instanceof java.nio.channels.ClosedChannelException; | ||
|
|
||
| // Status Upgrade: Overwrite graceful shutdown if a hard network error occurs | ||
| if (shutdownStatus.getCause() == null && s.getCause() != null && !isClosedChannel) { | ||
| shutdownStatus = s; | ||
| return true; | ||
| } |
Comment on lines
67
to
70
| /** Returns {@code true} if was the first shutdown. */ | ||
| @CanIgnoreReturnValue | ||
| public boolean notifyShutdown(Status s, DisconnectError disconnectError) { | ||
| notifyGracefulShutdown(s, disconnectError); |
Comment on lines
+302
to
+324
| @Test | ||
| public void networkErrorOverridesGracefulShutdownStatus() throws Exception { | ||
| startServer(); | ||
| NettyClientTransport transport = newTransport(newNegotiator()); | ||
| callMeMaybe(transport.start(clientTransportListener)); | ||
|
|
||
| // 1. Trigger graceful shutdown | ||
| Status gracefulStatus = Status.UNAVAILABLE.withDescription("Channel shutdown invoked"); | ||
| transport.shutdown(gracefulStatus); | ||
|
|
||
| // 2. Simulate a real network drop (e.g., Connection Reset) | ||
| java.io.IOException networkCause = new java.io.IOException("Connection reset by peer"); | ||
| transport.channel().pipeline().fireExceptionCaught(networkCause); | ||
| transport.channel().pipeline().fireChannelInactive(); | ||
|
|
||
| // 3. Verify the listener receives the IO error, NOT the graceful status | ||
| verify(clientTransportListener, timeout(5000)).transportShutdown( | ||
| org.mockito.ArgumentMatchers.argThat(status -> | ||
| status != null && status.getCause() instanceof java.io.IOException | ||
| ), | ||
| org.mockito.ArgumentMatchers.any() | ||
| ); | ||
| } |
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.
Fixes #12812
Motivation
Currently,
ClientTransportLifecycleManageracts as a strict latch for the first shutdown status it receives. If a user initiates a graceful shutdown (shutdownStatuswith no cause) and the channel subsequently experiences a hard network drop (channelInactivefiring aClosedChannelException), the transport masks the physical network error and propagates the benign graceful status to active streams. This blinds telemetry to actual infrastructure drops during shutdown windows.Modifications
ClientTransportLifecycleManager#notifyShutdownto introduce a status upgrade mechanism. If the existingshutdownStatusis a graceful intent (has noThrowablecause) and the incomingStatusrepresents a hard error (has aThrowablecause), the manager now overwrites the cached status.networkErrorOverridesGracefulShutdownStatustoNettyClientTransportTestwhich perfectly simulates the reproducer by firing a graceful shutdown followed byfireChannelInactive(), asserting that theClosedChannelExceptionis properly propagated to the transport listener.Result
Active streams that are forcefully interrupted during a graceful shutdown window will now correctly fail with
UNAVAILABLE: channel closed(with the underlying Netty exception) rather thanUNAVAILABLE: Channel shutdown invoked.