summaryrefslogtreecommitdiffstats
path: root/Lib/asyncio/proactor_events.py
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2020-09-12 06:50:18 (GMT)
committerGitHub <noreply@github.com>2020-09-12 06:50:18 (GMT)
commit1b0f0e3d7d03155da1cf9769a847874d559e57e3 (patch)
tree4668e74414cfc8b360dc2c68b7233766b54906ab /Lib/asyncio/proactor_events.py
parent7e711ead26fea6465e0ef2e3b8880b57ba8fc129 (diff)
downloadcpython-1b0f0e3d7d03155da1cf9769a847874d559e57e3.zip
cpython-1b0f0e3d7d03155da1cf9769a847874d559e57e3.tar.gz
cpython-1b0f0e3d7d03155da1cf9769a847874d559e57e3.tar.bz2
bpo-39651: Fix asyncio proactor _write_to_self() (GH-22197)
Fix a race condition in the call_soon_threadsafe() method of asyncio.ProactorEventLoop: do nothing if the self-pipe socket has been closed.
Diffstat (limited to 'Lib/asyncio/proactor_events.py')
-rw-r--r--Lib/asyncio/proactor_events.py11
1 files changed, 10 insertions, 1 deletions
diff --git a/Lib/asyncio/proactor_events.py b/Lib/asyncio/proactor_events.py
index 4670bd6..45c11ee 100644
--- a/Lib/asyncio/proactor_events.py
+++ b/Lib/asyncio/proactor_events.py
@@ -793,8 +793,17 @@ class BaseProactorEventLoop(base_events.BaseEventLoop):
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 "