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

Unable to connect to Unix socket using node-grpc client

Open gcarling opened this issue 8 years ago • 68 comments

I am trying to communicate with a server that is listening on a unix socket - if I use TCP I have no issues, but after much effort have been unable to get it working with the unix socket. Code is

    const BIND_SOCKET = '/tmp/collector.sock';
    const BIND_TCP = ':9832'; // works fine if I use this for client + server
    const PROTO_PATH = './Collector.proto';
    const collector = grpc.load({ root: __dirname, file: PROTO_PATH }).collectorv1;

    this.client = new collector.Collector(BIND_SOCKET, grpc.credentials.createInsecure());

I have tried many variations on the socket, such as unix:/tmp/collector.sock and many others I have found online. However none of them work and my write call simply hangs forever, regardless of whether the server is active and listening on the port.

I am using grpc version 1.10.1

gcarling avatar Apr 05 '18 20:04 gcarling

I am pretty sure the node library does not currently support unix domain sockets.

murgatroid99 avatar Apr 06 '18 21:04 murgatroid99

When I was debugging this I got up to here - line 485 of client.js

this.$channel = new grpc.Channel(address, credentials, options);

in this context, grpc has been required like this -

var grpc = require('./grpc_extension');

My understanding was there was some binary outside of JS that was being used to make the actual connection. This leads me to believe that any behavior supported by that library should be supported here? Maybe I am not fully understanding, but my point is that it does not appear as though the node client is actually the one making the connection. I could be wrong.

gcarling avatar Apr 06 '18 23:04 gcarling

@murgatroid99 Can you confirm for me that node-grpc uses the C++ library under the hood? I saw this issue (https://github.com/grpc/grpc/issues/6504) which confirms for me that the C++ version supports unix sockets. Not sure if you are using this library or a different C++ binary as the base for connection, in any event hoping you can give me some info on that so I can debug further on my own. This is pretty critical to our performance so I am willing to put time into this, even open a PR if necessary.

gcarling avatar Apr 09 '18 22:04 gcarling

So yes, node-grpc uses the grpc-core library under the hood. However, for nodejs integration, it uses the libuv codepath, which doesn't have uds integration. Adding uds to node-grpc would mean changing the name resolver of this file to have the same hack as this other file, and then we'd need to plumb the whole concept of libuv pipes into it.

This isn't trivial, that is.

nicolasnoble avatar Apr 09 '18 23:04 nicolasnoble

OK - thank you very much for the explanation. If at any point supporting unix sockets appears on your roadmap please let me know! I will check this issue once in a while to see if there are any updates. I can submit a formal feature request if that is useful to you.

gcarling avatar Apr 10 '18 20:04 gcarling

I think we can consider this issue to be an actual feature request :-) Also it might be easier for us to implement this using the pure-javascript version of the library.

nicolasnoble avatar Apr 10 '18 21:04 nicolasnoble

Soon there will be anniversary of this ticket :-) I started to work on this feature. Note that if all what you care about is the client side, then one can use grpc-js package (grpc implementation written fully in JS). However grpc server is not implemented in grpc-js, so for that part, the grpc c lib must be fixed. I have submitted a PR for that one: https://github.com/grpc/grpc/pull/18435 .

if that goes well and gets accepted, I will submit a PR for grpc-node as well, which will enable the feature.

jkryl avatar Mar 19 '19 20:03 jkryl

Ping on this issue. Having UDS support would be much appreciated. For now I will only need the client half and will try the grpc-js package, but it clearly states that it is a beta level release at the time of writing this.

faern avatar Jan 17 '20 12:01 faern

@faern : if you can leave a comment in https://github.com/grpc/grpc/pull/18435 that you would like this feature implemented, that might raise the priority of the feature in eyes of grpc maintainers. It's not moving anywhere currently. thanks

jkryl avatar Jan 17 '20 14:01 jkryl

There are zero examples or howtos that I can see for grpc-js. Does anyone have any code snippet on how to use it?

faern avatar Jan 17 '20 15:01 faern

@faern It seems it uses the same API as the c-based one.

@jkryl It seems grpc-js does have server support now but when I tried switching https://github.com/grpc/grpc-node/blob/master/packages/grpc-js/src/server.ts#L203 to using const options = {path: port} if port is not a number, I'm getting an RST_STREAM with error code PROTOCOL_ERROR when I try to grpcurl to it so ... 😕

entropitor avatar Jan 23 '20 10:01 entropitor

