diff --git a/Lib/asyncio/selector_events.py b/Lib/asyncio/selector_events.py index 22147451fa7ebd..c041b638c3ac90 100644 --- a/Lib/asyncio/selector_events.py +++ b/Lib/asyncio/selector_events.py @@ -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() diff --git a/Misc/NEWS.d/next/Library/2025-03-05-01-03-59.gh-issue-130850.FmU_5n.rst b/Misc/NEWS.d/next/Library/2025-03-05-01-03-59.gh-issue-130850.FmU_5n.rst new file mode 100644 index 00000000000000..6ea08e8e94c5df --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-03-05-01-03-59.gh-issue-130850.FmU_5n.rst @@ -0,0 +1,3 @@ +Issue: :gh:`130850` + +Detailed changes: Modified _close_self_pipe function in selector_events.py in python/asyncio. This update introduces a shutdown parameter, allowing for either a socket shutdown or the default socket close