Skip to content

gh-159534: Fix race condition in test_sampling_profiler - #160028

Merged
vstinner merged 2 commits into
python:mainfrom
vstinner:test_sampling_profiler
Oct 21, 2026
Merged

vstinner merged 2 commits into
python:mainfrom
vstinner:test_sampling_profiler

Conversation

@vstinner

@vstinner vstinner commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

Fix test_is_python_process_non_python_subprocess(). Wait until the forked process calls exec() before calling is_python_process(). Previously, is_python_build() returned True if exec() was not called yet in the forked process.

Fix test_is_python_process_non_python_subprocess(). Wait until the
forked process calls exec() before calling is_python_process().
Previously, is_python_build() returned True if exec() was not called
yet in the forked process.
if os.path.exists(f"/proc/{proc.pid}/exe"):
return True
else:
return True

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

On non-Linux platforms, this function basically did nothing. So in my change, I only call the function on Linux.


try:
if sys.platform == "linux":
if os.path.exists(f"/proc/{proc.pid}/exe"):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm not sure of the purpose of the old test. Just after fork, /proc/pid/exe is a symlink to the Python executable (and it does exist). After fork+exec, /proc/pid/exe is a symlink to /usr/bin/sleep (which also exists). I don't see when /proc/pid/exe doesn't exist.

@vstinner

Copy link
Copy Markdown
Member Author

cc @pablogsal

@vstinner

Copy link
Copy Markdown
Member Author

With this change, ./python -m test test_profiling.test_sampling_profiler.test_children -m test_is_python_process_non_python_subprocess -F no longer fails on AMD64 Debian root 3.x buildbot. I ran the test 4460 times in 5min47.

_wait_for_process_ready(proc, SHORT_TIMEOUT),
f"Process {proc.pid} should be ready within {SHORT_TIMEOUT}s",
)
if sys.platform == "linux":

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

There's

    @unittest.skipUnless(sys.platform == "linux", "Linux only test")

already.

@vstinner

Copy link
Copy Markdown
Member Author

It seems like Android failed during a download, it's a flaky failure:

  > Task :app:maxVersionCreate FAILED
                                                                                  Warning: An error occurred while preparing SDK package Android Emulator: Premature EOF.
  
  [======                                 ] 16% Downloading emulator-linux_x64-164
  FAILURE: Build failed with an exception.
  
  * What went wrong:
  Execution failed for task ':app:maxVersionCreate' (registered in build file 'app/build.gradle.kts').
  > Process 'command '/usr/local/lib/android/sdk/cmdline-tools/latest/bin/sdkmanager'' finished with non-zero exit value 1

@vstinner

Copy link
Copy Markdown
Member Author

The test failed again on AMD64 Debian root 3.x: https://buildbot.python.org/#/builders/345/builds/15753. So let me merge this fix.

@vstinner
vstinner merged commit 6fd15f0 into python:main Oct 21, 2026
49 of 50 checks passed
@vstinner
vstinner deleted the test_sampling_profiler branch October 21, 2026 17:56
@miss-islington-app

Copy link
Copy Markdown

Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

@bedevere-app

bedevere-app Bot commented Oct 21, 2026

Copy link
Copy Markdown

GH-160080 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label Oct 21, 2026
vstinner added a commit that referenced this pull request Oct 22, 2026
…0028) (#160080)

gh-159534: Fix race condition in test_sampling_profiler (GH-160028)

Fix test_is_python_process_non_python_subprocess(). Wait until the
forked Python process calls exec() before calling is_python_process().
Previously, is_python_build() returned True if exec() was not called
yet in the forked process.
(cherry picked from commit 6fd15f0)

Co-authored-by: Victor Stinner <vstinner@python.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip news tests Tests in the Lib/test dir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants