javasocketsgrpcspigotbungeecord

java.net.SocketException: Invalid argument: connect with BungeeCord


I wrote a Java dependency that uses gRPC to connect to a server. Using this dependency in my spigot plugins and plain java projects works fine, but using it in BungeeCord plugins produces the following exception:

Caused by: io.grpc.netty.shaded.io.netty.channel.AbstractChannel$AnnotatedConnectException: connect(..) failed: Invalid argument: /www.google.com
Caused by: java.net.ConnectException: connect(..) failed: Invalid argument
     at io.grpc.netty.shaded.io.netty.channel.unix.Errors.newConnectException0(Errors.java:155)
     at io.grpc.netty.shaded.io.netty.channel.unix.Errors.handleConnectErrno(Errors.java:128)
     at io.grpc.netty.shaded.io.netty.channel.unix.Socket.connect(Socket.java:312)
     at io.grpc.netty.shaded.io.netty.channel.epoll.AbstractEpollChannel.doConnect0(AbstractEpollChannel.java:773)
     at io.grpc.netty.shaded.io.netty.channel.epoll.EpollSocketChannel.doConnect0(EpollSocketChannel.java:138)
     at io.grpc.netty.shaded.io.netty.channel.epoll.AbstractEpollChannel.doConnect(AbstractEpollChannel.java:758)
     at io.grpc.netty.shaded.io.netty.channel.epoll.AbstractEpollChannel$AbstractEpollUnsafe.connect(AbstractEpollChannel.java:600)
     at io.grpc.netty.shaded.io.netty.channel.DefaultChannelPipeline$HeadContext.connect(DefaultChannelPipeline.java:1342)
     at io.grpc.netty.shaded.io.netty.channel.AbstractChannelHandlerContext.invokeConnect(AbstractChannelHandlerContext.java:548)
     at io.grpc.netty.shaded.io.netty.channel.AbstractChannelHandlerContext.connect(AbstractChannelHandlerContext.java:533)
     at io.grpc.netty.shaded.io.netty.channel.ChannelDuplexHandler.connect(ChannelDuplexHandler.java:54)
     at io.grpc.netty.shaded.io.grpc.netty.WriteBufferingAndExceptionHandler.connect(WriteBufferingAndExceptionHandler.java:157)
     at io.grpc.netty.shaded.io.netty.channel.AbstractChannelHandlerContext.invokeConnect(AbstractChannelHandlerContext.java:548)
     at io.grpc.netty.shaded.io.netty.channel.AbstractChannelHandlerContext.access$1000(AbstractChannelHandlerContext.java:61)
     at io.grpc.netty.shaded.io.netty.channel.AbstractChannelHandlerContext$9.run(AbstractChannelHandlerContext.java:538)
     at io.grpc.netty.shaded.io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:164)
     at io.grpc.netty.shaded.io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:469)
     at io.grpc.netty.shaded.io.netty.channel.epoll.EpollEventLoop.run(EpollEventLoop.java:387)
     at io.grpc.netty.shaded.io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:986)
     at io.grpc.netty.shaded.io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
     at io.grpc.netty.shaded.io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
     at java.lang.Thread.run(Thread.java:748)

What I tried

I tested this with the exact same java runtime, as well as newer versions. I also tried the suggestions in java.net.SocketException: Invalid argument: connect and similar questions asked previously, but none of the proposed solutions worked for me.

The most suggested fix of using -Djava.net.preferIPv4Stack=true changes the exception to

Caused by: io.grpc.netty.shaded.io.netty.channel.AbstractChannel$AnnotatedConnectException: connect(..) failed: Address family not supported by protocol: /www.google.com
Caused by: java.net.ConnectException: connect(..) failed: Address family not supported by protocol

but I'm stuck there as well. I already tried to switch the host I'm running this code on and enabled both IPv4 and IPv6 support on the server gRPC endpoint, with no luck. www.google.com is just a placeholder in these exceptions.

What I suspect

Given it works with all the other projects, I suspect that is has to do with BungeeCord. As far as I investigated BungeeCord itself does not use gRPC (I could be wrong about that). Relocating io.grpc.netty.shaded.io.netty with maven unfortunately did not work for me (maybe because it is already relocated?).

After two days of trying I'm out of my depth and have no idea how to further debug this.


Solution

  • Try to lower the version of io.grpc eg. to 1.30.0, worked for me. I had issue with jar packaged grpc client, what worked in IDE did not worked from jar, the -Djava.net.preferIPv4Stack=true only changed the exception... Spent some time on that as I've started to track the issue from k8s pod :/