Skip to content

add Python 3.14 compatible logic to check if a thread is alive #305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion _pydev_bundle/pydev_is_thread_alive.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
# It is required to debug threads started by start_new_thread in Python 3.4
_temp = threading.Thread()

if hasattr(_temp, "_handle") and hasattr(_temp, "_started"): # Python 3.13 and later has this
if hasattr(_temp, "_os_thread_handle") and hasattr(_temp, "_started"): # Python 3.14 has no `_handle`

def is_thread_alive(t):
return not t._os_thread_handle.is_done()

elif hasattr(_temp, "_handle") and hasattr(_temp, "_started"): # Python 3.13 and later has this

def is_thread_alive(t):
return not t._handle.is_done()
Expand Down
21 changes: 15 additions & 6 deletions _pydevd_sys_monitoring/_pydevd_sys_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ def _get_unhandled_exception_frame(exc, depth: int) -> Optional[FrameType]:
# cdef PyDBAdditionalThreadInfo additional_info
# thread: threading.Thread
# trace: bool
# _use_is_stopped: bool
# ELSE
class ThreadInfo:
additional_info: PyDBAdditionalThreadInfo
Expand All @@ -276,20 +275,30 @@ def __init__(self, thread: threading.Thread, thread_ident: int, trace: bool, add
self.thread_ident = thread_ident
self.additional_info = additional_info
self.trace = trace
self._use_is_stopped = hasattr(thread, '_is_stopped')


self._use_handle = hasattr(thread, "_handle")
self._use_started = hasattr(thread, "_started")
self._use_os_thread_handle = hasattr(thread, "_os_thread_handle")

# fmt: off
# IFDEF CYTHON
# cdef bint is_thread_alive(self):
# ELSE
def is_thread_alive(self):
# ENDIF
# fmt: on
if self._use_is_stopped:
return not self.thread._is_stopped
else:
# Python 3.14
if self._use_os_thread_handle and self._use_started:
return not self.thread._os_thread_handle.is_done()

# Python 3.13
elif self._use_handle and self._use_started:
return not self.thread._handle.is_done()

# Python 3.12
else:
return not self.thread._is_stopped


class _DeleteDummyThreadOnDel:
"""
Expand Down
21 changes: 15 additions & 6 deletions _pydevd_sys_monitoring/_pydevd_sys_monitoring_cython.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ cdef class ThreadInfo:
cdef PyDBAdditionalThreadInfo additional_info
thread: threading.Thread
trace: bool
_use_is_stopped: bool
# ELSE
# class ThreadInfo:
# additional_info: PyDBAdditionalThreadInfo
Expand All @@ -268,20 +267,30 @@ cdef class ThreadInfo:
self.thread_ident = thread_ident
self.additional_info = additional_info
self.trace = trace
self._use_is_stopped = hasattr(thread, '_is_stopped')


self._use_handle = hasattr(thread, "_handle")
self._use_started = hasattr(thread, "_started")
self._use_os_thread_handle = hasattr(thread, "_os_thread_handle")

# fmt: off
# IFDEF CYTHON -- DONT EDIT THIS FILE (it is automatically generated)
cdef bint is_thread_alive(self):
# ELSE
# def is_thread_alive(self):
# ENDIF
# fmt: on
if self._use_is_stopped:
return not self.thread._is_stopped
else:
# Python 3.14
if self._use_os_thread_handle and self._use_started:
return not self.thread._os_thread_handle.is_done()

# Python 3.13
elif self._use_handle and self._use_started:
return not self.thread._handle.is_done()

# Python 3.12
else:
return not self.thread._is_stopped


class _DeleteDummyThreadOnDel:
"""
Expand Down
Loading