Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Prev Previous commit
Next Next commit
Limit the number of attempts.
  • Loading branch information
serhiy-storchaka committed Feb 10, 2026
commit cae80b95be20edaf386c942390b97d6caae2fb36
5 changes: 4 additions & 1 deletion Lib/asyncio/windows_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
PIPE = subprocess.PIPE
STDOUT = subprocess.STDOUT
_mmap_counter = itertools.count()
_MAX_PIPE_ATTEMPTS = 100
Comment thread
serhiy-storchaka marked this conversation as resolved.
Outdated


# Replacement for os.pipe() using handles instead of fds
Expand Down Expand Up @@ -51,7 +52,7 @@ def pipe(*, duplex=False, overlapped=(True, True), bufsize=BUFSIZE):

h1 = h2 = None
try:
while True:
for attempts in itertools.count():
address = r'\\.\pipe\python-pipe-{:d}-{:d}-{}'.format(
os.getpid(), next(_mmap_counter), os.urandom(8).hex())
try:
Expand All @@ -60,6 +61,8 @@ def pipe(*, duplex=False, overlapped=(True, True), bufsize=BUFSIZE):
1, obsize, ibsize, _winapi.NMPWAIT_WAIT_FOREVER, _winapi.NULL)
break
except OSError as e:
if attempts >= _MAX_PIPE_ATTEMPTS:
raise
if e.winerror not in (_winapi.ERROR_PIPE_BUSY,
_winapi.ERROR_ACCESS_DENIED):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not know if this is even possible (\\.\pipe\ is not a real directory which you can forbid access to), but it will not harm if limit the number of attempts.

raise
Expand Down
9 changes: 7 additions & 2 deletions Lib/multiprocessing/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
CONNECTION_TIMEOUT = 20.

_mmap_counter = itertools.count()
_MAX_PIPE_ATTEMPTS = 100

default_family = 'AF_INET'
families = ['AF_INET']
Expand Down Expand Up @@ -480,12 +481,14 @@ def __init__(self, address=None, family=None, backlog=1, authkey=None):
if address:
self._listener = PipeListener(address, backlog)
else:
while True:
for attempts in itertools.count():
address = arbitrary_address(family)
try:
self._listener = PipeListener(address, backlog)
break
except OSError as e:
if attempts >= _MAX_PIPE_ATTEMPTS:
raise
if e.winerror not in (_winapi.ERROR_PIPE_BUSY,
_winapi.ERROR_ACCESS_DENIED):
raise
Expand Down Expand Up @@ -589,7 +592,7 @@ def Pipe(duplex=True):
access = _winapi.GENERIC_WRITE
obsize, ibsize = 0, BUFSIZE

while True:
for attempts in itertools.count():
address = arbitrary_address('AF_PIPE')
try:
h1 = _winapi.CreateNamedPipe(
Expand All @@ -603,6 +606,8 @@ def Pipe(duplex=True):
)
break
except OSError as e:
if attempts >= _MAX_PIPE_ATTEMPTS:
raise
if e.winerror not in (_winapi.ERROR_PIPE_BUSY,
_winapi.ERROR_ACCESS_DENIED):
raise
Expand Down
Loading