Skip to content

gh-87135: threading.Lock: Raise rather than hang on Python finalization - #135991

Merged
encukou merged 6 commits into
python:mainfrom
encukou:no-acquire-in-finalize
Jul 1, 2025
Merged

encukou merged 6 commits into
python:mainfrom
encukou:no-acquire-in-finalize

Conversation

@encukou

@encukou encukou commented Jun 26, 2025 •

Copy link
Copy Markdown
Member

After Python finalization gets to the point where no other thread can attach thread state, attempting to acquire a Python lock will hang.
Raise PythonFinalizationError instead of hanging.

@colesbury, does this look maintainable (and correct) to you?

…lization

After Python finalization gets to the point where no other thread
can attach thread state, attempting to acquire a Python lock must
hang.
Raise PythonFinalizationError instead of hanging.

@colesbury colesbury left a comment

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.

I think this approach makes sense. A comment about the flags below

Comment thread Python/lock.c Resolved
@vstinner

Copy link
Copy Markdown
Member

Tests / Windows (free-threading) / Build and test (arm64) (pull_request)
Tests / Windows (free-threading) / Build and test (x64) (pull_request)

There is a suspicious error when running test.test_multiprocessing_spawn.test_processes:

Assertion failed: !PyErr_Occurred(), file D:\a\cpython\cpython\Python\specialize.c, line 740
Fatal Python error: Aborted

<Cannot show all threads while the GIL is disabled>
Stack (most recent call first):
  File "D:\a\cpython\cpython\Lib\logging\__init__.py", line 891 in _removeHandlerRef

Current thread's C stack trace (most recent call first):
  <cannot get C stack on this system>

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@encukou
encukou merged commit fe119a0 into python:main Jul 1, 2025
@github-project-automation github-project-automation Bot moved this from Todo to Done in MEDX-arabic-programme Jul 1, 2025
@encukou
encukou deleted the no-acquire-in-finalize branch July 1, 2025 08:57
AndPuQing pushed a commit to AndPuQing/cpython that referenced this pull request Jul 11, 2025
…lization (pythonGH-135991)

After Python finalization gets to the point where no other thread
can attach thread state, attempting to acquire a Python lock must hang.
Raise PythonFinalizationError instead of hanging.
Pranjal095 pushed a commit to Pranjal095/cpython that referenced this pull request Jul 12, 2025
…lization (pythonGH-135991)

After Python finalization gets to the point where no other thread
can attach thread state, attempting to acquire a Python lock must hang.
Raise PythonFinalizationError instead of hanging.
picnixz pushed a commit to picnixz/cpython that referenced this pull request Jul 13, 2025
…lization (pythonGH-135991)

After Python finalization gets to the point where no other thread
can attach thread state, attempting to acquire a Python lock must hang.
Raise PythonFinalizationError instead of hanging.
taegyunkim pushed a commit to taegyunkim/cpython that referenced this pull request Aug 4, 2025
…lization (pythonGH-135991)

After Python finalization gets to the point where no other thread
can attach thread state, attempting to acquire a Python lock must hang.
Raise PythonFinalizationError instead of hanging.
Agent-Hellboy pushed a commit to Agent-Hellboy/cpython that referenced this pull request Aug 19, 2025
…lization (pythonGH-135991)

After Python finalization gets to the point where no other thread
can attach thread state, attempting to acquire a Python lock must hang.
Raise PythonFinalizationError instead of hanging.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

4 participants