0

I am a beginner in python and have been facing a problem. I am trying to create v4 only grpc server sockets based on the expectation that my code will need to run in ipv6 disabled environments.

My problem is that the following code does NOT create v4 sockets on address 0.0.0.0 by default.

To begin with, Here is a test file with output. Python version 2.7.5.


from gevent import monkey
monkey.patch_all()

from concurrent import patches
import grpc.experimental.gevent
grpc.experimental.gevent.init_gevent()


def main():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    server.add_insecure_port('0.0.0.0:50053')
    server.start()
    var = 1
    while var == 1 :
        pass

if __name__ == "__main__":
    import sys
    del sys.argv[1:]
    sys.exit(main())

Output I see for "netstat -antop" on Centos 7 for port 50053 is :

tcp6       0      0 :::50053                :::*                    LISTEN      745/python           off (0.00/0/0)

I want to get this :

tcp        0      0 0.0.0.0:50053            0.0.0.0:*               LISTEN      1/python             off (0.00/0/0)

Replacing

import grpc.experimental.gevent
grpc.experimental.gevent.init_gevent()

by

import grpc 

makes this work.
Can you please help me understand?

Thanks in advance.

codemos13
  • 1
  • 2

1 Answers1

0

This is a bit surprising to me, because in the implementation, we explicitly checks the socket family and create corresponding type of socket: https://github.com/grpc/grpc/blob/master/src/python/grpcio/grpc/_cython/_cygrpc/grpc_gevent.pyx.pxi#L77

After a quick search, it looks like that is a Linux feature named dual-stack. It helps applications to bind to ipv4 and ipv6 at the same time. If you don't want it, there are many documents about how to disable this feature.

Lidi Zheng
  • 1,801
  • 8
  • 13
  • Thanks @Lidi for your response. The issue seems to happen in the following code at : https://github.com/grpc/grpc/blob/01cc519b53eb2d37f2ee0218bbeee7f08b621c4c/src/core/lib/iomgr/tcp_server_custom.cc#L381. A tcp_server_custom “driver” seems to be selected based on the initialization at https://github.com/grpc/grpc/blob/01cc519b53eb2d37f2ee0218bbeee7f08b621c4c/src/python/grpcio/grpc/_cython/_cygrpc/grpc_gevent.pyx.pxi#L415. Doubts: Is this understanding correct ? Why is [::] and 0.0.0.0 treated as the same as 0.0.0.0 is AF_INET family ? For any wildcard, a default wildcard6 results. – codemos13 Oct 31 '20 at 06:13
  • Thanks for pointing this out. I wasn't aware of of this behavior until you mentioned it, and I don't have the answer. Can you open an issue on our GitHub repo: https://github.com/grpc/grpc/issues? Probably, saying that binding to dual-stack by default is surprising? – Lidi Zheng Nov 02 '20 at 18:15