Repository navigation
Conversation
…secutive failures
There was a problem hiding this comment.
Code Review
This pull request enhances the session pool implementation by capping desired capacity, bounding the pending RPC queue, fast-failing requests during budget lockouts, and clearing references upon cancellation to prevent memory leaks. The review feedback identifies several concurrency issues, recommending synchronization within executor tasks in SessionPoolImpl to avoid race conditions, declaring sessionEstablished as volatile in ChannelPoolDpImpl for thread visibility, and using double-checked locking to safely update session establishment state.
| () -> { | ||
| if (isCancelled) return; | ||
| isCancelled = true; | ||
| VRpcListener<RespT> pendingListener = listener; | ||
| req = null; | ||
| listener = null; | ||
| if (realCall != null) { | ||
| if (!onlyCancelPendingCall) { | ||
| realCall.cancel(status.getDescription(), status.getCause()); | ||
| } | ||
| } else { | ||
| listener.onClose(VRpcResult.createRejectedError(status)); | ||
| } else if (pendingListener != null) { | ||
| pendingListener.onClose(VRpcResult.createRejectedError(status)); | ||
| } | ||
| }); |
There was a problem hiding this comment.
Since ctx.getExecutor() can be a multi-threaded executor, there is a potential check-then-act race condition on isCancelled and a data race on realCall, req, and listener. Synchronizing on PendingVRpc.this inside the executor task ensures thread safety and prevents duplicate calls to onClose or concurrent modification issues.
| () -> { | |
| if (isCancelled) return; | |
| isCancelled = true; | |
| VRpcListener<RespT> pendingListener = listener; | |
| req = null; | |
| listener = null; | |
| if (realCall != null) { | |
| if (!onlyCancelPendingCall) { | |
| realCall.cancel(status.getDescription(), status.getCause()); | |
| } | |
| } else { | |
| listener.onClose(VRpcResult.createRejectedError(status)); | |
| } else if (pendingListener != null) { | |
| pendingListener.onClose(VRpcResult.createRejectedError(status)); | |
| } | |
| }); | |
| () -> { | |
| VRpcListener<RespT> pendingListener; | |
| VRpc<ReqT, RespT> localRealCall; | |
| synchronized (PendingVRpc.this) { | |
| if (isCancelled) return; | |
| isCancelled = true; | |
| pendingListener = listener; | |
| req = null; | |
| listener = null; | |
| localRealCall = realCall; | |
| } | |
| if (localRealCall != null) { | |
| if (!onlyCancelPendingCall) { | |
| localRealCall.cancel(status.getDescription(), status.getCause()); | |
| } | |
| } else if (pendingListener != null) { | |
| pendingListener.onClose(VRpcResult.createRejectedError(status)); | |
| } | |
| }); |
| ctx.getExecutor() | ||
| .execute( | ||
| () -> { | ||
| if (isCancelled) return; | ||
| isCancelled = true; | ||
| listener.onClose(result); | ||
| VRpcListener<RespT> pendingListener = listener; | ||
| req = null; | ||
| listener = null; | ||
| if (pendingListener != null) { | ||
| pendingListener.onClose(result); | ||
| } | ||
| }); |
There was a problem hiding this comment.
Similarly to the cancel method, cancelWithResult should synchronize on PendingVRpc.this to avoid race conditions on isCancelled, req, and listener when executed on a multi-threaded executor.
ctx.getExecutor()
.execute(
() -> {
VRpcListener<RespT> pendingListener;
synchronized (PendingVRpc.this) {
if (isCancelled) return;
isCancelled = true;
pendingListener = listener;
req = null;
listener = null;
}
if (pendingListener != null) {
pendingListener.onClose(result);
}
});| return new SessionStreamImpl(innerCall) { | ||
| // null until onBeforeSessionStart fires | ||
| @Nullable AfeId afeId = null; | ||
| boolean sessionEstablished = false; |
There was a problem hiding this comment.
The sessionEstablished flag is read outside of the synchronized block in onMessage (line 332). To prevent thread visibility issues and ensure memory consistency across threads, this field should be declared volatile.
| boolean sessionEstablished = false; | |
| volatile boolean sessionEstablished = false; |
References
- Ensure that fields used for cross-thread state checks, such as exceptions set by background threads and read by application threads, are declared as volatile to guarantee memory visibility and establish a proper happens-before relationship under the Java Memory Model.
| public void onMessage(SessionResponse msg) { | ||
| if (!sessionEstablished && msg.hasOpenSession()) { | ||
| synchronized (ChannelPoolDpImpl.this) { | ||
| sessionEstablished = true; | ||
| fw.consecutiveFailures = 0; | ||
| recycleBackoff = INITIAL_RECYCLE_BACKOFF; | ||
| } | ||
| } | ||
| super.onMessage(msg); | ||
| } |
There was a problem hiding this comment.
To avoid redundant writes and ensure thread safety when resetting consecutive failures, we should use a double-checked locking pattern under the synchronized block after checking sessionEstablished.
@Override
public void onMessage(SessionResponse msg) {
if (!sessionEstablished && msg.hasOpenSession()) {
synchronized (ChannelPoolDpImpl.this) {
if (!sessionEstablished) {
sessionEstablished = true;
fw.consecutiveFailures = 0;
recycleBackoff = INITIAL_RECYCLE_BACKOFF;
}
}
}
super.onMessage(msg);
}…-pool-pending-rpcs-bound
…ool-pre-open-session-failure
Ensures stream closes with non-OK status before
OpenSessionResponseis received are counted as consecutive session-establishment failures inChannelPoolDpImpl, even whenPeerInfoheaders were sent prior to stream rejection.Stack