Skip to content

gh-109746: Make _thread.start_new_thread delete state of new thread on its startup failure - #109761

Merged
serhiy-storchaka merged 9 commits into
python:mainfrom
chgnrdv:threading-fix-failed-thread-state-cleanup
Nov 22, 2024
Merged

serhiy-storchaka merged 9 commits into
python:mainfrom
chgnrdv:threading-fix-failed-thread-state-cleanup

Conversation

@chgnrdv

@chgnrdv chgnrdv commented Sep 22, 2023 •

Copy link
Copy Markdown
Contributor

Fixes #109746.

If Python fails to start newly created thread due to failure of underlying PyThread_start_new_thread() call, its state should be removed from interpreter' thread states list to avoid its double cleanup.

  • make PyThreadState_Delete accept unbound thread state
  • in thread_PyThread_start_new_thread, delete state of new thread from interpreter if PyThread_start_new_thread call fails
If Python fails to start newly created thread
due to failure of underlying PyThread_start_new_thread() call,
its state should be removed from interpreter' thread states list
to avoid its double cleanup

* make 'PyThreadState_Delete' accept unbound thread state
* in 'thread_PyThread_start_new_thread', delete state of new thread from interpreter if 'PyThread_start_new_thread' call fails
@chgnrdv chgnrdv changed the title Make _thread.start_new_thread delete state of new thread on its startup failure Sep 22, 2023
Comment thread Lib/test/test_threading.py Resolved
@serhiy-storchaka

Copy link
Copy Markdown
Member

The test did not fail on non-patched code, failed on Windows, and hanged on macOS. I rewrote it. According to a comment in a similar test, setrlimit() does not work on macOS. Will see if it works for this test.

@serhiy-storchaka

Copy link
Copy Markdown
Member

!buildbot bsd

@bedevere-bot

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @serhiy-storchaka for commit 1f3ecd9 🤖

The command will test the builders whose names match following regular expression: bsd

The builders matched are:

  • AMD64 FreeBSD14 PR
  • AMD64 FreeBSD15 PR
  • AMD64 FreeBSD PR
@devdanzin

Copy link
Copy Markdown
Member

Applying a version of the fix in this PR solves an issue I had where all processes with large thread counts would abort at finalization (particularly annoying when you're trying to fuzz CPython).

@serhiy-storchaka

Copy link
Copy Markdown
Member

!buildbot bsd

@bedevere-bot

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @serhiy-storchaka for commit 185d5a6 🤖

The command will test the builders whose names match following regular expression: bsd

The builders matched are:

  • AMD64 FreeBSD Refleaks PR
  • AMD64 FreeBSD14 PR
  • AMD64 FreeBSD15 PR
  • AMD64 FreeBSD PR
@serhiy-storchaka

Copy link
Copy Markdown
Member

!buildbot bsd

@bedevere-bot

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @serhiy-storchaka for commit 55a8237 🤖

The command will test the builders whose names match following regular expression: bsd

The builders matched are:

  • AMD64 FreeBSD Refleaks PR
  • AMD64 FreeBSD14 PR
  • AMD64 FreeBSD15 PR
  • AMD64 FreeBSD PR
@serhiy-storchaka
serhiy-storchaka merged commit ca3ea9a into python:main Nov 22, 2024
@serhiy-storchaka serhiy-storchaka added the needs backport to 3.13 only security fixes label Nov 22, 2024
@miss-islington-app

Copy link
Copy Markdown

Thanks @chgnrdv for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

@miss-islington-app

Copy link
Copy Markdown

Sorry, @chgnrdv and @serhiy-storchaka, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker ca3ea9ad05c3d876a58463595e5b4228fda06936 3.13
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request Nov 22, 2024
… new thread on its startup failure (pythonGH-109761)

If Python fails to start newly created thread
due to failure of underlying PyThread_start_new_thread() call,
its state should be removed from interpreter' thread states list
to avoid its double cleanup.

(cherry picked from commit ca3ea9a)

Co-authored-by: Radislav Chugunov <52372310+chgnrdv@users.noreply.github.com>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
@bedevere-app

bedevere-app Bot commented Nov 22, 2024

Copy link
Copy Markdown

GH-127171 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 only security fixes label Nov 22, 2024
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request Nov 22, 2024
… new thread on its startup failure (pythonGH-109761)

If Python fails to start newly created thread
due to failure of underlying PyThread_start_new_thread() call,
its state should be removed from interpreter' thread states list
to avoid its double cleanup.

(cherry picked from commit ca3ea9a)

Co-authored-by: Radislav Chugunov <52372310+chgnrdv@users.noreply.github.com>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
@bedevere-app

bedevere-app Bot commented Nov 22, 2024

Copy link
Copy Markdown

GH-127173 is a backport of this pull request to the 3.12 branch.

serhiy-storchaka added a commit that referenced this pull request Nov 22, 2024
…hread on its startup failure (GH-109761) (GH-127171)

If Python fails to start newly created thread
due to failure of underlying PyThread_start_new_thread() call,
its state should be removed from interpreter' thread states list
to avoid its double cleanup.

(cherry picked from commit ca3ea9a)

Co-authored-by: Radislav Chugunov <52372310+chgnrdv@users.noreply.github.com>
serhiy-storchaka added a commit that referenced this pull request Nov 22, 2024
…hread on its startup failure (GH-109761) (GH-127173)

If Python fails to start newly created thread
due to failure of underlying PyThread_start_new_thread() call,
its state should be removed from interpreter' thread states list
to avoid its double cleanup.

(cherry picked from commit ca3ea9a)

Co-authored-by: Radislav Chugunov <52372310+chgnrdv@users.noreply.github.com>
@vstinner

Copy link
Copy Markdown
Member

!buildbot bsd

The test is unstable on macOS and FreeBSD: it fails randomly. Examples:

Example of failure on FreeBSD:

FAIL: test_start_new_thread_failed (test.test_threading.ThreadTests.test_start_new_thread_failed)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/buildbot/buildarea/3.x.ware-freebsd/build/Lib/test/test_threading.py", line 1205, in test_start_new_thread_failed
    self.assertEqual(out, b'ok')
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^
AssertionError: b"skipshouldn't be printed" != b'ok'
@vstinner

Copy link
Copy Markdown
Member

On Linux, RLIMIT_NPROC is the maximum number of threads. On FreeBSD, RLIMIT_NPROC is the maximum number of processes.

@vstinner

Copy link
Copy Markdown
Member

I suggest to make the test specific to Linux, or use another approach than RLIMIT_NPROC.

@serhiy-storchaka

Copy link
Copy Markdown
Member

Ah, there is a race condition here. Should be fixed by #127299.

ebonnal pushed a commit to ebonnal/cpython that referenced this pull request Jan 12, 2025
…read on its startup failure (pythonGH-109761)

If Python fails to start newly created thread
due to failure of underlying PyThread_start_new_thread() call,
its state should be removed from interpreter' thread states list
to avoid its double cleanup.

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
@serhiy-storchaka serhiy-storchaka removed their assignment Jul 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

5 participants