Commit 249d7521 authored by Denis Bilenko's avatar Denis Bilenko

fix issue #148: EINVAL is randomly raised on Mac OS X by os.read/os.write.

As a benign side-effect, if read or write is interrupted with EINTR, hub.wait() will be
called before calling read/write again. (previously read/write was called immediatelly)
parent 698ff289
...@@ -22,7 +22,14 @@ if fcntl is None: ...@@ -22,7 +22,14 @@ if fcntl is None:
else: else:
from gevent.socket import _fileobject, EAGAIN, _get_memory from gevent.socket import _fileobject, EAGAIN, _get_memory
from errno import EINTR import errno
ignore_errors = [EAGAIN, errno.EINTR]
if sys.platform == 'darwin':
# EINVAL sometimes happens on macosx without reason
# http://code.google.com/p/gevent/issues/detail?id=148
ignore_errors.append(errno.EINVAL)
cancel_wait_ex = IOError(EBADF, 'File descriptor was closed in another greenlet') cancel_wait_ex = IOError(EBADF, 'File descriptor was closed in another greenlet')
try: try:
...@@ -100,10 +107,7 @@ else: ...@@ -100,10 +107,7 @@ else:
bytes_written += os.write(fileno, _get_memory(data, bytes_written)) bytes_written += os.write(fileno, _get_memory(data, bytes_written))
except (IOError, OSError): except (IOError, OSError):
code = sys.exc_info()[1].args[0] code = sys.exc_info()[1].args[0]
if code == EINTR: if code not in ignore_errors:
sys.exc_clear()
continue
elif code != EAGAIN:
raise raise
sys.exc_clear() sys.exc_clear()
self.hub.wait(self._write_event) self.hub.wait(self._write_event)
...@@ -114,12 +118,11 @@ else: ...@@ -114,12 +118,11 @@ else:
data = os.read(self.fileno(), size) data = os.read(self.fileno(), size)
except (IOError, OSError): except (IOError, OSError):
code = sys.exc_info()[1].args[0] code = sys.exc_info()[1].args[0]
if code == EBADF: if code in ignore_errors:
pass
elif code == EBADF:
return '' return ''
elif code == EINTR: else:
sys.exc_clear()
continue
elif code != EAGAIN:
raise raise
sys.exc_clear() sys.exc_clear()
else: else:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment