except ImportError:
_set_nonblocking = None # type: ignore
-# 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)
-
-if hasattr(errno, "WSAEWOULDBLOCK"):
- _ERRNO_WOULDBLOCK += (errno.WSAEWOULDBLOCK,) # type: ignore
-
# 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, errno.ETIMEDOUT)
# instead of an unexpected error.
_ERRNO_CONNRESET += (errno.EPROTOTYPE,) # type: ignore
-# More non-portable errnos:
-_ERRNO_INPROGRESS = (errno.EINPROGRESS,)
-
-if hasattr(errno, "WSAEINPROGRESS"):
- _ERRNO_INPROGRESS += (errno.WSAEINPROGRESS,) # type: ignore
-
_WINDOWS = sys.platform.startswith("win")
buf = bytearray(self.read_chunk_size)
bytes_read = self.read_from_fd(buf)
except (socket.error, IOError, OSError) as e:
- if errno_from_exception(e) == errno.EINTR:
- continue
# ssl.SSLError is a subclass of socket.error
if self._is_connreset(e):
# Treat ECONNRESET as a connection close rather than
break
self._write_buffer.advance(num_bytes)
self._total_write_done_index += num_bytes
+ except BlockingIOError:
+ break
except (socket.error, IOError, OSError) as e:
- if e.args[0] in _ERRNO_WOULDBLOCK:
- break
- else:
- if not self._is_connreset(e):
- # Broken pipe errors are usually caused by connection
- # reset, and its better to not log EPIPE errors to
- # minimize log spam
- gen_log.warning("Write error on %s: %s", self.fileno(), e)
- self.close(exc_info=e)
- return
+ if not self._is_connreset(e):
+ # Broken pipe errors are usually caused by connection
+ # reset, and its better to not log EPIPE errors to
+ # minimize log spam
+ gen_log.warning("Write error on %s: %s", self.fileno(), e)
+ self.close(exc_info=e)
+ return
while self._write_futures:
index, future = self._write_futures[0]
def read_from_fd(self, buf: Union[bytearray, memoryview]) -> Optional[int]:
try:
return self.socket.recv_into(buf, len(buf))
- except socket.error as e:
- if e.args[0] in _ERRNO_WOULDBLOCK:
- return None
- else:
- raise
+ except BlockingIOError:
+ return None
finally:
del buf
self._connect_future = typing.cast("Future[IOStream]", future)
try:
self.socket.connect(address)
- except socket.error as e:
+ except BlockingIOError:
# In non-blocking mode we expect connect() to raise an
# exception with EINPROGRESS or EWOULDBLOCK.
- #
+ pass
+ except socket.error as e:
# On freebsd, other errors such as ECONNREFUSED may be
# returned immediately when attempting to connect to
# localhost, so handle them the same way as an error
# reported later in _handle_connect.
- if (
- errno_from_exception(e) not in _ERRNO_INPROGRESS
- and errno_from_exception(e) not in _ERRNO_WOULDBLOCK
- ):
- if future is None:
- gen_log.warning(
- "Connect error on fd %s: %s", self.socket.fileno(), e
- )
- self.close(exc_info=e)
- return future
+ if future is None:
+ gen_log.warning("Connect error on fd %s: %s", self.socket.fileno(), e)
+ self.close(exc_info=e)
+ return future
self._add_io_state(self.io_loop.WRITE)
return future
return None
else:
raise
- except socket.error as e:
- if e.args[0] in _ERRNO_WOULDBLOCK:
- return None
- else:
- raise
+ except BlockingIOError:
+ return None
finally:
del buf
# For undiagnosed reasons, 'latin1' codec may also need to be preloaded.
u"foo".encode("latin1")
-# 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)
-
-if hasattr(errno, "WSAEWOULDBLOCK"):
- _ERRNO_WOULDBLOCK += (errno.WSAEWOULDBLOCK,) # type: ignore
-
# Default backlog used when calling sock.listen()
_DEFAULT_BACKLOG = 128
sock.setblocking(False)
try:
st = os.stat(file)
- except OSError as err:
- if errno_from_exception(err) != errno.ENOENT:
- raise
+ except FileNotFoundError:
+ pass
else:
if stat.S_ISSOCK(st.st_mode):
os.remove(file)
return
try:
connection, address = sock.accept()
- except socket.error as e:
- # _ERRNO_WOULDBLOCK indicate we have accepted every
+ except BlockingIOError:
+ # EWOULDBLOCK indicates we have accepted every
# connection that is available.
- if errno_from_exception(e) in _ERRNO_WOULDBLOCK:
- return
+ return
+ except ConnectionAbortedError:
# ECONNABORTED indicates that there was a connection
# but it was closed while still in the accept queue.
# (observed on FreeBSD).
- if errno_from_exception(e) == errno.ECONNABORTED:
- continue
- raise
+ continue
set_close_exec(connection.fileno())
callback(connection, address)
the server into multiple processes and managing subprocesses.
"""
-import errno
import os
import multiprocessing
import signal
from tornado.iostream import PipeIOStream
from tornado.log import gen_log
from tornado.platform.auto import set_close_exec
-from tornado.util import errno_from_exception
import typing
from typing import Tuple, Optional, Any, Callable
return id
num_restarts = 0
while children:
- try:
- pid, status = os.wait()
- except OSError as e:
- if errno_from_exception(e) == errno.EINTR:
- continue
- raise
+ pid, status = os.wait()
if pid not in children:
continue
id = children.pop(pid)
def _try_cleanup_process(cls, pid: int) -> None:
try:
ret_pid, status = os.waitpid(pid, os.WNOHANG)
- except OSError as e:
- if errno_from_exception(e) == errno.ECHILD:
- return
+ except ChildProcessError:
+ return
if ret_pid == 0:
return
assert ret_pid == pid