gRPC server on a particular Port

4,664 views
Skip to first unread message

Poojitha A

unread,
Feb 22, 2016, 7:25:02 PM2/22/16
to grpc.io
Hi All,

I have a small doubt. I want to run my grpc process on port X. For some reason if that port is busy, what is the expected output? 

Does the process run anyway on that port or what are the possible outcomes we see?

Ex:

I have tried running my process on port: 830 which already has another process running. 


Nicolas Noble

unread,
Feb 22, 2016, 7:50:31 PM2/22/16
to Poojitha A, grpc.io
To properly answer your question, we'd need to know which language you're running on.

From the C core's perspective, which excludes Java and Go, you'd get the following error messages:

E0223 01:47:15.375666421   10037 tcp_server_posix.c:296]     bind addr=[::]:80: Permission denied
E0223 01:47:15.375774834   10037 server_chttp2.c:122]        No address added out of total 1 resolved

or

E0223 01:47:19.743723195   10045 tcp_server_posix.c:296]     bind addr=[::]:9999: Address already in use
E0223 01:47:19.743854965   10045 server_chttp2.c:122]        No address added out of total 1 resolved


Depending on the language, you'll get different error codes bubbling up. In C++, BuildAndStart should return nullptr.

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+unsubscribe@googlegroups.com.
To post to this group, send email to grp...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/50ccd7ba-508f-4c0c-8b79-d747d9b04fa2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Vijay Pai

unread,
Feb 22, 2016, 7:51:30 PM2/22/16
to Poojitha A, grpc.io
Hello,
I'm assuming from your previous messages that you're coding in C++. I'm also assuming that you're talking about a server being generated by ServerBuilder::BuildAndStart . In this case, you can check the return value of the BuildAndStart method - if it is nullptr, your server didn't get its listening port.
Regards,
Vijay

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.

Poojitha A

unread,
Feb 23, 2016, 3:28:40 PM2/23/16
to grpc.io, pooji...@gmail.com
Hello, Here is what I am doing. I am checking for nullptr:
//server address =[::]:80

builder.AddListeningPort(server_add,creds);
....

server=builder.BuildAndStart();
    if(server==nullptr)
    {
        PRINTtoScreen("Server not started on the given port %s. Please check if the port is free.",port.c_str());
        return 0;
    }
    else
    {
       PRINTtoScreen("gRPC server listening on: %s",server_add.c_str());
        server->Wait();
        return 1;
    }
    }

LOG Trace:

 Starting the gRPC server
Server(985): gRPC server listening on: [::]:80

But While I try to connect from client:

E0223 12:26:22.921420890    8715 ssl_transport_security.c:874] Handshake failed with fatal error SSL_ERROR_SSL: error:1408F10B:SSL routines:SSL3_GET_RECORD:wrong version number.
E0223 12:26:22.921606989    8715 handshake.c:212]            Handshake failed with error TSI_PROTOCOL_FAILURE
E0223 12:26:22.921677121    8715 secure_channel_create.c:98] Secure handshake failed with error 2.
^C

Seeing this error. On first place I should get a null pointer if i am right. 

I am not sure how the grpc internally adds the port. If another process is listening on same port or if I have no permission to add that port I should receive a nullptr which I am not.  Can some one please clarify on this. 

Craig Tiller

unread,
Feb 24, 2016, 10:38:42 AM2/24/16
to Poojitha A, grpc.io
So it looks like the TCP connection succeeds but the SSL handshake is failing. I suspect the credentials you're passing to CreateChannel and to AddPort are mismatched. If you post how you're creating credentials on the client and server we can probably help more.

Poojitha A

unread,
Feb 24, 2016, 1:38:07 PM2/24/16
to Craig Tiller, grpc.io
Here is the snippet: 

        server_address << "[::]:" << port;
        std::string server_add(server_address.str());
        ServerBuilder builder;
        std::shared_ptr<ServerCredentials> creds;
        if(tls)
        {
                std::ifstream readkeyfile(GetKeyFile());
                std::stringstream buffer;
                buffer << readkeyfile.rdbuf();
                std::string key=buffer.str();
                std::ifstream readcertfile(GetCACert());
                buffer << readcertfile.rdbuf();
                std::string cert=buffer.str();
                SslServerCredentialsOptions::PemKeyCertPair pkcp ={key,cert};
                SslServerCredentialsOptions ssl_opts;
                ssl_opts.pem_root_certs="";
                ssl_opts.pem_key_cert_pairs.push_back(pkcp);
                creds = SslServerCredentials(ssl_opts);
        }
        else
        {
                
                creds=grpc::InsecureServerCredentials();
        }

   
    builder.AddListeningPort(server_add,creds);
    

The above is seen only on few ports not on all. It works fine on a diff ports on which no other process is running.

Also,  I was told, it returns a null pointer if the port is not available but I am not hitting that case. 

For ex:
port 830 is used by a different process. While I start my grpc service it doesnt give me nullptr, but while client tries to connect it gives hand shake failed. 

Poojitha A

unread,
Feb 25, 2016, 3:31:57 PM2/25/16
to Vijay Pai, grpc.io, Craig Tiller
Hi Vijay,

From my observation and tweaking the helloworld, i see that if there is another process on same port it says:

[New Thread 0x7ffff5a61700 (LWP 18454)]
E0225 12:24:44.320526998   18450 tcp_server_posix.c:284]     bind addr=[::]:50051: Address already in use
E0225 12:24:44.320717475   18450 server_chttp2.c:122]        No address added out of total 1 resolved
--- hangs here

