Skip to content

Commit aa34ab1

Browse files
committed
xds: Fix TSAN data race on ClientCall cancellation in ExternalProcessorClientInterceptor
Prevent concurrent cancellations of the underlying ClientCall in ExternalProcessorClientInterceptor: - Wrap rawCall with SimpleForwardingClientCall using an AtomicBoolean to ensure the underlying ClientCall.cancel() is executed at most once, even if invoked concurrently across threads or from DelayedListener. - Remove redundant downstreamCancelled AtomicBoolean from DataPlaneClientCall and simplify cancelDownstream() to directly delegate to delayedCall.cancel(), as DelayedClientCall internally synchronizes pending cancellations and the wrapped rawCall deduplicates active cancellations. - Remove the now-unused rawCall field and constructor parameter from DataPlaneListener. - In DataPlaneClientCall.cancel() and validateCompressionSupport(), atomically transition extProcStreamState to FAILED and clear extProcClientCallRequestObserver. - In sendToExtProc(), return early if the ext-proc stream is already completed or the observer is null. - Safely complete and clear extProcClientCallRequestObserver in closeExtProcStream(). - Route all rawCall.cancel() calls in sendMessage(), handleImmediateResponse(), and DataPlaneListener through cancelDownstream(). Jetski conversations: - 502db5da-b88e-474c-9c05-a14b441d3eac - 3318c948-f0f7-49ce-afe3-5670e30bf376 CONV=502db5da-b88e-474c-9c05-a14b441d3eac
1 parent 2858db2 commit aa34ab1

1 file changed

Lines changed: 40 additions & 32 deletions

File tree

‎xds/src/main/java/io/grpc/xds/ExternalProcessorClientInterceptor.java‎

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,18 @@ public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
238238
MethodDescriptor<InputStream, InputStream> rawMethod =
239239
(MethodDescriptor<InputStream, InputStream>) (MethodDescriptor<?, ?>) method;
240240
ClientCall<InputStream, InputStream> rawCall =
241-
(ClientCall<InputStream, InputStream>) (ClientCall<?, ?>)
242-
next.newCall(method, callOptions);
241+
new SimpleForwardingClientCall<InputStream, InputStream>(
242+
(ClientCall<InputStream, InputStream>) (ClientCall<?, ?>)
243+
next.newCall(method, callOptions)) {
244+
private final AtomicBoolean cancelled = new AtomicBoolean(false);
245+
246+
@Override
247+
public void cancel(@Nullable String message, @Nullable Throwable cause) {
248+
if (cancelled.compareAndSet(false, true)) {
249+
super.cancel(message, cause);
250+
}
251+
}
252+
};
243253

244254
// Create a local subclass instance to buffer outbound actions
245255
DataPlaneDelayedCall<InputStream, InputStream> delayedCall =
@@ -315,7 +325,6 @@ private static class DataPlaneClientCall
315325
final AtomicBoolean isProcessingTrailers = new AtomicBoolean(false);
316326
final AtomicBoolean pendingHalfClose = new AtomicBoolean(false);
317327
final AtomicBoolean bodyMessageSentToExtProc = new AtomicBoolean(false);
318-
private final AtomicBoolean downstreamCancelled = new AtomicBoolean(false);
319328

