Skip to content

Commit

Permalink
Fix NameResolvers calling Listener2.onResult2 outside of the synchron…
Browse files Browse the repository at this point in the history
…ization context to call it from inside of the synchronization context.

Fixes grpc#11662.
  • Loading branch information
kannanjgithub committed Nov 5, 2024
1 parent 00c8bc7 commit 75719be
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
6 changes: 4 additions & 2 deletions api/src/main/java/io/grpc/NameResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,15 @@ public abstract static class Listener2 implements Listener {
@Override
@Deprecated
@InlineMe(
replacement = "this.onResult2(ResolutionResult.newBuilder().setAddressesOrError("
replacement = "this.onResult(ResolutionResult.newBuilder().setAddressesOrError("
+ "StatusOr.fromValue(servers)).setAttributes(attributes).build())",
imports = {"io.grpc.NameResolver.ResolutionResult", "io.grpc.StatusOr"})
public final void onAddresses(
List<EquivalentAddressGroup> servers, @ResolutionResultAttr Attributes attributes) {
// TODO(jihuncho) need to promote Listener2 if we want to use ConfigOrError
onResult2(
// Calling onResult and not onResult2 because onResult2 can only be called from a
// synchronization context.
onResult(
ResolutionResult.newBuilder().setAddressesOrError(
StatusOr.fromValue(servers)).setAttributes(attributes).build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -878,13 +878,14 @@ public String getServiceAuthority() {

@Override
public void start(Listener2 listener) {
listener.onResult2(
ResolutionResult.newBuilder()
.setAddressesOrError(
StatusOr.fromValue(
Collections.singletonList(new EquivalentAddressGroup(address))))
.setAttributes(Attributes.EMPTY)
.build());
args.getSynchronizationContext().execute(() ->
listener.onResult2(
ResolutionResult.newBuilder()
.setAddressesOrError(
StatusOr.fromValue(
Collections.singletonList(new EquivalentAddressGroup(address))))
.setAttributes(Attributes.EMPTY)
.build()));
}

@Override
Expand Down
5 changes: 4 additions & 1 deletion netty/src/main/java/io/grpc/netty/UdsNameResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@
final class UdsNameResolver extends NameResolver {
private NameResolver.Listener2 listener;
private final String authority;
private final Args args;

UdsNameResolver(String authority, String targetPath, Args args) {
checkArgument(authority == null, "non-null authority not supported");
this.authority = targetPath;
this.args = args;
}

@Override
Expand All @@ -58,7 +60,8 @@ private void resolve() {
List<EquivalentAddressGroup> servers = new ArrayList<>(1);
servers.add(new EquivalentAddressGroup(new DomainSocketAddress(authority)));
resolutionResultBuilder.setAddressesOrError(StatusOr.fromValue(servers));
listener.onResult2(resolutionResultBuilder.build());
args.getSynchronizationContext().execute(() ->
listener.onResult2(resolutionResultBuilder.build()));
}

@Override
Expand Down

0 comments on commit 75719be

Please sign in to comment.