]> granicus.if.org Git - python/commitdiff
bpo-32622: Normalize ENOTCONN to ConnectionError on macOS (GH-5369)
authorYury Selivanov <yury@magic.io>
Sat, 27 Jan 2018 22:22:01 +0000 (17:22 -0500)
committerGitHub <noreply@github.com>
Sat, 27 Jan 2018 22:22:01 +0000 (17:22 -0500)
On mac, sendfile throws ENOTCONN on a repeated sendfile call if
the connection is closed. Normalize it to behave like other systems.

Lib/asyncio/unix_events.py

index a4d892acad05d4ce2e8e99b33420a30dd4e52c37..6cac137cacb31a723967a575a0fffeca8edcb533 100644 (file)
@@ -362,6 +362,17 @@ class _UnixSelectorEventLoop(selector_events.BaseSelectorEventLoop):
                             fd, sock, fileno,
                             offset, count, blocksize, total_sent)
         except OSError as exc:
+            if (registered_fd is not None and
+                    exc.errno == errno.ENOTCONN and
+                    type(exc) is not ConnectionError):
+                # If we have an ENOTCONN and this isn't a first call to
+                # sendfile(), i.e. the connection was closed in the middle
+                # of the operation, normalize the error to ConnectionError
+                # to make it consistent across all Posix systems.
+                new_exc = ConnectionError(
+                    "socket is not connected", errno.ENOTCONN)
+                new_exc.__cause__ = exc
+                exc = new_exc
             if total_sent == 0:
                 # We can get here for different reasons, the main
                 # one being 'file' is not a regular mmap(2)-like