Skip to content
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-94026: Workaround for BlockingIOError in runtest_mp on Emscripten #94164

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion Lib/test/libregrtest/runtest_mp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os.path
import queue
import signal
import select
import subprocess
import sys
import tempfile
Expand Down Expand Up @@ -97,10 +98,39 @@ def run_tests_worker(ns: Namespace, test_name: str) -> NoReturn:
print() # Force a newline (just in case)

# Serialize TestResult as dict in JSON
print(json.dumps(result, cls=EncodeTestResult), flush=True)
dump_result(json.dumps(result, cls=EncodeTestResult), sys.stdout)
sys.exit(0)


def dump_result(data, stream):
"""Dump result data to output stream

Workaround for non-blocking pipes on wasm32-emscripten, GH-94026.

Writes with len(data) <= PIPE_BUF either succeed or fail with EAGAIN
without writing any data. See man write(3) for more details.
"""
data = data.encode(stream.encoding)
# flush buffers, just in case
stream.flush()
Copy link
Member

Choose a reason for hiding this comment

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

It can fail here.

fd = stream.fileno()
while data:
exc = None
cur = data[:select.PIPE_BUF]
for _ in range(10):
# retry 10 times with 100 ms magic sleep between retries
try:
written = os.write(fd, cur)
except BlockingIOError as e:
exc = e
time.sleep(0.1)
else:
break
else:
raise exc from None
data = data[written:]


# We do not use a generator so multiple threads can call next().
class MultiprocessIterator:

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
``runtest_mp`` test worker now handles :exc:`BlockingIOError` to work around
non-blocking pipes on Emscripten.