grpc-java icon indicating copy to clipboard operation
grpc-java copied to clipboard

NullPointerException when create channel with hostname contains '_' underscore

Open MinosPong opened this issue 9 years ago • 6 comments

Please answer these questions before submitting your issue.

What version of gRPC are you using?

grpc-core-1.6.1

What JVM are you using (java -version)?

openjdk version 1.8.0_141

What did you do?

Create a channel with hostname contains underscore ('') ManagedChannelBuilder.forTarget("host_1").usePlaintext(true).build(); It is due to the java.net.URI return null on getHost() when hostname contains ''. We understand that '' is not align with standard, but many systems support '' nowadays, and is hard to avoid.

What did you see instead?

java.lang.NullPointerException: host
	at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:228)
	at io.grpc.internal.DnsNameResolver.<init>(DnsNameResolver.java:97)
	at io.grpc.internal.DnsNameResolverProvider.newNameResolver(DnsNameResolverProvider.java:50)
	at io.grpc.internal.DnsNameResolverProvider.newNameResolver(DnsNameResolverProvider.java:39)
	at io.grpc.NameResolverProvider$NameResolverFactory.newNameResolver(NameResolverProvider.java:162)
	at io.grpc.internal.ManagedChannelImpl.getNameResolver(ManagedChannelImpl.java:447)
	at io.grpc.internal.ManagedChannelImpl.<init>(ManagedChannelImpl.java:383)
	at io.grpc.internal.AbstractManagedChannelImplBuilder.build(AbstractManagedChannelImplBuilder.java:320)

MinosPong avatar Nov 09 '17 10:11 MinosPong

DNS names cannot contain underscores, it is forbidden by the RFC. The only valid domain names contain [a-zA-Z0-9-]

carl-mastrangelo avatar Nov 09 '17 19:11 carl-mastrangelo

Yeah, the crux of the problem is that Java's URI doesn't support underscores. So short of reimplementing it, there's not much we can do.

(Now, we could give a better error than NPE.)

ejona86 avatar Nov 10 '17 19:11 ejona86

@ejona86, Actually I find that DnsNameResolver is the only class using URL.create (other than test class). So I am thinking if it is possible to rewrite the DnsNameResolver to prevent using URL.create to workaround this problem. I think the _http._tcp.hostname.com will also suffer from the same issue.

MinosPong avatar Nov 13 '17 04:11 MinosPong

A patch on the URI to workaround it may also be an option

MinosPong avatar Nov 13 '17 04:11 MinosPong

We use new URI in ManagedChannelImpl, GrpcUtil, ProxyDetectorImpl (as necessary for ProxySelector), GoogleAuthLibraryCallCredentials, and some other places. You can create a URI with an underscore using create() and the 1 and 5 argument constructors, but it won't parse the authority so host will be null and port -1, so is generally useless to us. The 7 argument constructor throws if the host has an underscore.

_http._tcp.hostname.com is not a hostname; it should be fine.

Those patches look incompatible with Java 9. I don't see how we can guarantee underscore will work for users when such hacks are necessary. For things like proxy selection and security APIs we call require URI, so we couldn't even implement the URI parsing ourselves.

ejona86 avatar Nov 13 '17 21:11 ejona86

@ejona86 Seems there is a technical limitation to make it possible. I agree with it. Having a better error message is good enough right now. Thank you for your answer.

MinosPong avatar Nov 15 '17 07:11 MinosPong