Skip to content

Remove JSR-305 ThreadSafe annotation and replace with JavaDoc#12762

Open
Kainsin wants to merge 2 commits into
grpc:masterfrom
Kainsin:master
Open

Remove JSR-305 ThreadSafe annotation and replace with JavaDoc#12762
Kainsin wants to merge 2 commits into
grpc:masterfrom
Kainsin:master

Conversation

@Kainsin
Copy link
Copy Markdown
Contributor

@Kainsin Kainsin commented Apr 17, 2026

This is another attempt to remove JSR-305 annotations but instead of replacing with ErrorProne's ThreadSafe, sticks to adding a JavaDoc comment. This should basically keep things inline with what JSR-305 ThreadSafe affords.

Adding ErrorProne's ThreadSafe can be considered in the future, as it expects more things than JSR-305.

Removing the JSR-305 dependency here allows Java applications that have moved away from javax to compile and avoids a bug in Immutables and Lombok (and possibly other annotation processors) from failing when JSR-305 is not present.

@Kainsin Kainsin marked this pull request as ready for review April 17, 2026 15:39
@Kainsin
Copy link
Copy Markdown
Contributor Author

Kainsin commented Apr 17, 2026

I am not sure why that test is failing - it's passing locally. Am I missing a configuration setting that is causing an extra log to be emitted here?

@Kainsin Kainsin force-pushed the master branch 2 times, most recently from 951018b to 5fbc141 Compare April 20, 2026 15:20
@Kainsin
Copy link
Copy Markdown
Contributor Author

Kainsin commented Apr 20, 2026

I was able to make a change to fix the test - it's in the second commit.

@kannanjgithub you reviewed my last PR for this, would you be able to review this one as well? Thanks!

*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1704")
@ThreadSafe
public final class CompressorRegistry {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The suggestion from Eric was only to replace the Threadsafe annotation for the public non-final classes. This and many other classes changed in this PR are final classes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood, I'll fix that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kannanjgithub Sorry for taking so long to get back to this. I updated the commit and only public non-final classes should be included in the change.

Thanks!

@Kainsin Kainsin force-pushed the master branch 2 times, most recently from 417527f to 9425fd3 Compare May 11, 2026 15:06
@kannanjgithub
Copy link
Copy Markdown
Contributor

There is an unrelated commit 9425fd3 with this PR.

@Kainsin
Copy link
Copy Markdown
Contributor Author

Kainsin commented May 12, 2026

There is an unrelated commit 9425fd3 with this PR.

@kannanjgithub There was a test passing locally but failing in CI. I added this to fix the race condition.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR removes usages of the JSR-305 javax.annotation.concurrent.ThreadSafe annotation across several gRPC public APIs and internal interfaces, replacing it with JavaDoc statements, with the goal of reducing reliance on JSR-305-provided annotations and improving compatibility for builds that don’t include them.

Changes:

