Repository navigation
Closed
Labels
3.7 (EOL)end of lifeinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-crashA hard crash of the interpreter, possibly with a core dump
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 2017-10-26.15:54:05.986>
created_at = <Date 2014-11-18.21:26:16.533>
labels = ['interpreter-core', '3.7', 'type-crash']
title = 'segfault during shutdown attempting to log ResourceWarning'
updated_at = <Date 2017-10-26.15:54:05.984>
user = 'https://github.com/ajdavis'bugs.python.org fields:
activity = <Date 2017-10-26.15:54:05.984>
actor = 'xdegaye'
assignee = 'none'
closed = True
closed_date = <Date 2017-10-26.15:54:05.986>
closer = 'xdegaye'
components = ['Interpreter Core']
creation = <Date 2014-11-18.21:26:16.533>
creator = 'emptysquare'
dependencies = []
files = ['37224', '37229', '37230', '37278', '37279', '37286', '37288', '37289', '37316', '37332', '37333', '37334', '46940']
hgrepos = []
issue_num = 22898
keywords = ['patch']
message_count = 26.0
messages = ['231345', '231347', '231360', '231389', '231390', '231683', '231686', '231705', '231706', '231710', '231717', '231721', '231729', '231860', '231933', '262356', '262372', '262473', '262494', '294911', '295331', '295628', '295707', '295719', '295940', '305067']
nosy_count = 6.0
nosy_names = ['brett.cannon', 'pitrou', 'vstinner', 'xdegaye', 'serhiy.storchaka', 'emptysquare']
pr_nums = ['1981']
priority = 'normal'
resolution = 'out of date'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'crash'
url = 'https://bugs.python.org/issue22898'
versions = ['Python 3.6', 'Python 3.7']Metadata
Metadata
Assignees
Labels
3.7 (EOL)end of lifeinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-crashA hard crash of the interpreter, possibly with a core dump
Activity
ajdavis commentedon Nov 18, 2014
Running a unittest suite for Motor, my MongoDB driver which uses PyMongo, greenlet, and Tornado. The suite commonly segfaults during interpreter shutdown. I've reproduced this crash with Python 3.3.5, 3.4.1, and 3.4.2. Python 2.6 and 2.7 do *not* crash. The Python interpreters are all built like:
./configure --prefix=/mnt/jenkins/languages/python/rX.Y.Z --enable-shared && make && make install
This is Amazon Linux AMI release 2014.09.
The unittest suite's final output is:
----------------------------------------------------------------------
Ran 15 tests in 265.947s
OK
Segmentation fault (core dumped)
Backtrace from a Python 3.4.2 coredump attached.
ajdavis commentedon Nov 18, 2014
The crash can ignore whether or not I specify "-Wignore" on the python command line. I was hoping to avoid the crash by short-circuiting the ResourceWarning code path, since the following line appears in the backtrace:
#5 PyErr_WarnFormat (category=<optimized out>, stack_level=stack_level@entry=1, format=format@entry=0x7f5f1ca8b377 "unclosed file %R") at Python/_warnings.c:813
But "-Wignore" has no effect.
serhiy-storchaka commentedon Nov 19, 2014
Looks as globals in setup_context() is NULL. I suppose it is PyThreadState_Get()->interp->sysdict. interp->sysdict is cleared in PyInterpreterState_Clear(). Other code is executed after setting interp->sysdict to NULL (clearing interp->sysdict content, interp->builtins, interp->builtins_copy and interp->importlib) and this potentially can emit warnings.
vstinner commentedon Nov 19, 2014
It looks like the problem is that raising the PyExc_RecursionErrorInst singleton creates a traceback object which contains frames. The singleton keeps the frames alive longer than expected.
I tried to write a script to raise this singleton, but it looks like the local variables of the frames are not deleted, even if frames are deleted (by _PyExc_Fini).
You may try to finish my attached runtimerror_singleton.py script.
vstinner commentedon Nov 19, 2014
Oh, I also wrote a draft of patch fixing the issue, but I was unable to reproduce the issue. See attached warn.patch (not tested).
xdegaye commentedon Nov 25, 2014
The attached script raises the PyExc_RecursionErrorInst singleton and reproduces the issue.
The attached patch fixes the issue by ignoring the warning when clearing PyExc_RecursionErrorInst and clearing the frames associated with its traceback, in _PyExc_Fini().
vstinner commentedon Nov 25, 2014
+ /* during Python finalization, warnings may be emited after interp->sysdict
+ is cleared: see issue bpo-22898 */
I would prefer to see this comment in the else block.
xdegaye commentedon Nov 26, 2014
Indeed.
New updated patch attached.
serhiy-storchaka commentedon Nov 26, 2014
Why recursion limit is restored? Couldn't the test be simpler without it?
%a should be used instead of '%s' (file path can contain backslashes).
And it would be more robust to open file in binary mode (may be even in non-buffered). It can contain non-ascii characters.
May be the test should be marked as CPython only.
To check that script is executed at all we should print something from it and than test the out. Otherwise syntax error in script will make all test passed.
xdegaye commentedon Nov 26, 2014
For the sake of explicitness, so that the interpreter will not raise a RuntimeError during finalization when checking for the recursion limit after g.throw(MyException) has raised PyExc_RecursionErrorInst.
Thanks.
New patch attached.
The reason why the PyExc_RecursionErrorInst singleton keeps the frames alive longer than expected is that the reference count of the PyExc_RecursionErrorInst static variable never reaches zero until _PyExc_Fini(). So decrementing the reference count of this exception after the traceback has been printed in PyErr_PrintEx() does not decrement the reference count of its traceback attribute (as it is the case with the other exceptions) and the traceback is not freed. The following patch to PyErr_PrintEx() does that. With this new patch and without the changes made by warn_4.patch, the interpreter does not crash with the runtimerror_singleton_2.py reproducer and the ResourceWarning is now printed instead of being ignored as with the warn_4.patch:
If both patches were to be included, the test case in warn_4.patch would test the above patch and not the changes made in Python/_warnings.c.
serhiy-storchaka commentedon Nov 26, 2014
out can be b'Done.\r\n'. Use self.assertIn.
You can test err for warning message.
The traceback should be cleared before decrementing the reference count. And only if Py_REFCNT(v) is 2.
ajdavis commentedon Nov 26, 2014
With warn_4.patch applied I can no longer reproduce my original segfault, looks like the fix works.
xdegaye commentedon Nov 26, 2014
Ok, new patch attached.
In the case where PyExc_RecursionErrorInst would not leak frames, the code path followed by the test case would not run any of the changes made in _warnings.c.
I believe that attempting to fix the frames leak by clearing the traceback implies the following changes:
* The previous patch to PyErr_PrintEx()
* v->context and v->cause should also be tested against equality with PyExc_RecursionErrorInst.
* In PyErr_PrintEx() the variable v2 may also be PyExc_RecursionErrorInst.
* The sames change should also be done when freeing the exception value at least:
in PyErr_WriteUnraisable() called when an exception occurs during finalization
in PyErr_Restore()
[1] when sys.last_value is cleared in PyImport_Cleanup()
And that would miss the case [1] where sys.last_value is set to None by some python user code.
Note [1]:
Unless it is acceptable to clear the PyExc_RecursionErrorInst traceback even when sys.last_value has been set (then Py_REFCNT(v) is 3 instead of 2).
Not sure if this is worth the trouble.
1 remaining item
xdegaye commentedon Dec 1, 2014
When tstate->overflowed is already set to 1 before entering PyErr_NormalizeException() to normalize an exception, the following cases may occur:
=> PyObject_Call() never returns and the interpreter aborts with a fatal error when the high warter mark is exceeded, the infinite recursion is in PyObject_Call().
a) Without any patch, we get a segfault caused by another bug in PyErr_NormalizeException() at Py_DECREF(*val), just before setting val to PyExc_RecursionErrorInst.
This is fixed by changing Py_DECREF(*val) to Py_XDECREF(*val).
With the above fix, we get the same abort as the one caused by runtimerror_singleton_2.py, so this is another reproducer of the current issue.
b) The test is ok with patch warn_5.patch, and the above fix.
c) With patch remove_singleton.patch the interpreter aborts with a fatal error when the high warter mark is exceeded, the infinite recursion is in PyErr_NormalizeException().
Cases 3) and 4) can be tested with runtimerror_singleton_3.py (install mymodule with setup.py for all three test cases in 4).
remove_singleton.patch introduces a regression in case c), but IMHO the abort in case c) is consistent with the abort in case 3), they
are both related to a more general problem involving the low/high water mark heuristic and described by Antoine in [1].
[1] http://thread.gmane.org/gmane.comp.python.devel/97016
vstinner commentedon Mar 24, 2016
I tried the following script on Python 3.5 and Python 3.6 and I failed to reproduce the bug:
---
Note: I had to add "+3" to the sys.setrecursionlimit() call, otherwise the limit is too low and you get a RecursionError (it's a recent bugfix, issue bpo-25274).
Can somone else please confirm that the bug is fixed?
xdegaye commentedon Mar 24, 2016
When tested with runtimerror_singleton_3.py (see msg 231933 above), the latest Python 3.6.0a0 (default:3eec7bcc14a4, Mar 24 2016, 20:16:19) still crashes:
vstinner commentedon Mar 26, 2016
warn_5.patch: The patch cannot be reviewed on Rietveld :-( You must not use the git format for diff.
warn_5.patch: "if (globals == NULL) { (...) return 0; }"
It looks like filename is not initialized. I suggest to use:
*filename = f->f_code->co_filename;
It looks like you have to add:
if (PyUnicode_Check(*filename)) *filename = NULL;
To mimick the code below.
xdegaye commentedon Mar 26, 2016
Victor,
With warn_5.patch *filename is not set when globals is NULL: setup_context() returns 0, and so do_warn() returns NULL without calling warn_explicit().
This is different from your initial warn.patch where setup_context() returns 1 in that case and an attempt is made to issue the warning.
vstinner commentedon Jun 1, 2017
The issue bpo-17852 is still alive and has a reference to this issue. It would be nice to rebase the latest patch on master and create a PR ;-)
xdegaye commentedon Jun 7, 2017
With PR 1981, a ResourceWarning is printed when a RecursionError occurs while normalizing another exception and its traceback holds a reference to a non-closed file object.
For information, bpo-5437 removed the MemoryError singleton for the same reasons as PR 1981 does.
xdegaye commentedon Jun 10, 2017
Antoine asked in PR 1981:
Just checked that crasher infinite_rec_2.py (removed by 1e534b5) does not crash with PR 1981. The other crashers listed at 1e534b5 are not valid Python 3.7 code. Does anyone know how to translate them into Python 3.7 ?
With PR 1981 infinite recursion does not occur in PyErr_NormalizeException() when the tstate->overflowed flag is false upon entering this function and:
* either (obviously) the normalizing of this exception does not fail
* or the normalizing of this exception fails with an exception whose normalization won't fail (for example a RecursionError).
Removing the PyExc_RecursionErrorInst singleton decreases the cases covered by the recursion checks because the test made upon using PyExc_RecursionErrorInst (in the 'finally' label of PyErr_NormalizeException()) has the side effect of adding another recursion check to the normal recursion machinery of _Py_CheckRecursiveCall(). Those are corner cases though, such as for example the following case that will abort instead now with PR 1981 [1]:
* tstate->overflowed has been set to true outside PyErr_NormalizeException() and the corresponding RecursionError has been discarded
* PyErr_NormalizeException() attempts normalizing a python exception that raises a python exception that raises ... (and so on indefinitely)
IMO it is ok to abort in such cases. As Brett wrote 9 years ago in https://mail.python.org/pipermail/python-dev/2008-August/082107.html:
"I have always viewed the check as a bonus sanity check, but not something to heavily rely upon."
One can also note that this other recursion check added with the use of PyExc_RecursionErrorInst does not respect the tstate->overflowed flag so that it adds another level of complexity to the recursion machinery.
[1] But with PR 1981, a RecursionError is raised when replacing MyException in test_recursion_normalizing_exception() at Lib/test/test_exceptions.py with:
serhiy-storchaka commentedon Jun 11, 2017
The fact that the traceback of PyExc_RecursionErrorInst causes an issue means that PyExc_RecursionErrorInst is used. We can't just remove PyExc_RecursionErrorInst since this can cause a stack overflow or, with merged PR 2035, an infinite loop.
Perhaps the solution of this issue is clearing __traceback__, __cause__ and __context__ attributes of PyExc_RecursionErrorInst as early as possible.
serhiy-storchaka commentedon Jun 11, 2017
The simplest solution -- make BaseException_set_tb(), BaseException_set_context() and BaseException_set_cause() no-ops for PyExc_RecursionErrorInst.
xdegaye commentedon Jun 13, 2017
Please give an example where a stack overflow occurs when PyExc_RecursionErrorInst has been removed.
xdegaye commentedon Oct 26, 2017
Fixed by bpo-30697.