Skip to content

Commit 57c050e

Browse files
committed
Revert "[3.11] pythongh-94026: Buffer regrtest worker stdout in temporary file (pythonGH-94253) (pythonGH-94408)"
This reverts commit 0122ab2.
1 parent 9a03f9c commit 57c050e

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

Lib/test/libregrtest/runtest_mp.py

+26-28
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
import signal
66
import subprocess
77
import sys
8-
import tempfile
98
import threading
109
import time
1110
import traceback
12-
from typing import NamedTuple, NoReturn, Literal, Any, TextIO
11+
from typing import NamedTuple, NoReturn, Literal, Any
1312

1413
from test import support
1514
from test.support import os_helper
@@ -52,7 +51,7 @@ def parse_worker_args(worker_args) -> tuple[Namespace, str]:
5251
return (ns, test_name)
5352

5453

55-
def run_test_in_subprocess(testname: str, ns: Namespace, stdout_fh: TextIO) -> subprocess.Popen:
54+
def run_test_in_subprocess(testname: str, ns: Namespace) -> subprocess.Popen:
5655
ns_dict = vars(ns)
5756
worker_args = (ns_dict, testname)
5857
worker_args = json.dumps(worker_args)
@@ -68,17 +67,18 @@ def run_test_in_subprocess(testname: str, ns: Namespace, stdout_fh: TextIO) -> s
6867
# Running the child from the same working directory as regrtest's original
6968
# invocation ensures that TEMPDIR for the child is the same when
7069
# sysconfig.is_python_build() is true. See issue 15300.
71-
kw = dict(
72-
stdout=stdout_fh,
73-
# bpo-45410: Write stderr into stdout to keep messages order
74-
stderr=stdout_fh,
75-
text=True,
76-
close_fds=(os.name != 'nt'),
77-
cwd=os_helper.SAVEDCWD,
78-
)
70+
kw = {}
7971
if USE_PROCESS_GROUP:
8072
kw['start_new_session'] = True
81-
return subprocess.Popen(cmd, **kw)
73+
return subprocess.Popen(cmd,
74+
stdout=subprocess.PIPE,
75+
# bpo-45410: Write stderr into stdout to keep
76+
# messages order
77+
stderr=subprocess.STDOUT,
78+
universal_newlines=True,
79+
close_fds=(os.name != 'nt'),
80+
cwd=os_helper.SAVEDCWD,
81+
**kw)
8282

8383

8484
def run_tests_worker(ns: Namespace, test_name: str) -> NoReturn:
@@ -204,12 +204,12 @@ def mp_result_error(
204204
test_result.duration_sec = time.monotonic() - self.start_time
205205
return MultiprocessResult(test_result, stdout, err_msg)
206206

207-
def _run_process(self, test_name: str, stdout_fh: TextIO) -> int:
207+
def _run_process(self, test_name: str) -> tuple[int, str, str]:
208208
self.start_time = time.monotonic()
209209

210210
self.current_test_name = test_name
211211
try:
212-
popen = run_test_in_subprocess(test_name, self.ns, stdout_fh)
212+
popen = run_test_in_subprocess(test_name, self.ns)
213213

214214
self._killed = False
215215
self._popen = popen
@@ -226,10 +226,10 @@ def _run_process(self, test_name: str, stdout_fh: TextIO) -> int:
226226
raise ExitThread
227227

228228
try:
229-
# gh-94026: stdout+stderr are written to tempfile
230-
retcode = popen.wait(timeout=self.timeout)
229+
# bpo-45410: stderr is written into stdout
230+
stdout, _ = popen.communicate(timeout=self.timeout)
231+
retcode = popen.returncode
231232
assert retcode is not None
232-
return retcode
233233
except subprocess.TimeoutExpired:
234234
if self._stopped:
235235
# kill() has been called: communicate() fails on reading
@@ -244,12 +244,17 @@ def _run_process(self, test_name: str, stdout_fh: TextIO) -> int:
244244
# bpo-38207: Don't attempt to call communicate() again: on it
245245
# can hang until all child processes using stdout
246246
# pipes completes.
247+
stdout = ''
247248
except OSError:
248249
if self._stopped:
249250
# kill() has been called: communicate() fails
250251
# on reading closed stdout
251252
raise ExitThread
252253
raise
254+
else:
255+
stdout = stdout.strip()
256+
257+
return (retcode, stdout)
253258
except:
254259
self._kill()
255260
raise
@@ -259,17 +264,7 @@ def _run_process(self, test_name: str, stdout_fh: TextIO) -> int:
259264
self.current_test_name = None
260265

261266
def _runtest(self, test_name: str) -> MultiprocessResult:
262-
# gh-94026: Write stdout+stderr to a tempfile as workaround for
263-
# non-blocking pipes on Emscripten with NodeJS.
264-
with tempfile.TemporaryFile(
265-
'w+', encoding=sys.stdout.encoding
266-
) as stdout_fh:
267-
# gh-93353: Check for leaked temporary files in the parent process,
268-
# since the deletion of temporary files can happen late during
269-
# Python finalization: too late for libregrtest.
270-
retcode = self._run_process(test_name, stdout_fh)
271-
stdout_fh.seek(0)
272-
stdout = stdout_fh.read().strip()
267+
retcode, stdout = self._run_process(test_name)
273268

274269
if retcode is None:
275270
return self.mp_result_error(Timeout(test_name), stdout)
@@ -316,6 +311,9 @@ def run(self) -> None:
316311
def _wait_completed(self) -> None:
317312
popen = self._popen
318313

314+
# stdout must be closed to ensure that communicate() does not hang
315+
popen.stdout.close()
316+
319317
try:
320318
popen.wait(JOIN_TIMEOUT)
321319
except (subprocess.TimeoutExpired, OSError) as exc:

0 commit comments

Comments
 (0)