  • Removed javax.annotation.concurrent.ThreadSafe imports/usages from multiple API, core, stub, and binder types and replaced them with JavaDoc text asserting thread-safety.
  • Updated OkHttpClientTransportTest teardown to wait briefly for executor termination after shutdown.

Reviewed changes

Copilot reviewed 25 out of 25 changed files in this pull request and generated 28 comments.

Show a summary per file
File Description
stub/src/main/java/io/grpc/stub/AbstractStub.java Removed @ThreadSafe, added JavaDoc thread-safety note.
stub/src/main/java/io/grpc/stub/AbstractFutureStub.java Removed @ThreadSafe, added JavaDoc thread-safety note.
stub/src/main/java/io/grpc/stub/AbstractBlockingStub.java Removed @ThreadSafe, added JavaDoc thread-safety note.
stub/src/main/java/io/grpc/stub/AbstractAsyncStub.java Removed @ThreadSafe, added JavaDoc thread-safety note.
okhttp/src/test/java/io/grpc/okhttp/OkHttpClientTransportTest.java Improved executor shutdown behavior in tearDown().
core/src/main/java/io/grpc/internal/ObjectPool.java Removed @ThreadSafe, added JavaDoc thread-safety note.
core/src/main/java/io/grpc/internal/ManagedClientTransport.java Removed @ThreadSafe, added JavaDoc thread-safety note.
core/src/main/java/io/grpc/internal/KeepAliveManager.java Removed @ThreadSafe on nested interface, added JavaDoc thread-safety note.
core/src/main/java/io/grpc/internal/InternalServer.java Removed @ThreadSafe, added JavaDoc thread-safety note.
core/src/main/java/io/grpc/internal/ConnectionClientTransport.java Removed @ThreadSafe, added JavaDoc thread-safety note.
core/src/main/java/io/grpc/internal/ClientTransport.java Removed @ThreadSafe, added JavaDoc thread-safety note.
binder/src/main/java/io/grpc/binder/internal/BinderTransport.java Removed @ThreadSafe, added JavaDoc thread-safety note.
api/src/main/java/io/grpc/StreamTracer.java Removed @ThreadSafe, added JavaDoc thread-safety note.
api/src/main/java/io/grpc/ServerStreamTracer.java Removed @ThreadSafe, added JavaDoc thread-safety note.
api/src/main/java/io/grpc/ServerInterceptor.java Removed @ThreadSafe, added JavaDoc thread-safety note.
api/src/main/java/io/grpc/ServerCallHandler.java Removed @ThreadSafe, added JavaDoc thread-safety note.
api/src/main/java/io/grpc/Server.java Removed @ThreadSafe, added JavaDoc thread-safety note.
api/src/main/java/io/grpc/NameResolver.java Removed @ThreadSafe on Listener, added JavaDoc thread-safety note.
api/src/main/java/io/grpc/ManagedChannel.java Removed @ThreadSafe, added JavaDoc thread-safety note.
api/src/main/java/io/grpc/LoadBalancer.java Removed @ThreadSafe on nested types, added JavaDoc thread-safety notes.
api/src/main/java/io/grpc/HandlerRegistry.java Removed @ThreadSafe, added JavaDoc thread-safety note.
api/src/main/java/io/grpc/ClientStreamTracer.java Removed @ThreadSafe, added JavaDoc thread-safety note.
api/src/main/java/io/grpc/ClientInterceptor.java Removed @ThreadSafe, added JavaDoc thread-safety note.
api/src/main/java/io/grpc/ChannelLogger.java Removed @ThreadSafe, added JavaDoc thread-safety note.
api/src/main/java/io/grpc/Channel.java Removed @ThreadSafe, added JavaDoc thread-safety note.
Comments suppressed due to low confidence (7)

api/src/main/java/io/grpc/Channel.java:34

  • The new JavaDoc includes a whitespace-only line with trailing spaces and a tooling-specific TODO about using Error Prone’s @ThreadSafe later. Please remove trailing whitespace and keep the JavaDoc focused on the actual thread-safety contract.
 * 
 * <p>This is thread-safe and should be considered
 * for the errorprone ThreadSafe annotation in the future.
 */

api/src/main/java/io/grpc/ChannelLogger.java:27

  • The added JavaDoc includes a trailing-whitespace blank line and a tooling-specific TODO about adopting Error Prone’s @threadsafe. Please remove trailing whitespace and keep the public JavaDoc focused on the thread-safety contract rather than future tooling changes.
 * A Channel-specific logger provided by GRPC library to {@link LoadBalancer} implementations.
 * Information logged here goes to <strong>Channelz</strong>, and to the Java logger of this class
 * as well.
 * 
 * <p>This is thread-safe and should be considered
 * for the errorprone ThreadSafe annotation in the future.
 */

api/src/main/java/io/grpc/ClientInterceptor.java:22

