user1071136
user1071136

Reputation: 15725

Binding a Java gRPC server to a unix domain socket

I have the following, which creates a Java gRPC server and attempts to bind it to the USD /tmp/foo:

  EpollEventLoopGroup group = new EpollEventLoopGroup();
  Server server =
      NettyServerBuilder.forAddress(new DomainSocketAddress("/tmp/foo"))
          .channelType(EpollServerSocketChannel.class)
          .bossEventLoopGroup(group)
          .workerEventLoopGroup(group)
          .addService(new Impl())
          .build()
          .start();
  server.awaitTermination();

However, this fails with bind(..) failed: Invalid argument:

Exception in thread "main" java.io.IOException: Failed to bind
        at io.grpc.netty.NettyServer.start(NettyServer.java:231)
        at io.grpc.internal.ServerImpl.start(ServerImpl.java:151)
        at io.grpc.internal.ServerImpl.start(ServerImpl.java:75)
        at com.google.devtools.javatools.jade.pkgloader.GrpcLocalServer.main(GrpcLocalServer.java:60)
Caused by: io.netty.channel.unix.Errors$NativeIoException: bind(..) failed: Invalid argument
        at io.netty.channel.unix.Errors.newIOException(Errors.java:117)
        at io.netty.channel.unix.Socket.bind(Socket.java:291)
        at io.netty.channel.epoll.AbstractEpollChannel.doBind(AbstractEpollChannel.java:714)
        at io.netty.channel.epoll.EpollServerSocketChannel.doBind(EpollServerSocketChannel.java:70)
        at io.netty.channel.AbstractChannel$AbstractUnsafe.bind(AbstractChannel.java:558)
        at io.netty.channel.DefaultChannelPipeline$HeadContext.bind(DefaultChannelPipeline.java:1283)
        at io.netty.channel.AbstractChannelHandlerContext.invokeBind(AbstractChannelHandlerContext.java:501)
        at io.netty.channel.AbstractChannelHandlerContext.bind(AbstractChannelHandlerContext.java:486)
        at io.netty.channel.DefaultChannelPipeline.bind(DefaultChannelPipeline.java:989)
        at io.netty.channel.AbstractChannel.bind(AbstractChannel.java:254)
        at io.netty.bootstrap.AbstractBootstrap$2.run(AbstractBootstrap.java:364)
        at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
        at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:403)
        at io.netty.channel.epoll.EpollEventLoop.run(EpollEventLoop.java:309)
        at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858)
        at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:138)
        at java.lang.Thread.run(Thread.java:745)

I checked that the file /tmp/foo doesn't exist, that I have write permissions to it and its parent directory, and that no process is bound to it (netstat -na | grep /tmp/foo.socket). I'm on a Linux system.

Am I missing something obvious?

Upvotes: 3

Views: 3452

Answers (1)

user1071136
user1071136

Reputation: 15725

Using EpollServerDomainSocketChannel instead of EpollServerSocketChannel fixed the issue.

For easy future copy/paste, the following binds (listens) to a UDS:

EpollEventLoopGroup group = new EpollEventLoopGroup();
Server server = NettyServerBuilder.forAddress(new DomainSocketAddress("/tmp/foo"))
    .channelType(EpollServerDomainSocketChannel.class)
    .workerEventLoopGroup(group)
    .bossEventLoopGroup(group)
    .addService(new Impl())
    .build()

Upvotes: 6

Related Questions