Skip to content

Commit aaa0536

Browse files
authored
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 83a2c28)
1 parent 0d26704 commit aaa0536

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
@@ -36,6 +36,10 @@ Core and Builtins
3636
Library
3737
-------
3838

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

0 commit comments

Comments
 (0)