Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

possible Improvement: Using shutdown() Before close() in server.py #1012

Open
allrob23 opened this issue Mar 4, 2025 · 0 comments
Open

possible Improvement: Using shutdown() Before close() in server.py #1012

allrob23 opened this issue Mar 4, 2025 · 0 comments

Comments

@allrob23
Copy link

allrob23 commented Mar 4, 2025

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant