-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-137335: Fix unlikely name conflicts for named pipes in multiprocessing and asyncio on Windows #137389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-137335: Fix unlikely name conflicts for named pipes in multiprocessing and asyncio on Windows #137389
Changes from 3 commits
2a0a5e9
5b91a56
ed050a0
7363c99
5243b9e
81cb66e
5207bf6
cae80b9
de04089
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,11 +6,10 @@ | |
| raise ImportError('win32 only') | ||
|
|
||
| import _winapi | ||
| import itertools | ||
| import msvcrt | ||
| import os | ||
| import random | ||
| import subprocess | ||
| import tempfile | ||
| import warnings | ||
|
|
||
|
|
||
|
|
@@ -23,18 +22,13 @@ | |
| BUFSIZE = 8192 | ||
| PIPE = subprocess.PIPE | ||
| STDOUT = subprocess.STDOUT | ||
| _mmap_counter = itertools.count() | ||
|
|
||
|
|
||
| # Replacement for os.pipe() using handles instead of fds | ||
|
|
||
|
|
||
| def pipe(*, duplex=False, overlapped=(True, True), bufsize=BUFSIZE): | ||
| """Like os.pipe() but with overlapped support and using handles not fds.""" | ||
| address = tempfile.mktemp( | ||
| prefix=r'\\.\pipe\python-pipe-{:d}-{:d}-'.format( | ||
| os.getpid(), next(_mmap_counter))) | ||
|
|
||
| if duplex: | ||
| openmode = _winapi.PIPE_ACCESS_DUPLEX | ||
| access = _winapi.GENERIC_READ | _winapi.GENERIC_WRITE | ||
|
|
@@ -56,9 +50,17 @@ def pipe(*, duplex=False, overlapped=(True, True), bufsize=BUFSIZE): | |
|
|
||
| h1 = h2 = None | ||
| try: | ||
| h1 = _winapi.CreateNamedPipe( | ||
| address, openmode, _winapi.PIPE_WAIT, | ||
| 1, obsize, ibsize, _winapi.NMPWAIT_WAIT_FOREVER, _winapi.NULL) | ||
| while True: | ||
| address = r'\\.\pipe\python-pipe-' + random.randbytes(8).hex() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A comment: we could use Now, I'm actually worried that it if we're able to interact with the process that is creating the pipes, then we could actually recover enough samples from the underlying PRNG instance and get the original seed. But this is only if 1) there are no random calls in between and 2) we can get 624 consecutive 32-bit samples from the random source. Condition 2) already holds because random.randbytes(8) is actually equivalent to two calls of Therefore, I would still suggest using
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, I did not thought about this. Sorry for not answering immediately, I missed your comment. |
||
| try: | ||
| h1 = _winapi.CreateNamedPipe( | ||
| address, openmode, _winapi.PIPE_WAIT, | ||
| 1, obsize, ibsize, _winapi.NMPWAIT_WAIT_FOREVER, _winapi.NULL) | ||
| break | ||
| except OSError as e: | ||
| if e.winerror not in (_winapi.ERROR_PIPE_BUSY, | ||
| _winapi.ERROR_ACCESS_DENIED): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. an infinite loop retrying on this error could potentially hide an error if the system simply doesn't allow this process to create a pipe with the given address? (here and below)
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not know if this is even possible ( |
||
| raise | ||
|
|
||
| h2 = _winapi.CreateFile( | ||
| address, access, 0, _winapi.NULL, _winapi.OPEN_EXISTING, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Get rid of any possibility of a name conflict for named pipes in | ||
| :mod:`multiprocessing` and :mod:`asyncio` on Windows, no matter how small. |
Uh oh!
There was an error while loading. Please reload this page.