Skip to content

Commit 0bba27c

Browse files
committed
Implement retry logic when the npipe open procedure fails
with ERROR_PIPE_BUSY Signed-off-by: Joffrey F <[email protected]>
1 parent 20be7d5 commit 0bba27c

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

docker/transport/npipesocket.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
import win32file
66
import win32pipe
77

8+
cERROR_PIPE_BUSY = 0xe7
89
cSECURITY_SQOS_PRESENT = 0x100000
910
cSECURITY_ANONYMOUS = 0
10-
cPIPE_READMODE_MESSAGE = 2
11+
12+
RETRY_WAIT_TIMEOUT = 10000
1113

1214

1315
def check_closed(f):
@@ -45,15 +47,27 @@ def close(self):
4547
@check_closed
4648
def connect(self, address):
4749
win32pipe.WaitNamedPipe(address, self._timeout)
48-
handle = win32file.CreateFile(
49-
address,
50-
win32file.GENERIC_READ | win32file.GENERIC_WRITE,
51-
0,
52-
None,
53-
win32file.OPEN_EXISTING,
54-
cSECURITY_ANONYMOUS | cSECURITY_SQOS_PRESENT,
55-
0
56-
)
50+
try:
51+
handle = win32file.CreateFile(
52+
address,
53+
win32file.GENERIC_READ | win32file.GENERIC_WRITE,
54+
0,
55+
None,
56+
win32file.OPEN_EXISTING,
57+
cSECURITY_ANONYMOUS | cSECURITY_SQOS_PRESENT,
58+
0
59+
)
60+
except win32pipe.error as e:
61+
# See Remarks:
62+
# https://msdn.microsoft.com/en-us/library/aa365800.aspx
63+
if e.winerror == cERROR_PIPE_BUSY:
64+
# Another program or thread has grabbed our pipe instance
65+
# before we got to it. Wait for availability and attempt to
66+
# connect again.
67+
win32pipe.WaitNamedPipe(address, RETRY_WAIT_TIMEOUT)
68+
return self.connect(address)
69+
raise e
70+
5771
self.flags = win32pipe.GetNamedPipeInfo(handle)[0]
5872

5973
self._handle = handle

0 commit comments

Comments
 (0)