Skip to content

Commit c9ba45d

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

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
@@ -310,9 +310,12 @@ def shutdown(self):
310310
self.file.close()
311311
try:
312312
self.sock.shutdown(socket.SHUT_RDWR)
313-
except OSError as e:
314-
# The server might already have closed the connection
315-
if e.errno != errno.ENOTCONN:
313+
except OSError as exc:
314+
# The server might already have closed the connection.
315+
# On Windows, this may result in WSAEINVAL (error 10022):
316+
# An invalid operation was attempted.
317+
if (exc.errno != errno.ENOTCONN
318+
and getattr(exc, 'winerror', 0) != 10022):
316319
raise
317320
finally:
318321
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
@@ -49,6 +49,10 @@ Extension Modules
4949
Library
5050
-------
5151

52+
- bpo-30329: imaplib and poplib now catch the Windows socket WSAEINVAL error
53+
(code 10022) on shutdown(SHUT_RDWR): An invalid operation was attempted.
54+
This error occurs sometimes on SSL connections.
55+
5256
- bpo-30375: Warnings emitted when compile a regular expression now always
5357
point to the line in the user code. Previously they could point into inners
5458
of the re module if emitted from inside of groups or conditionals.

0 commit comments

Comments
 (0)