Repository navigation
Add sys.unraisablehook() to customize how "unraisable exceptions" are logged #81010
Copy link
Copy link
Closed
Labels
3.8 (EOL)end of lifeend of lifeinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancementA feature request or enhancement
Description
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee = None
closed_at = <Date 2019-05-27.22:57:04.603>
created_at = <Date 2019-05-07.12:10:07.913>
labels = ['interpreter-core', 'type-feature', '3.8']
title = 'Add sys.unraisablehook() to customize how "unraisable exceptions" are logged'
updated_at = <Date 2019-07-09.10:38:23.720>
user = 'https://github.com/graingert'bugs.python.org fields:
activity = <Date 2019-07-09.10:38:23.720>
actor = 'vstinner'
assignee = 'none'
closed = True
closed_date = <Date 2019-05-27.22:57:04.603>
closer = 'vstinner'
components = ['Interpreter Core']
creation = <Date 2019-05-07.12:10:07.913>
creator = 'graingert'
dependencies = []
files = ['48313', '48314', '48315', '48316', '48321', '48329']
hgrepos = []
issue_num = 36829
keywords = ['patch']
message_count = 32.0
messages = ['341708', '341718', '341736', '341752', '341787', '341868', '342000', '342001', '342003', '342011', '342013', '342014', '342413', '342482', '342618', '343161', '343170', '343201', '343213', '343245', '343247', '343250', '343253', '343255', '343286', '343412', '343436', '343609', '343697', '343778', '344318', '347544']
nosy_count = 6.0
nosy_names = ['vstinner', 'serhiy.storchaka', 'graingert', 'matrixise', 'ZackerySpytz', 'xtreak']
pr_nums = ['13175', '13187', '13487', '13488', '13490', '13507', '13554', '13620', '13752']
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue36829'
versions = ['Python 3.8']Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
3.8 (EOL)end of lifeend of lifeinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancementA feature request or enhancement
Activity
graingert commentedon May 7, 2019
Currently it's quite easy for these errors to go unnoticed.
I'd like a way to easily detect these in CI.
nedbat suggested piping the process output to another tool, and looking for 'Exception ignored in:' but this seems a little diff
matrixise commentedon May 7, 2019
Hi Serhiy,
What do you think about this idea?
Normally, we could use -W error when there is an exception.
serhiy-storchaka commentedon May 7, 2019
It looks like a good idea to me. But it should not be -W error. It should be an -X option, and this option should be used exclusively for debugging user extension modules.
graingert commentedon May 7, 2019
I have a very large codebase that fires the odd ResourceWarning, and after fixing them all I'd like to ensure that they do not reoccur. When using
-W errorit still won't fail CI. So I would use this -X option for more than debugging user extension modulesZackerySpytz commentedon May 7, 2019
I am working on this issue.
[-]CLI option to make PyErr_WriteUnraisable abortthe current process[/-][+]CLI option to make PyErr_WriteUnraisable abort the current process[/+]vstinner commentedon May 8, 2019
I wrote PR 13187 to control how unraisable exceptions are handled.
Attached uncollectable.py, gc_callback.py and io_destructor.py examples can be
used to test unraisable exceptions.
Without my PR:
---
$ ./python -Werror uncollectable.py ResourceWarning: gc: 2 uncollectable objects at shutdown; use gc.set_debug(gc.DEBUG_UNCOLLECTABLE) to list themFor example, apply attached site_hook.patch to install a custom unraisablehook.
Output with my PR + site_hook.patch:
---
$ ./python -Werror uncollectable.py ResourceWarning: gc: 2 uncollectable objects at shutdown; use gc.set_debug(gc.DEBUG_UNCOLLECTABLE) to list themThe first good news is that it *is* possible to write a custom hook for unraisable for one of the last unraisable exception: _PyGC_DumpShutdownStats() which logs "uncollectable objects at shutdown".
When an unraisable exceptions is logged before Python finalization, the hook can inspect the Python stack to see where the exception has been raised which helps debugging.
vstinner commentedon May 9, 2019
too_late_unraisable.py is an example where PyErr_WriteUnraisable() is called very lated during Python finalization to be handled by user code. A destructor (del) fails on print() because the stream has been closed.
PyErr_WriteUnraisable() is called by _PyGC_CollectNoFail() at the *end* of PyImport_Cleanup(). At this point, the sys module has already been cleared, as all other modules. A custom sys.unraisablehook cannot be used, because sys has been called.
vstinner commentedon May 9, 2019
Ok, let me come back to the initial issue:
Thomas Grainger:
When PyErr_WriteUnraisable() is called before Python finalization, my PR 13187 allows to handle these exceptions: log them in a dedicated file, abort the process, maybe even open a network connection, etc. The hook allows to implement your chosen behavior.
The problem is more during Python finalization: see attached too_late_unraisable.py example and my previous comment. If PyErr_WriteUnraisable() is called after sys.stderr is closed or closed to None, the function does nothing: the exception is not logged.
The question now becomes: do *all* calls to PyErr_WriteUnraisable() must abort the process? What is the point? Only a very low level debugger like gdb can be used to see the exception.
@thomas Grainger: Do you want to have to use gdb to trace such very level exception?
IMHO sadly when PyErr_WriteUnraisable() is called way too late, we should simply ignore such exceptions. And so my PR 13187 is good enough to cover most cases.
If someone cares about exceptions raised very late during Python finalization, Python finalization should be enhanced. But this code is very fragile and is not deterministic. It is a work in progress for years to enhance it.
graingert commentedon May 9, 2019
The point for me is that CI will fail if it happens, then I can use gdb to
find out the cause
On Thu, 9 May 2019, 23:17 STINNER Victor, <report@bugs.python.org> wrote:
vstinner commentedon May 9, 2019
I'm not comfortable with forcing users to use a low-level debugger to debug "unraisable exceptions".
I tried PR 13175 on the test suite by forcing the option to 1: always call Py_FatalError(). Many tests break:
14 tests failed:
test_asyncio test_cmd_line test_coroutines test_cprofile
test_exceptions test_generators test_import test_io
test_raise test_repl test_signal test_ssl test_urllib
test_yield_from
Examples:
test_error_through_destructor (test.test_io.CBufferedReaderTest) ... Fatal Python error: Unraisable exception
FAIL: test_warn_on_full_buffer (test.test_signal.WakeupSocketSignalTests)
FAIL: test_send_error (test.test_signal.WakeupSocketSignalTests)
FAIL: test_wakeup_write_error (test.test_signal.WakeupSignalTests)
test_unraisable (test.test_exceptions.ExceptionTests) ... Fatal Python error: Unraisable exception
test_generators:
Trying:
del g
Expecting nothing
Fatal Python error: Unraisable exception
etc.
Unraisable exceptions are bad, but it's really hard to fix all of them. They are too many cases where Python is unable to pass exceptions to the parent.
If you want to make the situation better, maybe we should investigate where Python cannot raise exceptions and try to make it possible.
IMHO my PR 13187 adding sys.unraisablehook() is more usable/reasonable option.
--
Thomas Grainger: which code are you running on your CI? Did you try PR 13175 on your CI? Try it with this additional change, to always crash on PyErr_WriteUnraisable(). Does your CI still pass?
graingert commentedon May 9, 2019
Defaulting to noop when the hook fails means I'll never notice the failure
to be able to debug it
On Fri, 10 May 2019, 00:42 STINNER Victor, <report@bugs.python.org> wrote:
10 remaining items
vstinner commentedon May 22, 2019
In PR 13490, Thomas Grainger proposed a cool context manager:
It allows to raise an unraisable exception :-D Example:
try:
with support.throw_unraisable_exceptions():
...
except Exception as e:
... # the exception is now here
I don't need such context manager right now, but I like the fact that it becomes possible to write such context manager :-)
vstinner commentedon May 22, 2019
I wrote PR 13512 to use support.catch_unraisable_exception() in test_io.test_error_through_destructor(). But this PR is associated to bpo-18748 since the main change is related to the io module, not sys.unraisablehook ;-)
vstinner commentedon May 22, 2019
See also bpo-1230540: "sys.excepthook doesn't work in threads".
vstinner commentedon May 22, 2019
New changeset df22c03 by Victor Stinner in branch 'master':
bpo-36829: PyErr_WriteUnraisable() normalizes exception (GH-13507)
df22c03
tirkarthi commentedon May 23, 2019
Could test.support.catch_unraisable_exception also be documented at https://docs.python.org/3/library/test.html#module-test.support ?
vstinner commentedon May 24, 2019
I wrote PR 13554 to document it.
vstinner commentedon May 24, 2019
New changeset 6dbbe74 by Victor Stinner in branch 'master':
bpo-36829: Document test.support.catch_unraisable_exception() (GH-13554)
6dbbe74
vstinner commentedon May 27, 2019
New changeset 71c52e3 by Victor Stinner in branch 'master':
bpo-36829: Add _PyErr_WriteUnraisableMsg() (GH-13488)
71c52e3
vstinner commentedon May 27, 2019
Ok, the initial issue has been fixed by adding a new sys.unraisablehook() function. You can kill the process with SIGABRT using the recipe I proposed there:
https://bugs.python.org/issue36829#msg343201
As a follow-up, I created bpo-37069: "regrtest: log unraisable exceptions and uncaught thread exceptions".
Thanks Thomas Grainger for reviews and for reporting this interesting issue ;-)
vstinner commentedon May 28, 2019
New changeset a85a1d3 by Victor Stinner in branch 'master':
bpo-36829: sys.excepthook and sys.unraisablehook flush (GH-13620)
a85a1d3
[-]Add sys.unraisablehook() to custom how "unraisable exceptions" are logged[/-][+]Add sys.unraisablehook() to customize how "unraisable exceptions" are logged[/+]vstinner commentedon Jun 2, 2019
New changeset cdce057 by Victor Stinner in branch 'master':
bpo-36829: test_threading: Fix a ref cycle (GH-13752)
cdce057
vstinner commentedon Jul 9, 2019
See also bpo-37526: Add support.catch_threading_exception().