|
5 | 5 | import win32file |
6 | 6 | import win32pipe |
7 | 7 |
|
| 8 | +cERROR_PIPE_BUSY = 0xe7 |
8 | 9 | cSECURITY_SQOS_PRESENT = 0x100000 |
9 | 10 | cSECURITY_ANONYMOUS = 0 |
10 | | -cPIPE_READMODE_MESSAGE = 2 |
| 11 | + |
| 12 | +RETRY_WAIT_TIMEOUT = 10000 |
11 | 13 |
|
12 | 14 |
|
13 | 15 | def check_closed(f): |
@@ -45,15 +47,27 @@ def close(self): |
45 | 47 | @check_closed |
46 | 48 | def connect(self, address): |
47 | 49 | 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 | + |
57 | 71 | self.flags = win32pipe.GetNamedPipeInfo(handle)[0] |
58 | 72 |
|
59 | 73 | self._handle = handle |
|
0 commit comments