From af7feddab6ff80c5a66a2d97f9594c59f68b0139 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Tue, 6 May 2014 14:42:40 -0700 Subject: asyncio: Fix the second half of issue #21447: race in _write_to_self(). --- Lib/asyncio/selector_events.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'Lib/asyncio') diff --git a/Lib/asyncio/selector_events.py b/Lib/asyncio/selector_events.py index 367c5fbe3f..c7df8d8dd0 100644 --- a/Lib/asyncio/selector_events.py +++ b/Lib/asyncio/selector_events.py @@ -87,10 +87,17 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop): pass def _write_to_self(self): - try: - self._csock.send(b'x') - except (BlockingIOError, InterruptedError): - pass + # 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: + csock.send(b'x') + except OSError: + pass def _start_serving(self, protocol_factory, sock, sslcontext=None, server=None): -- cgit v1.2.1