@@ -911,7 +911,7 @@ class PendingVRpc<ReqT extends Message, RespT extends Message> implements VRpc<R
911911 private boolean isCancelled = false ;
912912 private VRpc <ReqT , RespT > realCall ;
913913
914- private BigtableTimer .Timeout deadlineTimeout ;
914+ private volatile BigtableTimer .Timeout deadlineTimeout ;
915915
916916 public PendingVRpc (VRpcDescriptor <?, ReqT , RespT > desc ) {
917917 this .desc = desc ;
@@ -922,7 +922,7 @@ public PendingVRpc(VRpcDescriptor<?, ReqT, RespT> desc) {
922922 // region. Bounded acquisition on the request hot path; see poolLock.
923923 @ SuppressWarnings ("GuardedBy" )
924924 public void start (ReqT req , VRpcCallContext ctx , VRpcListener <RespT > listener ) {
925- Preconditions .checkState (this .req == null , "request is already started" );
925+ Preconditions .checkState (this .ctx == null , "request is already started" );
926926 Preconditions .checkNotNull (req , "request can't be null" );
927927 Preconditions .checkNotNull (ctx , "ctx can't be null" );
928928 Preconditions .checkNotNull (listener , "listener can't be null" );
@@ -940,34 +940,22 @@ public void start(ReqT req, VRpcCallContext ctx, VRpcListener<RespT> listener) {
940940 VRpcResult .createUncommitedError (
941941 Status .UNAVAILABLE .withCause (
942942 new IllegalStateException ("SessionPool lock unavailable (pool wedged)" )));
943- ctx .getExecutor ()
944- .execute (
945- () -> {
946- if (isCancelled ) return ;
947- isCancelled = true ;
948- listener .onClose (result );
949- });
943+ cancelWithResult (result );
950944 return ;
951945 }
952946 try {
953947 if (SessionPoolImpl .this .poolState != PoolState .STARTED ) {
954948 VRpcResult result =
955949 VRpcResult .createUncommitedError (
956950 Status .UNAVAILABLE .withCause (new IllegalStateException ("SessionPool is closed" )));
957- ctx . getExecutor (). execute (() -> listener . onClose ( result ) );
951+ cancelWithResult ( result );
958952 return ;
959953 }
960954 if (poolSizer .isDisabled ()) {
961955 VRpcResult result =
962956 VRpcResult .createRejectedError (
963957 Status .UNAVAILABLE .withDescription ("SessionPool configuration disabled" ));
964- ctx .getExecutor ()
965- .execute (
966- () -> {
967- if (isCancelled ) return ;
968- isCancelled = true ;
969- listener .onClose (result );
970- });
958+ cancelWithResult (result );
971959 return ;
972960 }
973961 // Only arm the deadline monitor after we've committed to queueing; otherwise the
@@ -1013,8 +1001,10 @@ public void cancel(@Nullable String message, @Nullable Throwable cause) {
10131001 // region below.
10141002 @ SuppressWarnings ("GuardedBy" )
10151003 private void cancel (Status status , boolean onlyCancelPendingCall ) {
1016- if (deadlineTimeout != null ) {
1017- deadlineTimeout .cancel ();
1004+ BigtableTimer .Timeout timeout = deadlineTimeout ;
1005+ deadlineTimeout = null ;
1006+ if (timeout != null ) {
1007+ timeout .cancel ();
10181008 }
10191009 // The eager removal is only an optimization to skip a wasted session pick in
10201010 // tryDrainPendingRpcs.
@@ -1037,26 +1027,36 @@ private void cancel(Status status, boolean onlyCancelPendingCall) {
10371027 () -> {
10381028 if (isCancelled ) return ;
10391029 isCancelled = true ;
1030+ VRpcListener <RespT > pendingListener = listener ;
1031+ req = null ;
1032+ listener = null ;
10401033 if (realCall != null ) {
10411034 if (!onlyCancelPendingCall ) {
10421035 realCall .cancel (status .getDescription (), status .getCause ());
10431036 }
1044- } else {
1045- listener .onClose (VRpcResult .createRejectedError (status ));
1037+ } else if ( pendingListener != null ) {
1038+ pendingListener .onClose (VRpcResult .createRejectedError (status ));
10461039 }
10471040 });
10481041 }
10491042
10501043 void cancelWithResult (VRpcResult result ) {
1051- if (deadlineTimeout != null ) {
1052- deadlineTimeout .cancel ();
1044+ BigtableTimer .Timeout timeout = deadlineTimeout ;
1045+ deadlineTimeout = null ;
1046+ if (timeout != null ) {
1047+ timeout .cancel ();
10531048 }
10541049 ctx .getExecutor ()
10551050 .execute (
10561051 () -> {
10571052 if (isCancelled ) return ;
10581053 isCancelled = true ;
1059- listener .onClose (result );
1054+ VRpcListener <RespT > pendingListener = listener ;
1055+ req = null ;
1056+ listener = null ;
1057+ if (pendingListener != null ) {
1058+ pendingListener .onClose (result );
1059+ }
10601060 });
10611061 }
10621062
@@ -1077,8 +1077,10 @@ public void requestNext() {
10771077 }
10781078
10791079 private void drainTo (SessionHandle handle ) {
1080- if (deadlineTimeout != null ) {
1081- deadlineTimeout .cancel ();
1080+ BigtableTimer .Timeout timeout = deadlineTimeout ;
1081+ deadlineTimeout = null ;
1082+ if (timeout != null ) {
1083+ timeout .cancel ();
10821084 }
10831085 ctx .getExecutor ()
10841086 .execute (
@@ -1087,15 +1089,15 @@ private void drainTo(SessionHandle handle) {
10871089 SessionPoolImpl .this .onPendingVRpcCancelled (handle );
10881090 return ;
10891091 }
1092+ ReqT pendingReq = req ;
1093+ VRpcListener <RespT > pendingListener = listener ;
1094+ req = null ;
1095+ listener = null ;
10901096 realCall = newRealCall (desc , handle );
1091- realCall .start (req , ctx , listener );
1097+ realCall .start (pendingReq , ctx , pendingListener );
10921098 });
10931099 }
10941100
1095- private VRpcListener <RespT > getListener () {
1096- return listener ;
1097- }
1098-
10991101 private BigtableTimer .Timeout monitorDeadline (Deadline deadline ) {
11001102 // Body runs on backgroundExecutor (off the tick thread). onlyCancelPendingCall=true avoids
11011103 // racing with a user cancel that already attached a real call.
0 commit comments