classification
Title: Allow to retrieve ongoing exception handled by every threads
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: jd, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2020-07-29 15:39 by jd, last changed 2020-11-02 20:54 by serhiy.storchaka. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 21689 merged jd, 2020-07-30 11:36
Messages (4)
msg374575 - (view) Author: Julien Danjou (jd) * Date: 2020-07-29 15:39
In order to do statistical profiling on raised exception, having the ability to poll all the running threads for their currently handled exception would be fantastic.

There is an exposed function named `sys._current_frames()` that allows to list the current frame handled by CPython. Having an equivalent for `sys._current_exceptions()` that would return the content of `sys.exc_info()` for each running thread would solve the issue.
msg374727 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-08-03 08:10
Would not be more useful to add a method to the Thread or frame object to obtain the topmost handled exception? Then you could iterate all threads or frames and obtain exceptions together with other useful information.

Note also that it omits exceptions in generators and coroutines.
msg374728 - (view) Author: Julien Danjou (jd) * Date: 2020-08-03 08:38
Adding to the thread object might be a good idea, but it does not work if you ever start threads with the low-level `_thread` API.
It's also a different design from the existing `sys._current_frames`.

Adding it on top of a frame is interesting though. I think internally it's more linked to the execution stack in a thread than to a frame, though.
msg380223 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-11-02 14:16
New changeset 64366fa9b3ba71b8a503a8719eff433f4ea49eb9 by Julien Danjou in branch 'master':
bpo-41435: Add sys._current_exceptions() function (GH-21689)
https://github.com/python/cpython/commit/64366fa9b3ba71b8a503a8719eff433f4ea49eb9
History
Date User Action Args
2020-11-02 20:54:45serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-11-02 14:16:31serhiy.storchakasetmessages: + msg380223
2020-08-03 08:38:38jdsetmessages: + msg374728
2020-08-03 08:10:12serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg374727
2020-07-30 11:36:20jdsetkeywords: + patch
stage: patch review
pull_requests: + pull_request20832
2020-07-29 15:39:46jdcreate