asyncio: Fix the second half of issue #21447: race in _write_to_self().
This commit is contained in:
parent
9fafc9f79a
commit
3d139d8ed6
|
@ -87,9 +87,16 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def _write_to_self(self):
|
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 not None:
|
||||||
try:
|
try:
|
||||||
self._csock.send(b'x')
|
csock.send(b'x')
|
||||||
except (BlockingIOError, InterruptedError):
|
except OSError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def _start_serving(self, protocol_factory, sock,
|
def _start_serving(self, protocol_factory, sock,
|
||||||
|
|
|
@ -121,8 +121,9 @@ class BaseSelectorEventLoopTests(unittest.TestCase):
|
||||||
self.assertIsNone(self.loop._write_to_self())
|
self.assertIsNone(self.loop._write_to_self())
|
||||||
|
|
||||||
def test_write_to_self_exception(self):
|
def test_write_to_self_exception(self):
|
||||||
self.loop._csock.send.side_effect = OSError()
|
# _write_to_self() swallows OSError
|
||||||
self.assertRaises(OSError, self.loop._write_to_self)
|
self.loop._csock.send.side_effect = RuntimeError()
|
||||||
|
self.assertRaises(RuntimeError, self.loop._write_to_self)
|
||||||
|
|
||||||
def test_sock_recv(self):
|
def test_sock_recv(self):
|
||||||
sock = mock.Mock()
|
sock = mock.Mock()
|
||||||
|
|
Loading…
Reference in New Issue