]> granicus.if.org Git - python/commitdiff
bpo-30329: Catch Windows error 10022 on shutdown() (#1538) (#1620)
authorVictor Stinner <victor.stinner@gmail.com>
Tue, 16 May 2017 22:29:41 +0000 (15:29 -0700)
committerGitHub <noreply@github.com>
Tue, 16 May 2017 22:29:41 +0000 (15:29 -0700)
Catch the Windows socket WSAEINVAL error (code 10022) in imaplib and
poplib on shutdown(SHUT_RDWR): An invalid operation was attempted

This error occurs sometimes on SSL connections.
(cherry picked from commit 83a2c2879839da2e10037f5e4af1bd1dafbf1a52)

Lib/imaplib.py
Lib/poplib.py
Misc/NEWS

index 2fa90120e7decc9de8ccd61b16ba4c8eb04f5588..1c0b03bff8a3792c2fda3ca566690889daa1b4be 100644 (file)
@@ -318,9 +318,12 @@ class IMAP4:
         self.file.close()
         try:
             self.sock.shutdown(socket.SHUT_RDWR)
-        except OSError as e:
-            # The server might already have closed the connection
-            if e.errno != errno.ENOTCONN:
+        except OSError as exc:
+            # The server might already have closed the connection.
+            # On Windows, this may result in WSAEINVAL (error 10022):
+            # An invalid operation was attempted.
+            if (exc.errno != errno.ENOTCONN
+               and getattr(exc, 'winerror', 0) != 10022):
                 raise
         finally:
             self.sock.close()
index cae6950eb6d2d6c980bbdf7f6aeb507a35e88b7e..6bcfa5cfeba37bfb9ce06da52ec905c8d4fbc513 100644 (file)
@@ -288,9 +288,12 @@ class POP3:
             if sock is not None:
                 try:
                     sock.shutdown(socket.SHUT_RDWR)
-                except OSError as e:
-                    # The server might already have closed the connection
-                    if e.errno != errno.ENOTCONN:
+                except OSError as exc:
+                    # The server might already have closed the connection.
+                    # On Windows, this may result in WSAEINVAL (error 10022):
+                    # An invalid operation was attempted.
+                    if (exc.errno != errno.ENOTCONN
+                       and getattr(exc, 'winerror', 0) != 10022):
                         raise
                 finally:
                     sock.close()
index ca38fe0e5cdf362254d8dd81df23d8f837b7ef1d..6a50c6265c5cf17e6ade2457c5f9f4bdc67aeca0 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -36,6 +36,10 @@ Core and Builtins
 Library
 -------
 
+- bpo-30329: imaplib and poplib now catch the Windows socket WSAEINVAL error
+  (code 10022) on shutdown(SHUT_RDWR): An invalid operation was attempted.
+  This error occurs sometimes on SSL connections.
+
 - bpo-30375: Warnings emitted when compile a regular expression now always
   point to the line in the user code.  Previously they could point into inners
   of the re module if emitted from inside of groups or conditionals.