Skip to content

Commit

Permalink
all: Add suppressions for GuardedBy violations (grpc#7291)
Browse files Browse the repository at this point in the history
This supports releasing a new version of GuardedBy which finds more mistakes than it used to.

Filed grpc#6578 to try to clean up the suppressions.

Co-authored-by: Graeme Morgan <mail@graememorgan.uk>
  • Loading branch information
rickwebiii and graememorgan committed Aug 17, 2020
1 parent 62e8655 commit bb47680
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
15 changes: 15 additions & 0 deletions core/src/main/java/io/grpc/internal/RetriableStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ abstract class RetriableStream<ReqT> implements ClientStream {
this.throttle = throttle;
}

@SuppressWarnings("GuardedBy")
@Nullable // null if already committed
@CheckReturnValue
private Runnable commit(final Substream winningSubstream) {
Expand All @@ -138,6 +139,8 @@ private Runnable commit(final Substream winningSubstream) {

final Future<?> retryFuture;
if (scheduledRetry != null) {
// TODO(b/145386688): This access should be guarded by 'this.scheduledRetry.lock'; instead
// found: 'this.lock'
retryFuture = scheduledRetry.markCancelled();
scheduledRetry = null;
} else {
Expand All @@ -146,6 +149,8 @@ private Runnable commit(final Substream winningSubstream) {
// cancel the scheduled hedging if it is scheduled prior to the commitment
final Future<?> hedgingFuture;
if (scheduledHedging != null) {
// TODO(b/145386688): This access should be guarded by 'this.scheduledHedging.lock'; instead
// found: 'this.lock'
hedgingFuture = scheduledHedging.markCancelled();
scheduledHedging = null;
} else {
Expand Down Expand Up @@ -338,6 +343,7 @@ public void runWith(Substream substream) {
drain(substream);
}

@SuppressWarnings("GuardedBy")
private void pushbackHedging(@Nullable Integer delayMillis) {
if (delayMillis == null) {
return;
Expand All @@ -356,6 +362,8 @@ private void pushbackHedging(@Nullable Integer delayMillis) {
return;
}

// TODO(b/145386688): This access should be guarded by 'this.scheduledHedging.lock'; instead
// found: 'this.lock'
futureToBeCancelled = scheduledHedging.markCancelled();
scheduledHedging = future = new FutureCanceller(lock);
}
Expand All @@ -381,6 +389,7 @@ private final class HedgingRunnable implements Runnable {
public void run() {
callExecutor.execute(
new Runnable() {
@SuppressWarnings("GuardedBy")
@Override
public void run() {
// It's safe to read state.hedgingAttemptCount here.
Expand All @@ -392,6 +401,9 @@ public void run() {
FutureCanceller future = null;

synchronized (lock) {
// TODO(b/145386688): This access should be guarded by
// 'HedgingRunnable.this.scheduledHedgingRef.lock'; instead found:
// 'RetriableStream.this.lock'
if (scheduledHedgingRef.isCancelled()) {
cancelled = true;
} else {
Expand Down Expand Up @@ -695,10 +707,13 @@ private boolean hasPotentialHedging(State state) {
&& !state.hedgingFrozen;
}

@SuppressWarnings("GuardedBy")
private void freezeHedging() {
Future<?> futureToBeCancelled = null;
synchronized (lock) {
if (scheduledHedging != null) {
// TODO(b/145386688): This access should be guarded by 'this.scheduledHedging.lock'; instead
// found: 'this.lock'
futureToBeCancelled = scheduledHedging.markCancelled();
scheduledHedging = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,12 @@ public void run() {
return new StartCallback().clientStream;
}

@SuppressWarnings("GuardedBy")
@GuardedBy("lock")
private void startStream(CronetClientStream stream) {
streams.add(stream);
// TODO(b/145386688): This access should be guarded by 'stream.transportState().lock'; instead
// found: 'this.lock'
stream.transportState().start(streamFactory);
}

Expand Down
9 changes: 9 additions & 0 deletions okhttp/src/main/java/io/grpc/okhttp/OkHttpClientStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,13 @@ public TransportState(
tag = PerfMark.createTag(methodName);
}

@SuppressWarnings("GuardedBy")
@GuardedBy("lock")
public void start(int streamId) {
checkState(id == ABSENT_ID, "the stream has been started with id %s", streamId);
id = streamId;
// TODO(b/145386688): This access should be guarded by 'OkHttpClientStream.this.state.lock';
// instead found: 'this.lock'
state.onStreamAllocated();

if (canStart) {
Expand Down Expand Up @@ -364,6 +367,7 @@ private void onEndOfStream() {
}
}

@SuppressWarnings("GuardedBy")
@GuardedBy("lock")
private void cancel(Status reason, boolean stopDelivery, Metadata trailers) {
if (cancelSent) {
Expand All @@ -372,6 +376,8 @@ private void cancel(Status reason, boolean stopDelivery, Metadata trailers) {
cancelSent = true;
if (canStart) {
// stream is pending.
// TODO(b/145386688): This access should be guarded by 'this.transport.lock'; instead found:
// 'this.lock'
transport.removePendingStream(OkHttpClientStream.this);
// release holding data, so they can be GCed or returned to pool earlier.
requestHeaders = null;
Expand Down Expand Up @@ -405,6 +411,7 @@ private void sendBuffer(Buffer buffer, boolean endOfStream, boolean flush) {
}
}

@SuppressWarnings("GuardedBy")
@GuardedBy("lock")
private void streamReady(Metadata metadata, String path) {
requestHeaders =
Expand All @@ -415,6 +422,8 @@ private void streamReady(Metadata metadata, String path) {
userAgent,
useGet,
transport.isUsingPlaintext());
// TODO(b/145386688): This access should be guarded by 'this.transport.lock'; instead found:
// 'this.lock'
transport.streamReadyToStart(OkHttpClientStream.this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,12 +419,15 @@ void streamReadyToStart(OkHttpClientStream clientStream) {
}
}

@SuppressWarnings("GuardedBy")
@GuardedBy("lock")
private void startStream(OkHttpClientStream stream) {
Preconditions.checkState(
stream.id() == OkHttpClientStream.ABSENT_ID, "StreamId already assigned");
streams.put(nextStreamId, stream);
setInUse(stream);
// TODO(b/145386688): This access should be guarded by 'stream.transportState().lock'; instead
// found: 'this.lock'
stream.transportState().start(nextStreamId);
// For unary and server streaming, there will be a data frame soon, no need to flush the header.
if ((stream.getType() != MethodType.UNARY && stream.getType() != MethodType.SERVER_STREAMING)
Expand Down Expand Up @@ -1106,6 +1109,7 @@ public void run() {
/**
* Handle an HTTP2 DATA frame.
*/
@SuppressWarnings("GuardedBy")
@Override
public void data(boolean inFinished, int streamId, BufferedSource in, int length)
throws IOException {
Expand All @@ -1131,6 +1135,8 @@ public void data(boolean inFinished, int streamId, BufferedSource in, int length
PerfMark.event("OkHttpClientTransport$ClientFrameHandler.data",
stream.transportState().tag());
synchronized (lock) {
// TODO(b/145386688): This access should be guarded by 'stream.transportState().lock';
// instead found: 'OkHttpClientTransport.this.lock'
stream.transportState().transportDataReceived(buf, inFinished);
}
}
Expand All @@ -1148,6 +1154,7 @@ public void data(boolean inFinished, int streamId, BufferedSource in, int length
/**
* Handle HTTP2 HEADER and CONTINUATION frames.
*/
@SuppressWarnings("GuardedBy")
@Override
public void headers(boolean outFinished,
boolean inFinished,
Expand Down Expand Up @@ -1181,6 +1188,8 @@ public void headers(boolean outFinished,
if (failedStatus == null) {
PerfMark.event("OkHttpClientTransport$ClientFrameHandler.headers",
stream.transportState().tag());
// TODO(b/145386688): This access should be guarded by 'stream.transportState().lock';
// instead found: 'OkHttpClientTransport.this.lock'
stream.transportState().transportHeadersReceived(headerBlock, inFinished);
} else {
if (!inFinished) {
Expand Down

0 comments on commit bb47680

Please sign in to comment.