Skip to content

[3.9] bpo-39651: Fix asyncio proactor _write_to_self() (GH-22197) #22216

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

Merged
merged 1 commit into from
Sep 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Lib/asyncio/proactor_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -791,8 +791,17 @@ def _loop_self_reading(self, f=None):
f.add_done_callback(self._loop_self_reading)

def _write_to_self(self):
# This may be called from a different thread, possibly after
# _close_self_pipe() has been called or even while it is
# running. Guard for self._csock being None or closed. When
# a socket is closed, send() raises OSError (with errno set to
# EBADF, but let's not rely on the exact error code).
csock = self._csock
if csock is None:
return

try:
self._csock.send(b'\0')
csock.send(b'\0')
except OSError:
if self._debug:
logger.debug("Fail to write a null byte into the "
Expand Down
18 changes: 10 additions & 8 deletions Lib/asyncio/selector_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,16 @@ def _write_to_self(self):
# a socket is closed, send() raises OSError (with errno set to
# EBADF, but let's not rely on the exact error code).
csock = self._csock
if csock is not None:
try:
csock.send(b'\0')
except OSError:
if self._debug:
logger.debug("Fail to write a null byte into the "
"self-pipe socket",
exc_info=True)
if csock is None:
return

try:
csock.send(b'\0')
except OSError:
if self._debug:
logger.debug("Fail to write a null byte into the "
"self-pipe socket",
exc_info=True)

def _start_serving(self, protocol_factory, sock,
sslcontext=None, server=None, backlog=100,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a race condition in the ``call_soon_threadsafe()`` method of
``asyncio.ProactorEventLoop``: do nothing if the self-pipe socket has been
closed.