320329
protected DataPlaneClientCall(
321330
DataPlaneDelayedCall<InputStream, InputStream> delayedCall,
@@ -397,13 +406,14 @@ private boolean validateCompressionSupport(BodyResponse bodyResponse) {
397406
.withDescription("gRPC message compression not supported in ext_proc")
398407
.asRuntimeException();
399408
synchronized (streamLock) {
400-
if (!extProcStreamState.get().isCompleted()
401-
&& extProcClientCallRequestObserver != null) {
402-
extProcClientCallRequestObserver.onError(ex);
409+
if (markExtProcStreamFailed(extProcStreamState)) {
410+
if (extProcClientCallRequestObserver != null) {
411+
extProcClientCallRequestObserver.onError(ex);
412+
extProcClientCallRequestObserver = null;
413+
}
403414
}
404415
}
405416
activateCall();
406-
markExtProcStreamFailed(extProcStreamState);
407417
cancelDownstream("gRPC message compression not supported in ext_proc", ex);
408418
closeExtProcStream();
409419
return false;
@@ -419,7 +429,7 @@ public void start(Listener<InputStream> responseListener, Metadata headers) {
419429
this.callContext = Context.current();
420430
clientHeadersStartNanos = System.nanoTime();
421431
this.requestHeaders = headers;
422-
this.wrappedListener = new DataPlaneListener(responseListener, rawCall, this);
432+
this.wrappedListener = new DataPlaneListener(responseListener, this);
423433

424434
// DelayedClientCall.start will buffer the listener and headers until setCall is called.
425435
super.start(wrappedListener, headers);
@@ -600,6 +610,9 @@ public void onError(Throwable t) {
600610
@Override
601611
public void onCompleted() {
602612
if (markExtProcStreamCompleted(extProcStreamState)) {
613+
synchronized (streamLock) {
614+
extProcClientCallRequestObserver = null;
615+
}
603616
handleFailOpen(wrappedListener);
604617
}
605618
}
@@ -629,7 +642,7 @@ public void onCompleted() {
629642

630643
private void sendToExtProc(ProcessingRequest request) {
631644
synchronized (streamLock) {
632-
if (extProcStreamState.get().isCompleted()) {
645+
if (extProcStreamState.get().isCompleted() || extProcClientCallRequestObserver == null) {
633646
return;
634647
}
635648

@@ -691,6 +704,7 @@ private void closeExtProcStream() {
691704
if (markExtProcStreamCompleted(extProcStreamState)) {
692705
if (extProcClientCallRequestObserver != null) {
693706
extProcClientCallRequestObserver.onCompleted();
707+
extProcClientCallRequestObserver = null;
694708
}
695709
}
696710
}
@@ -700,11 +714,7 @@ private void internalOnError(Throwable t) {
700714
if (markExtProcStreamFailed(extProcStreamState)) {
701715
synchronized (streamLock) {
702716
if (extProcClientCallRequestObserver != null) {
703-
try {
704-
extProcClientCallRequestObserver.onError(t);
705-
} catch (Throwable ignored) {
706-
// Ignore exceptions during cancel/onError propagation
707-
}
717+
extProcClientCallRequestObserver.onError(t);
708718
extProcClientCallRequestObserver = null;
709719
}
710720
}
@@ -809,7 +819,7 @@ public void sendMessage(InputStream message) {
809819
ByteString copiedBody = ByteString.readFrom(message);
810820
pendingDrainingMessages.add(new KnownLengthInputStream(copiedBody));
811821
} catch (IOException e) {
812-
rawCall.cancel("Failed to copy outbound message for buffering", e);
822+
cancelDownstream("Failed to copy outbound message for buffering", e);
813823
}
814824
return;
815825
}
@@ -835,7 +845,7 @@ public void sendMessage(InputStream message) {
835845
super.sendMessage(new KnownLengthInputStream(bodyByteString));
836846
}
837847
} catch (IOException e) {
838-
rawCall.cancel("Failed to serialize message for External Processor", e);
848+
cancelDownstream("Failed to serialize message for External Processor", e);
839849
}
840850
}
841851

@@ -900,21 +910,22 @@ public void halfClose() {
900910
.build());
901911
}
902912

903-
private void cancelDownstream(@Nullable String message, @Nullable Throwable cause) {
904-
if (downstreamCancelled.compareAndSet(false, true)) {
905-
delayedCall.cancel(message, cause);
906-
}
913+
void cancelDownstream(@Nullable String message, @Nullable Throwable cause) {
914+
delayedCall.cancel(message, cause);
907915
}
908916

909917
@Override
910918
public void cancel(@Nullable String message, @Nullable Throwable cause) {
911919
synchronized (streamLock) {
912-
if (!extProcStreamState.get().isCompleted() && extProcClientCallRequestObserver != null) {
913-
extProcClientCallRequestObserver.onError(
914-
Status.CANCELLED
915-
.withDescription(message)
916-
.withCause(cause)
917-
.asRuntimeException());
920+
if (markExtProcStreamFailed(extProcStreamState)) {
921+
if (extProcClientCallRequestObserver != null) {
922+
extProcClientCallRequestObserver.onError(
923+
Status.CANCELLED
924+
.withDescription(message)
925+
.withCause(cause)
926+
.asRuntimeException());
927+
extProcClientCallRequestObserver = null;
928+
}
918929
}
919930
}
920931
cancelDownstream(message, cause);
@@ -970,7 +981,7 @@ private void handleImmediateResponse(ImmediateResponse immediate, DataPlaneListe
970981
// If sent in response to any other event, it will cause the data plane RPC to
971982
// immediately fail with the specified status as if it were an out-of-band
972983
// cancellation.
973-
rawCall.cancel(status.getDescription(), null);
984+
cancelDownstream(status.getDescription(), null);
974985
listener.unblockAfterStreamComplete();
975986
}
976987
closeExtProcStream();
@@ -1058,7 +1069,6 @@ AtomicBoolean getIsProcessingTrailers() {
10581069
}
10591070

10601071
private static class DataPlaneListener extends SimpleForwardingClientCallListener<InputStream> {
1061-
private final ClientCall<?, ?> rawCall;
10621072
private final DataPlaneClientCall dataPlaneClientCall;
10631073
private final Queue<InputStream> savedMessages = new ConcurrentLinkedQueue<>();
10641074
private boolean inboundPassThrough = false;
@@ -1071,10 +1081,8 @@ private static class DataPlaneListener extends SimpleForwardingClientCallListene
10711081

10721082
protected DataPlaneListener(
10731083
ClientCall.Listener<InputStream> delegate,
1074-
ClientCall<?, ?> rawCall,
10751084
DataPlaneClientCall dataPlaneClientCall) {
10761085
super(delegate);
1077-
this.rawCall = rawCall;
10781086
this.dataPlaneClientCall = dataPlaneClientCall;
10791087
}
10801088

@@ -1153,7 +1161,7 @@ public void onMessage(InputStream message) {
11531161
ByteString copiedBody = ByteString.readFrom(message);
11541162
savedMessages.add(new KnownLengthInputStream(copiedBody));
11551163
} catch (IOException e) {
1156-
rawCall.cancel("Failed to copy inbound message for buffering", e);
1164+
dataPlaneClientCall.cancelDownstream("Failed to copy inbound message for buffering", e);
11571165
}
11581166
return;
11591167
}
@@ -1184,7 +1192,7 @@ public void onMessage(InputStream message) {
11841192
() -> delegate().onMessage(bodyByteString.newInput()));
11851193
}
11861194
} catch (IOException e) {
1187-
rawCall.cancel("Failed to read server response", e);
1195+
dataPlaneClientCall.cancelDownstream("Failed to read server response", e);
11881196
}
11891197
}
11901198

0 commit comments

Comments
 (0)