Skip to content

Faulthandler output should include the thread name #125434

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
spacemanspiff2007 opened this issue Oct 14, 2024 · 7 comments
Open

Faulthandler output should include the thread name #125434

spacemanspiff2007 opened this issue Oct 14, 2024 · 7 comments
Labels
extension-modules C modules in the Modules dir type-feature A feature request or enhancement

Comments

@spacemanspiff2007
Copy link
Contributor

spacemanspiff2007 commented Oct 14, 2024

Feature or enhancement

Proposal:

Currently faulthandler just dumps the thread id.
It would be nice if the thread name would also be part of the output since this makes debugging much easier

Current output

Thread 0x00003b9c (most recent call first):
  File "C:\ProgramFiles\Python\Python312\Lib\concurrent\futures\thread.py", line 89 in _worker
  File "C:\ProgramFiles\Python\Python312\Lib\threading.py", line 1012 in run
  File "C:\ProgramFiles\Python\Python312\Lib\threading.py", line 1075 in _bootstrap_inner
  File "C:\ProgramFiles\Python\Python312\Lib\threading.py", line 1032 in _bootstrap
...

Better output with thread name in square brackets

Thread 0x00003b9c [Thread pool worker 1] (most recent call first):
  File "C:\ProgramFiles\Python\Python312\Lib\concurrent\futures\thread.py", line 89 in _worker
  File "C:\ProgramFiles\Python\Python312\Lib\threading.py", line 1012 in run
  File "C:\ProgramFiles\Python\Python312\Lib\threading.py", line 1075 in _bootstrap_inner
  File "C:\ProgramFiles\Python\Python312\Lib\threading.py", line 1032 in _bootstrap
...

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

@spacemanspiff2007 spacemanspiff2007 added the type-feature A feature request or enhancement label Oct 14, 2024
@picnixz picnixz added the extension-modules C modules in the Modules dir label Oct 14, 2024
@rruuaanng
Copy link
Contributor

Sorry, It seems that there is no thread name member in PyThreadState :(. In my opinion, The thread name does not make much sense, although it is more convenient for debugging.
(Maybe I didn't find it).

@spacemanspiff2007
Copy link
Contributor Author

If it's not there it obviously can't be included. Thank you very much for looking!
I'm debugging an asyncio applications which also uses a thread pool.
Since I'm only interested in the threads from the thread pool the thread name would have been a very easy and convenient way to identify these threads.

@vstinner
Copy link
Member

I don't think that we can implement this feature.

Sadly, faulthandler is very low-level. It doesn't have access to threading.Threading.name attribute. It cannot import a module or do complex operation.

faulthandler is called in a signal handler, only "async signal-safe" function can be used, memory can only be allocated on the stack (not on the heap).

I suggest to close this issue as "won't fix".

@picnixz
Copy link
Member

picnixz commented Nov 27, 2024

I think I'll follow Victor's call. I had a quick look at the code to see how we could make it include the thread name but I don't think it's feasible.

One possibility is to carry the latter everywhere but since only stack-allocated memory can be used I really don't think we can have a simple fix.

@picnixz picnixz added the pending The issue will be closed if no feedback is provided label Nov 27, 2024
@vstinner
Copy link
Member

One approach would be to set the thread name in the OS: #59705 and then retrieve the thread name from the OS. It might work if retrieving the name is safe in a signal handler.

@vstinner
Copy link
Member

I wrote a change to set the thread name to the operating system: #127338

@erlend-aasland erlend-aasland removed the pending The issue will be closed if no feedback is provided label Nov 29, 2024
vstinner added a commit to vstinner/cpython that referenced this issue Apr 2, 2025
@vstinner
Copy link
Member

vstinner commented Apr 2, 2025

I wrote PR gh-132016 to display the thread native name in faulthandler.

serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Apr 11, 2025
pthread_get_name_np is defined in pthread_np.h.
serhiy-storchaka added a commit that referenced this issue Apr 12, 2025
pthread_get_name_np is defined in pthread_np.h.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extension-modules C modules in the Modules dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

5 participants