Skip to content

gh-140746: Fix Thread.start() that can hang indefinitely (alternative to GH-140799) - #144750

Closed
YvesDup wants to merge 20 commits into
python:mainfrom
YvesDup:thread-hang-memory
Closed

YvesDup wants to merge 20 commits into
python:mainfrom
YvesDup:thread-hang-memory

Conversation

@YvesDup

@YvesDup YvesDup commented Feb 12, 2026 •

Copy link
Copy Markdown
Contributor

This PR is based on the @ryv-odoo PR. I would have preferred to start from @ryv-odoo's PR branch rather than duplicating his code, but I don't know if that's possible.

Following the @colesbury comment,

I would lean towards changing Thread.start and _start_joinable_thread so that we can do the waiting in C:

       try:
           # Start joinable thread
           _start_joinable_thread(self._bootstrap, handle=self._os_thread_handle,
                                  daemon=self.daemon)
       except Exception:
           with _active_limbo_lock:
               del _limbo[self]
           raise

I think we could add a new states to _os_thread_handle to represent the state when self._started.set() was called and to represent the state when the thread fails before then.

I submit this PR that includes the waiting in C the module and raise a runtime exception when the thread handle state is set to failed. This exception is treated in the Thread.start.

@brijkapadia brijkapadia 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.

Left a couple of small comments on semantics.

Comment thread Modules/_threadmodule.c Resolved
Comment thread Modules/_threadmodule.c Resolved
Comment thread Modules/_threadmodule.c Resolved
Comment thread Modules/_threadmodule.c Resolved
Comment thread Modules/_threadmodule.c Resolved

@ryv-odoo ryv-odoo left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hello @YvesDup ,

Now I understand what you meant in your comment 😄. Sorry I certainly misunderstood your comment at the time.

This version changes the behavior slightly from mine: The serving thread (aka the one who call start() on the new Thread) will crash (raise an exception) because of an error raised from the new Thread.
I am not a huge fan of this change because it means that the serving thread might not recover from it (and I feel that errors from sub thread shouldn't impact the parent Thread). But I don't have a strong opinion about it 🤔

I will apply the comments of @bkap123 in my branch since they mainly target my code.

Have a nice day.

Comment thread Lib/test/test_threading.py Resolved
@YvesDup

YvesDup commented Feb 25, 2026

Copy link
Copy Markdown
Contributor Author

I am not a huge fan of this change because it means that the serving thread might not recover from it (and I feel that errors from sub thread shouldn't impact the parent Thread). But I don't have a strong opinion about it 🤔

Hi @ryv-odoo ,
I agree with your remark about the threads, that changes the initial behavior. I hadn't considered that point.

In this PR, it is possible to wait in C and just cleans up the two dicts in the start method in an exception block or outside, regarding the value of self._os_thread_handle.is_bootstraped().

I proposed raising an exception because I would notify the devs that the new thread starts fine but fails before calling the _bootstrap(*) methods. Peharps, it is not a good idea and not appropriate due to the change of initial behavior.

Comment thread Lib/test/test_threading.py Resolved
@picnixz picnixz changed the title gh-140746: Fix Thread.start() that can hang indefinitely - v2 Mar 15, 2026
@ambv

ambv commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

This PR is superseded by #153776, a newer open pull request for the same issue (gh-140746), and it no longer merges cleanly. Closing to cull the number of open pull requests. Do not reopen this pull request without getting a go-ahead from a triager or core developer.

@ambv ambv closed this Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

5 participants