The program is hung at the call BuildAndStart().  It doesn't return back with a null pointer and hence i never hit the if condition. I tried running gdb on the same. Looks like its polling in a loop and not coming out of it. 

(gdb) bt
#0  0x00007ffff68f712d in poll () at ../sysdeps/unix/syscall-template.S:81
#1  0x00007ffff778daba in ?? () from /usr/local/lib/libgrpc.so.0
#2  0x00007ffff778d5b4 in grpc_pollset_work () from /usr/local/lib/libgrpc.so.0
#3  0x00007ffff7798aff in grpc_completion_queue_next () from /usr/local/lib/libgrpc.so.0
#4  0x00007ffff7bc4ae8 in grpc::CompletionQueue::AsyncNextInternal(void**, bool*, gpr_timespec) () from /usr/local/lib/libgrpc++_unsecure.so.0
#5  0x00007ffff7bc9f72 in grpc::Server::~Server() () from /usr/local/lib/libgrpc++_unsecure.so.0
#6  0x00007ffff7bca0f9 in grpc::Server::~Server() () from /usr/local/lib/libgrpc++_unsecure.so.0
#7  0x00007ffff7bcca05 in grpc::ServerBuilder::BuildAndStart() () from /usr/local/lib/libgrpc++_unsecure.so.0
#8  0x000000000040d02b in RunServer() ()
#9  0x000000000040d217 in main ()
(gdb) 


code snippet: 

std::cout<<"after port"<<endl;
  // Register "service" as the instance through which we'll communicate with
  // clients. In this case it corresponds to an *synchronous* service.
  builder.RegisterService(&service);
  // Finally assemble the server.
  std::unique_ptr<Server> server=builder.BuildAndStart();
  if(!server)
  {
        std::cout<<"server not listening port issue"<<std::endl;
        return;
  }
  std::cout << "Server listening on " << server_address << std::endl;

  // Wait for the server to shutdown. Note that some other thread must be
  // responsible for shutting down the server for this call to ever return.
  server->Wait();

One more Question: 
What does it mean when the grpc server is able to start a process on a particular port even though there is another process actively running on the same. These are two ambiguities i am seeing. 

any help is greatly appreciated. 

Nicolas Noble

unread,
Feb 25, 2016, 4:01:20 PM2/25/16
to Poojitha A, Vijay Pai, grpc.io, Craig Tiller
So, let me be clear: no process in the world can bind on an already occupied IP:port pair. And gRPC certainly cannot do that in particular.

However, what gRPC does is that if you provide a list of more than one IP:port to connect to, it'll only fail if none of them were able to be bound. So if you specify, say, ::80, ::830, and ::8080, then if ::80 and ::830 but ::8080 succeeds, then the server will still start, but only by listening to ::8080.

Poojitha A

unread,
Feb 25, 2016, 6:45:27 PM2/25/16
to grpc.io
I think my question was misleading. I am not providing multiple ip:port values. I understand no process can bind on already bound IP:Port.  

But the behavior i saw was, though there is some other process on a port, the grpc server starts but the client fails with SSL_V3 version bound issue. 


E0223 12:26:22.921420890    8715 ssl_transport_security.c:874] Handshake failed with fatal error SSL_ERROR_SSL: error:1408F10B:SSL routines:SSL3_GET_RECORD:wrong version number.
E0223 12:26:22.921606989    8715 handshake.c:212]            Handshake failed with error TSI_PROTOCOL_FAILURE
E0223 12:26:22.921677121    8715 secure_channel_create.c:98] Secure handshake failed with error 2.
^C

Q2. 
When I start my grpc process on a busy port it is hanging at the BuildAndStart() and not returning the control. 

Poojitha A

unread,
Feb 29, 2016, 2:55:54 PM2/29/16
to Craig Tiller, grpc.io

Hi Craig, 


I have pulled release 0_12 about a week ago. This says its fixed. 

Looks like its still there! I might be wrong though. 

server=builder.BuildAndStart();
    if(server==nullptr)
    {
        PRINTtoScreen("Server not started on the given port %s. Please check if the port is free.",port.c_str());
        return 0;
    }
    else
    {
       PRINTtoScreen("gRPC server listening on: %s",server_add.c_str());
        server->Wait();
        return 1;
    }
    }

LOG Trace:

At the call of BuildAndStart()

E0226 17:32:01.333655913 71004 tcp_server_posix.c:284] bind addr=[::ffff:127.0.0.1]:50051: Address already in use
E0226 17:32:01.333775574 71004 server_chttp2.c:122] No address added out of total 1 resolved

---->  simply hangs here I cant get control back. 
^C

Please let me know if i am missing something. 

On Wed, Feb 24, 2016 at 7:38 AM, Craig Tiller <cti...@google.com> wrote:

Poojitha A

unread,
Feb 29, 2016, 4:06:43 PM2/29/16
to Craig Tiller, Vijay Pai, grpc.io
Observation:

This is fixed in 0_13 but not in 0_12. 

I m not sure how the grpc versioning works. It would be great if it can be fixed on 12 as well. 

Another minor suggestion not so important:
The example throws seq fault when there is another process running on same port. This is because it calls server->wait() where server is nullptr. We can add that condition though if its important.

Just saying as this can be helpful for naive beginners like me. 
Reply all
Reply to author
Forward
0 new messages