grpc-java icon indicating copy to clipboard operation
grpc-java copied to clipboard

Clarification on gRPC-Java KeepAliveManager shutdown trigger (PING timeout vs general read inactivity)

Open Trajanv opened this issue 1 year ago • 3 comments

We’re investigating a recurring TCP RST observed ~2.5 seconds after a gRPC client sends application data (PSH, ACK) on a bidirectional stream, and we’re trying to confirm whether this behavior is expected or a side-effect of the keepalive configuration.

Environment

gRPC-Java version: [1.64.0] Transport: Netty Channel configured with: .keepAliveTime(3, TimeUnit.MINUTES) .keepAliveTimeout(2, TimeUnit.SECONDS) .keepAliveWithoutCalls(true) Server side allows keepalive and does not appear to terminate connections.

Observed behavior

The client sends an HTTP/2 DATA frame (visible as TCP PSH, ACK). No further packets are received from the server. Approximately 2.5 seconds later, the client issues a TCP RST. This occurs consistently when the server does not reply or acknowledge within that interval.

https://github.com/grpc/grpc-java/blob/master/core/src/main/java/io/grpc/internal/KeepAliveManager.java

However, we do not see a ping explicitly sent at the time the RST occurs. It appears that a timeout due to lack of any inbound data (not necessarily a PING-ACK) may trigger shutdown().

Questions

  1. Does KeepAliveManager consider only unacknowledged PINGs when starting the keepalive timeout, or anyperiod of read inactivity (including outstanding DATA frames)?
  2. If no PING was sent yet (because keepAliveTime >> 2 s), can the timeout still trigger a shutdown purely due to read inactivity?
  3. Could the RST behavior stem from the Netty transport closing the channel immediately when shutdown()fires (e.g., via Channel.close() with SO_LINGER=0)?
  4. Are there known differences between gRPC-Java and gRPC-C/C++ regarding this shutdown trigger?

Additional context

We’re analyzing this in the context of a long-lived bidirectional streaming RPC. tcpdump shows the client’s last sent frame is application DATA, not a PING.

We suspect the combination of .keepAliveTimeout(2s) and .keepAliveTime(3min) may result in a “false positive” closure if the server doesn’t respond quickly enough after the last DATA frame.

channel =
NettyChannelBuilder.forAddress(serverHost, serverPort)
					.keepAliveTime(180, TimeUnit.SECONDS)
					.keepAliveTimeout(2, TimeUnit.SECONDS)
					.keepAliveWithoutCalls(true)

We have changed (keepAliveTimeout) this to the default (20 sec), and that does see to have an effect on when tcp-retrans occur and RST time.

We’d appreciate clarification or a reference to where in the codebase this distinction (PING ACK vs generic read inactivity) is definitively made.

Thanks for your time and for maintaining gRPC-Java.

Trajanv avatar Nov 10 '25 17:11 Trajanv

This occurs consistently when the server does not reply or acknowledge within that interval.

This is related to keepalive. It isn't using PING, but when you configure keepalive gRPC tries to use TCP_USER_TIMEOUT to the same timeout value you specified. So every packet can act somewhat like a PING (PINGs are still able to notice more issues when TCP LBs are being used), even though keepAliveTime wasn't exceeded. Using TCP_USER_TIMEOUT from Java requires some JNI code, so there's some cases we can't set it and then we rely exclusively on PING. See https://github.com/grpc/proposal/blob/master/A18-tcp-user-timeout.md

https://github.com/grpc/grpc-java/blob/7cb8b68c3aa8dea787bf0ac71e11aadf0f4eed85/netty/src/main/java/io/grpc/netty/NettyClientTransport.java#L303-L307

ejona86 avatar Nov 10 '25 18:11 ejona86

Thank you for your detailed explanation regarding TCP_USER_TIMEOUT and its interaction with gRPC-Java keepalive. That helps clarify the RST behavior we’ve been observing. I had a follow-up regarding the gRPC keepalive documentation: gRPC Keepalive guide. The table describes:

KEEPALIVE_TIMEOUT — Client and Server — The timeout in milliseconds for a PING frame to be acknowledged. If sender does not receive an acknowledgment within this time, it will close the connection.

Given that gRPC-Java may trigger a connection closure via TCP_USER_TIMEOUT even without sending a PING frame, do you think this description might need an update to reflect that behavior? Specifically, that any unacknowledged outbound packet (DATA or PING) can lead to connection termination when TCP_USER_TIMEOUT is in effect, independent of the PING-based timeout.

We just want to make sure users understand the potential for RSTs on long-lived streams when keepAliveTimeout is very low.

Thanks again for maintaining gRPC-Java and for your time!

Trajanv avatar Nov 10 '25 20:11 Trajanv

https://github.com/grpc/grpc.io/pull/1477 will be updating the guide.

ejona86 avatar Nov 10 '25 23:11 ejona86