Skip to content

Commit b044961

Browse files
committed
Fix the second half of bugs.python.org/issue21447: race in _write_to_self().
1 parent 1fe19c2 commit b044961

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

asyncio/selector_events.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,17 @@ def _read_from_self(self):
8787
pass
8888

8989
def _write_to_self(self):
90-
try:
91-
self._csock.send(b'x')
92-
except (BlockingIOError, InterruptedError):
93-
pass
90+
# This may be called from a different thread, possibly after
91+
# _close_self_pipe() has been called or even while it is
92+
# running. Guard for self._csock being None or closed. When
93+
# a socket is closed, send() raises OSError (with errno set to
94+
# EBADF, but let's not rely on the exact error code).
95+
csock = self._csock
96+
if csock is not None:
97+
try:
98+
csock.send(b'x')
99+
except OSError:
100+
pass
94101

95102
def _start_serving(self, protocol_factory, sock,
96103
sslcontext=None, server=None):

tests/test_selector_events.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,9 @@ def test_write_to_self_tryagain(self):
121121
self.assertIsNone(self.loop._write_to_self())
122122

123123
def test_write_to_self_exception(self):
124-
self.loop._csock.send.side_effect = OSError()
125-
self.assertRaises(OSError, self.loop._write_to_self)
124+
# _write_to_self() swallows OSError
125+
self.loop._csock.send.side_effect = RuntimeError()
126+
self.assertRaises(RuntimeError, self.loop._write_to_self)
126127

127128
def test_sock_recv(self):
128129
sock = mock.Mock()

0 commit comments

Comments
 (0)