Skip to content

Commit 75719be

Browse files
committed
Fix NameResolvers calling Listener2.onResult2 outside of the synchronization context to call it from inside of the synchronization context.
Fixes grpc#11662.
1 parent 00c8bc7 commit 75719be

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

api/src/main/java/io/grpc/NameResolver.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,15 @@ public abstract static class Listener2 implements Listener {
219219
@Override
220220
@Deprecated
221221
@InlineMe(
222-
replacement = "this.onResult2(ResolutionResult.newBuilder().setAddressesOrError("
222+
replacement = "this.onResult(ResolutionResult.newBuilder().setAddressesOrError("
223223
+ "StatusOr.fromValue(servers)).setAttributes(attributes).build())",
224224
imports = {"io.grpc.NameResolver.ResolutionResult", "io.grpc.StatusOr"})
225225
public final void onAddresses(
226226
List<EquivalentAddressGroup> servers, @ResolutionResultAttr Attributes attributes) {
227227
// TODO(jihuncho) need to promote Listener2 if we want to use ConfigOrError
228-
onResult2(
228+
// Calling onResult and not onResult2 because onResult2 can only be called from a
229+
// synchronization context.
230+
onResult(
229231
ResolutionResult.newBuilder().setAddressesOrError(
230232
StatusOr.fromValue(servers)).setAttributes(attributes).build());
231233
}

core/src/main/java/io/grpc/internal/ManagedChannelImplBuilder.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -878,13 +878,14 @@ public String getServiceAuthority() {
878878

879879
@Override
880880
public void start(Listener2 listener) {
881-
listener.onResult2(
882-
ResolutionResult.newBuilder()
883-
.setAddressesOrError(
884-
StatusOr.fromValue(
885-
Collections.singletonList(new EquivalentAddressGroup(address))))
886-
.setAttributes(Attributes.EMPTY)
887-
.build());
881+
args.getSynchronizationContext().execute(() ->
882+
listener.onResult2(
883+
ResolutionResult.newBuilder()
884+
.setAddressesOrError(
885+
StatusOr.fromValue(
886+
Collections.singletonList(new EquivalentAddressGroup(address))))
887+
.setAttributes(Attributes.EMPTY)
888+
.build()));
888889
}
889890

890891
@Override

netty/src/main/java/io/grpc/netty/UdsNameResolver.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@
3030
final class UdsNameResolver extends NameResolver {
3131
private NameResolver.Listener2 listener;
3232
private final String authority;
33+
private final Args args;
3334

3435
UdsNameResolver(String authority, String targetPath, Args args) {
3536
checkArgument(authority == null, "non-null authority not supported");
3637
this.authority = targetPath;
38+
this.args = args;
3739
}
3840

3941
@Override
@@ -58,7 +60,8 @@ private void resolve() {
5860
List<EquivalentAddressGroup> servers = new ArrayList<>(1);
5961
servers.add(new EquivalentAddressGroup(new DomainSocketAddress(authority)));
6062
resolutionResultBuilder.setAddressesOrError(StatusOr.fromValue(servers));
61-
listener.onResult2(resolutionResultBuilder.build());
63+
args.getSynchronizationContext().execute(() ->
64+
listener.onResult2(resolutionResultBuilder.build()));
6265
}
6366

6467
@Override

0 commit comments

Comments
 (0)