  • This file now contains two blank lines between the package statement and the type JavaDoc after removing the last import. Please reduce to a single blank line to match the project’s usual formatting for no-import files (e.g., api/src/main/java/io/grpc/CallbackMetricInstrument.java:17-21).

This issue also appears on line 39 of the same file.

package io.grpc;


/**
 * Interface for intercepting outgoing calls before they are dispatched by a {@link Channel}.
 *

api/src/main/java/io/grpc/ClientInterceptor.java:42

  • The newly added JavaDoc includes a trailing-whitespace blank line and a tooling/TODO note about potentially switching to Error Prone’s @ThreadSafe in the future. Please remove trailing whitespace and keep the JavaDoc to the actual thread-safety contract.
 * 
 * <p>This is thread-safe and should be considered
 * for the errorprone ThreadSafe annotation in the future.
 */

api/src/main/java/io/grpc/ServerInterceptor.java:22

  • There are now two blank lines between the package declaration and the type JavaDoc (after removing the only import). Please reduce this to a single blank line (consistent with api/src/main/java/io/grpc/CallbackMetricInstrument.java:17-21).
package io.grpc;


/**
 * Interface for intercepting incoming calls before they are dispatched by
 * {@link ServerCallHandler}.

api/src/main/java/io/grpc/LoadBalancer.java:1036

  • The added JavaDoc includes a tooling/TODO note about adopting Error Prone’s @ThreadSafe later. Please keep this public API JavaDoc focused on the thread-safety contract, and remove the future tooling discussion (also ensure blank lines don’t contain trailing whitespace).
   * Provides essentials for LoadBalancer implementations.
   *
   * <p>This is thread-safe and should be considered
   * for the errorprone ThreadSafe annotation in the future.
   *

api/src/main/java/io/grpc/LoadBalancer.java:1559

  • This new JavaDoc block includes a tooling-focused TODO about potentially switching to Error Prone’s @threadsafe. Since this is part of the public API docs, please replace it with a direct thread-safety guarantee/requirement and remove the tooling discussion (and avoid trailing whitespace on blank lines).
  /**
   * Factory to create {@link LoadBalancer} instance.
   *
   * <p>This is thread-safe and should be considered
   * for the errorprone ThreadSafe annotation in the future.
   *

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

executor.shutdownNow();
executor.awaitTermination(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
// Ignore in a test and continue on as normal.
Comment on lines +48 to +49
* <p>This is thread-safe and should be considered
* for the errorprone ThreadSafe annotation in the future.
Comment on lines +30 to +31
* <p>This is thread-safe and should be considered
* for the errorprone ThreadSafe annotation in the future.
Comment on lines +30 to +31
* <p>This is thread-safe and should be considered
* for the errorprone ThreadSafe annotation in the future.
Comment on lines +30 to +31
* <p>This is thread-safe and should be considered
* for the errorprone ThreadSafe annotation in the future.
Comment on lines +23 to +25
*
* <p>This is thread-safe and should be considered
* for the errorprone ThreadSafe annotation in the future.
Comment thread api/src/main/java/io/grpc/Server.java Outdated
Comment on lines +28 to +30
*
* <p>This is thread-safe and should be considered
* for the errorprone ThreadSafe annotation in the future.
Comment on lines +216 to +219
*
* <p>This is thread-safe and should be considered
* for the errorprone ThreadSafe annotation in the future.
*
Comment on lines 34 to 38
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.ThreadSafe;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commit message is only for removing the ThreadSafe dependency. It will not be removed everywhere, though. Only in public, non-final classes and interfaces.

Comment on lines +449 to +451
*
* <p>This is thread-safe and should be considered
* for the errorprone ThreadSafe annotation in the future.
Kainsin added 2 commits May 13, 2026 09:09
This fixes a potential race condition where a test runs a thread in
the executor that does some logging. Without waiting for threads to
finish, another test can be run while that background thread is still
executing. This can cause flaky issues with tests that only think
that their test code is executing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants