Open
Description
Description:
While reviewing the get_routable_ip_to function in torchx/apps/serve/serve.py, I noticed that the socket is directly closed using s.close(), without calling shutdown() beforehand.
def get_routable_ip_to(addr: str) -> str:
"""
get_routable_ip_to opens a dummy connection to the target HTTP URL and
returns the IP address used to connect to it.
"""
parsed = urlparse(addr)
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((parsed.hostname, parsed.port or 80))
return s.getsockname()[0]
finally:
s.close()
Question
Would there be any potential downsides or benefits to adding a shutdown(socket.SHUT_RDWR) call before closing the socket in the get_routable_ip_to function?
Possible Benefits
- Ensures that all pending data is properly discarded before closing, particularly if the socket is still in a half-open state.
- Prevents potential issues with lingering resources and improves resource management.
- Aligns with best practices for socket cleanup.
Reference
The Python socket documentation states:
"close() releases the resource associated with a connection but does not necessarily close the connection immediately. If you want to close the connection in a timely fashion, call shutdown() before close()." link
Looking forward to your thoughts!
Thanks!
Metadata
Metadata
Assignees
Labels
No labels