Using the advice in this thread, I've been trying to get a grpc-js client to connect to a unix domain socket (running from a go server). I'm hoping someone might have some insight on a more correct way to potentially get this working.

When I try to get the helloworld example working with a UDS:

var grpc = require('@grpc/grpc-js');
...
var client = new hello_proto.Greeter('unix:///var/run/test.sock',
                                     grpc.credentials.createInsecure());

I get the following error (obtained by adding some additional logging):

connect error: Error: getaddrinfo ENOTFOUND var
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:60:26) {
  errno: 'ENOTFOUND',
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: 'var'
}

It seems that when supplying an address prefixed with unix:, some path parsing happens in the resolver, but when the http2 library tries to open a network connection, its not using the correct path. I seem to be able to work around this by overriding createConnection in the credentials options:

creds = grpc.credentials.createInsecure();
creds._getConnectionOptions = () => {
  return {
    createConnection: (address, options) => {
      return net.createConnection("/" + address.hostname + address.pathname);
    },
  };
};

var client = new hello_proto.Greeter('unix:///var/run/test.sock', creds, options);

Does anyone have any insight into why getaddrinfo is being called and what config option I may be missing to get this working? Or if the workaround above seems legitimate?

jcantwell-JC avatar Jan 28 '20 20:01 jcantwell-JC

I'm sorry that I haven't addressed this earlier. Unfortunately, the linked PR grpc/grpc#18435 has gotten stale again and has merge conflicts. If @jkryl updates it I will review it as soon as possible.

As for the @grpc/grpc-js issue, it looks like the unix: address handling was simply implemented incorrectly. I will look into fixing that.

Changing the credentials like that is not a great workaround because that is an implementation detail that just coincidentally does what you want.

murgatroid99 avatar Jan 28 '20 23:01 murgatroid99

One other thing I'm looking into is supporting windows named pipes. Using the code above, and simply passing the named pipe name into the net.createConnection call appears to work. Obviously I understand this isn't a long term option, but it does indicate that the hurdles are similar, which is nice.

After I see what it takes to properly implement the unix: address, I can possibly look into supporting the windows named pipe as well.

jcantwell-JC avatar Jan 29 '20 04:01 jcantwell-JC

@jcantwell-JC I've also been experimenting with this and also got it to work by providingcreateConnection with a socket.

I've found that startConnectingInternal in grpc-js/src/subchannel.ts prefixes the address with either "http://" or "https://". In addition it uses http2.connect which in my understanding does not support uds (unless you provide it with a socket from net.createConnection, as in your example). Modifying that function to not add "http://"/"https://" and to open a connection using net.createConnection works as well.

When it comes to supporting names pipes on windows my guess is that the only additional thing that's missing is a resolver (like grpc-js/src/resolver-uds.ts) which resolves and formats "\\.\pipe\" and "//./pipe/" addresses.

@murgatroid99 Where is unix:address handling implemented incorrectly? Or in which commit? Let me know if my ideas sound good and I'll make a PR.

raksooo avatar Jan 29 '20 08:01 raksooo

@jcantwell-JC : thanks for putting together the example on how to use unix socket with grpc-js!

@murgatroid99 : It has been a long time since I submitted the original PR (over a year). It made sense at that time, but now it's maybe better to fix just grpc-js and move on. Especially if it is a simple fix. Is there a ticket to track the issue in grpc-js that prevents unix: addresses from working? I would like to test it once it gets fixed. thanks!

Also, I haven't been following grpc-js closely. When is it expected to become production-ready? I guess that's an important question for anyone who would like to use Unix sockets in his/her product.

jkryl avatar Jan 29 '20 09:01 jkryl

I have a speculative fix for this grpc-js problem in #1245.

I'm not exactly sure when we will consider the library production-ready, but the Google Cloud API client libraries have been using it officially for a while.

murgatroid99 avatar Jan 30 '20 18:01 murgatroid99

I've published grpc-js version 0.6.16 with the change I linked to. Please try it out and say if it worked for you.

murgatroid99 avatar Feb 04 '20 23:02 murgatroid99

@murgatroid99 do I understand correctly this is only support for the client side? Or should the server side also work with UDS now?

entropitor avatar Feb 05 '20 08:02 entropitor

What I have so far is only for the client side. The server code has a completely different code path, and it doesn't use any of the code in the linked change.

murgatroid99 avatar Feb 05 '20 17:02 murgatroid99