Menu

#14 Improper return of in-use port from allocateTCPPort

v1.0 (example)
open
nobody
None
5
2016-01-19
2016-01-19
No

I will occasionally see a port returned from allocateTCPPort even thought there is a client connection connected to the port, and this causes issues for the server processes being started since the servers ensure that all available interfaces are bound when they are given a port to listen on.
The portIsInUse call will call s.bind(("", port)) assuming that use of the empty host will bind to all interfaces, the definition of empty host is that it will bind to all available interfaces - if one of the interfaces is not available, it will simply not bind to it and the call will still return without error. To test this, log onto your favorite linux machine via ssh and find the client port of your connection by running "netstat -an | grep <your-ip-address>" look for the ESTABLISHED status where the other side is bound to your linux machine's IP address on port 22. Use that port to pass into the pysys portIsInUse method and it will return false.</your-ip-address>

A suggestion would be to obtain all available interfaces by calling :
socket.getaddrinfo("", port, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE)
This will return a list with tuples of family, socktype, protocol, canonname, sockaddr. Use this list to attempt to bind the port (then listen etc.) on every interface. Only if all interfaces are not in use, can it be said that the port is not in use at that time.

Discussion


Log in to post a comment.