bpo-30329: Catch Windows error 10022 on shutdown() (#1538) (#1620)

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 83a2c28798)
This commit is contained in:
Victor Stinner 2017-05-16 15:29:41 -07:00 committed by GitHub
parent 0d267041c4
commit aaa0536525
3 changed files with 16 additions and 6 deletions

View file

@ -318,9 +318,12 @@ class IMAP4:
self.file.close() self.file.close()
try: try:
self.sock.shutdown(socket.SHUT_RDWR) self.sock.shutdown(socket.SHUT_RDWR)
except OSError as e: except OSError as exc:
# The server might already have closed the connection # The server might already have closed the connection.
if e.errno != errno.ENOTCONN: # 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 raise
finally: finally:
self.sock.close() self.sock.close()

View file

@ -288,9 +288,12 @@ class POP3:
if sock is not None: if sock is not None:
try: try:
sock.shutdown(socket.SHUT_RDWR) sock.shutdown(socket.SHUT_RDWR)
except OSError as e: except OSError as exc:
# The server might already have closed the connection # The server might already have closed the connection.
if e.errno != errno.ENOTCONN: # 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 raise
finally: finally:
sock.close() sock.close()

View file

@ -36,6 +36,10 @@ Core and Builtins
Library 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 - 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 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. of the re module if emitted from inside of groups or conditionals.