Skip to content

Commit 83a2c28

Browse files
authored
bpo-30329: Catch Windows error 10022 on shutdown() (#1538)
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.
1 parent edef358 commit 83a2c28

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

Lib/imaplib.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,12 @@ def shutdown(self):
318318
self.file.close()
319319
try:
320320
self.sock.shutdown(socket.SHUT_RDWR)
321-
except OSError as e:
322-
# The server might already have closed the connection
323-
if e.errno != errno.ENOTCONN:
321+
except OSError as exc:
322+
# The server might already have closed the connection.
323+
# On Windows, this may result in WSAEINVAL (error 10022):
324+
# An invalid operation was attempted.
325+
if (exc.errno != errno.ENOTCONN
326+
and getattr(exc, 'winerror', 0) != 10022):
324327
raise
325328
finally:
326329
self.sock.close()

Lib/poplib.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,12 @@ def close(self):
288288
if sock is not None:
289289
try:
290290
sock.shutdown(socket.SHUT_RDWR)
291-
except OSError as e:
292-
# The server might already have closed the connection
293-
if e.errno != errno.ENOTCONN:
291+
except OSError as exc:
292+
# The server might already have closed the connection.
293+
# On Windows, this may result in WSAEINVAL (error 10022):
294+
# An invalid operation was attempted.
295+
if (exc.errno != errno.ENOTCONN
296+
and getattr(exc, 'winerror', 0) != 10022):
294297
raise
295298
finally:
296299
sock.close()

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,10 @@ Extension Modules
323323
Library
324324
-------
325325

326+
- bpo-30329: imaplib and poplib now catch the Windows socket WSAEINVAL error
327+
(code 10022) on shutdown(SHUT_RDWR): An invalid operation was attempted.
328+
This error occurs sometimes on SSL connections.
329+
326330
- bpo-29196: Removed previously deprecated in Python 2.4 classes Plist, Dict
327331
and _InternalDict in the plistlib module. Dict values in the result of
328332
functions readPlist() and readPlistFromBytes() are now normal dicts. You

0 commit comments

Comments
 (0)