From d9b8d1329c19dbbd60a36d0e5e7efee06047531e Mon Sep 17 00:00:00 2001 From: yonatan-genai Date: Fri, 3 Apr 2026 23:58:46 +0300 Subject: [PATCH 1/2] gh-144766: Fix flaky test_trampoline_works_with_forks The fork child ran full Python finalization instead of calling os._exit(0), which is fragile when perf trampoline support is active (unmapping executable memory and unregistering code watchers during finalization can crash intermittently). The newer test added in the same file (test_trampoline_works_after_fork_with_many_code_objects) already uses os._exit(0) for this reason. Also fix the parent's wait status handling: os.waitpid returns a raw wait status, not an exit code. Use os.WEXITSTATUS to extract the actual exit code, and check os.WIFSIGNALED for signal deaths. --- Lib/test/test_perf_profiler.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_perf_profiler.py b/Lib/test/test_perf_profiler.py index 597e6599352049..91a83531d0fb75 100644 --- a/Lib/test/test_perf_profiler.py +++ b/Lib/test/test_perf_profiler.py @@ -118,9 +118,12 @@ def foo(): if pid == 0: print(os.getpid()) baz_fork() + os._exit(0) else: _, status = os.waitpid(-1, 0) - sys.exit(status) + if os.WIFSIGNALED(status): + sys.exit(1) + sys.exit(os.WEXITSTATUS(status)) def bar(): foo() From d9fa578036f3cbc0bca7e089b94a07a890b925f7 Mon Sep 17 00:00:00 2001 From: yonatan-genai Date: Sat, 4 Apr 2026 00:31:04 +0300 Subject: [PATCH 2/2] Add flush=True before os._exit and add NEWS entry os._exit() does not flush Python IO buffers, so the print(os.getpid()) output was lost when stdout is piped. Add flush=True to ensure the child PID reaches the parent. Also add the required NEWS entry. --- Lib/test/test_perf_profiler.py | 2 +- .../next/Tests/2026-04-04-00-00-00.gh-issue-144766.Yk3mPq.rst | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Tests/2026-04-04-00-00-00.gh-issue-144766.Yk3mPq.rst diff --git a/Lib/test/test_perf_profiler.py b/Lib/test/test_perf_profiler.py index 91a83531d0fb75..ae6bfe513eba81 100644 --- a/Lib/test/test_perf_profiler.py +++ b/Lib/test/test_perf_profiler.py @@ -116,7 +116,7 @@ def baz_fork(): def foo(): pid = os.fork() if pid == 0: - print(os.getpid()) + print(os.getpid(), flush=True) baz_fork() os._exit(0) else: diff --git a/Misc/NEWS.d/next/Tests/2026-04-04-00-00-00.gh-issue-144766.Yk3mPq.rst b/Misc/NEWS.d/next/Tests/2026-04-04-00-00-00.gh-issue-144766.Yk3mPq.rst new file mode 100644 index 00000000000000..6851b470e1ac97 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2026-04-04-00-00-00.gh-issue-144766.Yk3mPq.rst @@ -0,0 +1,4 @@ +Fix flaky :func:`test_trampoline_works_with_forks` by using ``os._exit(0)`` +in the fork child to avoid fragile Python finalization, and by flushing +stdout before exit. Also fix the parent's wait status handling to use +``os.WEXITSTATUS()``.