Skip to content

gh-144766: Fix flaky test_trampoline_works_with_forks#148056

Open
yonatan-genai wants to merge 3 commits intopython:mainfrom
yonatan-genai:gh-144766-fix-fork-test
Open

gh-144766: Fix flaky test_trampoline_works_with_forks#148056
yonatan-genai wants to merge 3 commits intopython:mainfrom
yonatan-genai:gh-144766-fix-fork-test

Conversation

@yonatan-genai
Copy link
Copy Markdown

Summary

test_trampoline_works_with_forks intermittently fails on macOS CI because the fork child runs full Python finalization instead of calling os._exit(0). Perf trampoline finalization in a forked child is fragile: it unmaps executable memory and unregisters code watchers while code objects are being destroyed, which can crash intermittently.

The newer test in the same file (test_trampoline_works_after_fork_with_many_code_objects, added in gh-144766) already uses os._exit(0) in the child for exactly this reason. This applies the same pattern to the older test.

Also fixes the parent's wait status handling: os.waitpid returns a raw 16-bit wait status, not an exit code. The old code passed this raw status to sys.exit(), which could produce incorrect exit codes. Now uses os.WEXITSTATUS() to extract the actual exit code and os.WIFSIGNALED() to detect signal deaths.

Changes

  • Lib/test/test_perf_profiler.py: Add os._exit(0) in fork child, fix wait status handling in parent

Discovered while testing #148050 (unrelated multiprocessing change that was blocked by this flaky test on macOS CI).

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.

<claude>
@bedevere-app bedevere-app bot added awaiting review tests Tests in the Lib/test dir labels Apr 3, 2026
@bedevere-app
Copy link
Copy Markdown

bedevere-app bot commented Apr 3, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

yonatan-genai and others added 2 commits April 4, 2026 00:31
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.

<claude>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review tests Tests in the Lib/test dir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant