except ImportError:
_set_nonblocking = None
+# These errnos indicate that a non-blocking operation must be retried
+# at a later time. On most platforms they're the same value, but on
+# some they differ.
+_ERRNO_WOULDBLOCK = (errno.EWOULDBLOCK, errno.EAGAIN)
+
+# These errnos indicate that a connection has been abruptly terminated.
+# They should be caught and handled less noisily than other errors.
+_ERRNO_CONNRESET = (errno.ECONNRESET, errno.ECONNABORTED, errno.EPIPE)
class StreamClosedError(IOError):
"""Exception raised by `IOStream` methods when the stream is closed.
chunk = self.read_from_fd()
except (socket.error, IOError, OSError) as e:
# ssl.SSLError is a subclass of socket.error
- if e.args[0] == errno.ECONNRESET:
+ if e.args[0] in _ERRNO_CONNRESET:
# Treat ECONNRESET as a connection close rather than
# an error to minimize log spam (the exception will
# be available on self.error for apps that care).
_merge_prefix(self._write_buffer, num_bytes)
self._write_buffer.popleft()
except socket.error as e:
- if e.args[0] in (errno.EWOULDBLOCK, errno.EAGAIN):
+ if e.args[0] in _ERRNO_WOULDBLOCK:
self._write_buffer_frozen = True
break
else:
- if e.args[0] not in (errno.EPIPE, errno.ECONNRESET):
+ if e.args[0] not in _ERRNO_CONNRESET:
# Broken pipe errors are usually caused by connection
# reset, and its better to not log EPIPE errors to
# minimize log spam
try:
chunk = self.socket.recv(self.read_chunk_size)
except socket.error as e:
- if e.args[0] in (errno.EWOULDBLOCK, errno.EAGAIN):
+ if e.args[0] in _ERRNO_WOULDBLOCK:
return None
else:
raise
# returned immediately when attempting to connect to
# localhost, so handle them the same way as an error
# reported later in _handle_connect.
- if e.args[0] not in (errno.EINPROGRESS, errno.EWOULDBLOCK):
+ if (e.args[0] != errno.EINPROGRESS and
+ e.args[0] not in _ERRNO_WOULDBLOCK):
gen_log.warning("Connect error on fd %d: %s",
self.socket.fileno(), e)
self.close(exc_info=True)
return self.close(exc_info=True)
raise
except socket.error as err:
- if err.args[0] in (errno.ECONNABORTED, errno.ECONNRESET):
+ if err.args[0] in _ERRNO_CONNRESET:
return self.close(exc_info=True)
except AttributeError:
# On Linux, if the connection was reset before the call to
else:
raise
except socket.error as e:
- if e.args[0] in (errno.EWOULDBLOCK, errno.EAGAIN):
+ if e.args[0] in _ERRNO_WOULDBLOCK:
return None
else:
raise
try:
chunk = os.read(self.fd, self.read_chunk_size)
except (IOError, OSError) as e:
- if e.args[0] in (errno.EWOULDBLOCK, errno.EAGAIN):
+ if e.args[0] in _ERRNO_WOULDBLOCK:
return None
elif e.args[0] == errno.EBADF:
# If the writing half of a pipe is closed, select will