Skip to content

Instantly share code, notes, and snippets.

@6220119
Created January 23, 2018 07:41
Show Gist options
  • Save 6220119/54a03f1fc6cbfa70511d782e53201009 to your computer and use it in GitHub Desktop.
Save 6220119/54a03f1fc6cbfa70511d782e53201009 to your computer and use it in GitHub Desktop.
Fix the BlockingIOError on Python for Windows
--- proactor_events.py 2018-01-23 13:55:46.665000000 +0700
+++ proactor_events_fixed.py 2018-01-23 12:22:08.319000000 +0700
@@ -493,7 +493,23 @@
f.add_done_callback(self._loop_self_reading)
def _write_to_self(self):
- self._csock.send(b'\0')
+ # Original code
+ # self._csock.send(b'\0')
+
+ # 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'\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):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment