Skip to content
30 changes: 24 additions & 6 deletions Lib/asyncio/selector_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,32 @@ def close(self):
self._selector.close()
self._selector = None

def _close_self_pipe(self):
self._remove_reader(self._ssock.fileno())
self._ssock.close()
self._ssock = None
self._csock.close()
self._csock = None
def _close_self_pipe(self, shutdown=False):
if self._ssock is not None:
self._remove_reader(self._ssock.fileno())
if shutdown:
try:
self._ssock.shutdown(socket.SHUT_RDWR)
except OSError as e:
# Log the error with more context
print(f"Error shutting down _ssock (fileno={self._ssock.fileno()}): {e}")
self._ssock.close()
self._ssock = None

if self._csock is not None:
if shutdown:
try:
self._csock.shutdown(socket.SHUT_RDWR)
except OSError as e:
# Log the error with more context
print(f"Error shutting down _csock (fileno={self._csock.fileno()}): {e}")
self._csock.close()
self._csock = None

self._internal_fds -= 1



def _make_self_pipe(self):
# A self-socket, really. :-)
self._ssock, self._csock = socket.socketpair()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Type: Enhancement
Title: Refactor :code:`_close_self_pipe` for conditional socket shutdown in :code:`asyncio/selector_events.py`
Issue: :gh:`130850`

Detailed changes:
Python's :code:`asyncio` module has been enhanced with a refactored :code:`_close_self_pipe` function in :code:`selector_events.py`. This update introduces a :code:`shutdown` parameter, allowing developers to choose between a graceful socket shutdown or a standard close operation, thereby offering greater control over socket management within asynchronous applications.
Loading