diff --git a/Lib/test/test_perf_profiler.py b/Lib/test/test_perf_profiler.py index 597e6599352049..ae6bfe513eba81 100644 --- a/Lib/test/test_perf_profiler.py +++ b/Lib/test/test_perf_profiler.py @@ -116,11 +116,14 @@ 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: _, status = os.waitpid(-1, 0) - sys.exit(status) + if os.WIFSIGNALED(status): + sys.exit(1) + sys.exit(os.WEXITSTATUS(status)) def bar(): foo() 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()``.