Skip to content

asyncio possible Improvement: Using shutdown() Before close() in _close_self_pipe #130850

@allrob23

Description

@allrob23

Description

While reviewing the _close_self_pipe method in Lib/asyncio/selector_events.py, I noticed that the sockets _ssock and _csock are directly closed using close(), without first calling shutdown().

Code Reference

    def _close_self_pipe(self):
        self._remove_reader(self._ssock.fileno())
        self._ssock.close()
        self._ssock = None
        self._csock.close()
        self._csock = None
        self._internal_fds -= 1

Question

Would there be any potential downsides or benefits to adding a shutdown(socket.SHUT_RDWR) call before closing these sockets?

Possible Benefits

  • Ensures that all pending data is properly discarded before closing, particularly in scenarios where data might still be buffered.
  • Prevents potential issues with lingering resources in some edge cases.
    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!

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-asynciotype-featureA feature request or enhancement

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions