Skip to content

Commit

Permalink
bpo-30329: Catch Windows error 10022 on shutdown() (#1538)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vstinner committed May 15, 2017
1 parent edef358 commit 83a2c28
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
9 changes: 6 additions & 3 deletions Lib/imaplib.py
Expand Up @@ -318,9 +318,12 @@ def shutdown(self):
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()
Expand Down
9 changes: 6 additions & 3 deletions Lib/poplib.py
Expand Up @@ -288,9 +288,12 @@ def close(self):
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()
Expand Down
4 changes: 4 additions & 0 deletions Misc/NEWS
Expand Up @@ -323,6 +323,10 @@ Extension Modules
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-29196: Removed previously deprecated in Python 2.4 classes Plist, Dict
and _InternalDict in the plistlib module. Dict values in the result of
functions readPlist() and readPlistFromBytes() are now normal dicts. You
Expand Down

0 comments on commit 83a2c28

Please sign in to comment.