From c73a6dd6d624130e9ef5e0d7295899303c5bd878 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Wed, 30 Nov 2022 23:41:40 +0100 Subject: [PATCH] socket: handle npipe close on Windows (https://github.com/docker/docker-py/pull/3056) Fixes https://github.com/docker/docker-py/issues/3045 Cherry-picked from https://github.com/docker/docker-py/commit/30022984f6445fbc322cbe97bb99aab1ddb1e4fd Co-authored-by: Nick Santos --- plugins/module_utils/_api/utils/socket.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/plugins/module_utils/_api/utils/socket.py b/plugins/module_utils/_api/utils/socket.py index b47210223..81c0c4f87 100644 --- a/plugins/module_utils/_api/utils/socket.py +++ b/plugins/module_utils/_api/utils/socket.py @@ -29,6 +29,11 @@ class SocketError(Exception): pass +# NpipeSockets have their own error types +# pywintypes.error: (109, 'ReadFile', 'The pipe has been ended.') +NPIPE_ENDED = 109 + + def read(socket, n=4096): """ Reads at most n bytes from socket @@ -48,6 +53,15 @@ def read(socket, n=4096): except EnvironmentError as e: if e.errno not in recoverable_errors: raise + except Exception as e: + is_pipe_ended = (isinstance(socket, NpipeSocket) and + len(e.args) > 0 and + e.args[0] == NPIPE_ENDED) + if is_pipe_ended: + # npipes don't support duplex sockets, so we interpret + # a PIPE_ENDED error as a close operation (0-length read). + return 0 + raise def read_exactly(socket, n):