Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
1ce16fb0
Commit
1ce16fb0
authored
Sep 18, 2019
by
Victor Stinner
Committed by
GitHub
Sep 18, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-38070: Py_FatalError() logs runtime state (GH-16246)
parent
d3b90414
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
73 additions
and
32 deletions
+73
-32
Lib/test/test_capi.py
Lib/test/test_capi.py
+2
-0
Lib/test/test_faulthandler.py
Lib/test/test_faulthandler.py
+12
-6
Python/pylifecycle.c
Python/pylifecycle.c
+59
-26
No files found.
Lib/test/test_capi.py
View file @
1ce16fb0
...
...
@@ -198,6 +198,7 @@ class CAPITest(unittest.TestCase):
self
.
assertRegex
(
err
.
replace
(
b'
\
r
'
,
b''
),
br'Fatal Python error: a function returned NULL '
br'without setting an error\n'
br'Python runtime state: initialized\n'
br'SystemError: <built-in function '
br'return_null_without_error> returned NULL '
br'without setting an error\n'
...
...
@@ -225,6 +226,7 @@ class CAPITest(unittest.TestCase):
self
.
assertRegex
(
err
.
replace
(
b'
\
r
'
,
b''
),
br'Fatal Python error: a function returned a '
br'result with an error set\n'
br'Python runtime state: initialized\n'
br'ValueError\n'
br'\n'
br'The above exception was the direct cause '
...
...
Lib/test/test_faulthandler.py
View file @
1ce16fb0
...
...
@@ -90,7 +90,8 @@ class FaultHandlerTests(unittest.TestCase):
def
check_error
(
self
,
code
,
line_number
,
fatal_error
,
*
,
filename
=
None
,
all_threads
=
True
,
other_regex
=
None
,
fd
=
None
,
know_current_thread
=
True
):
fd
=
None
,
know_current_thread
=
True
,
py_fatal_error
=
False
):
"""
Check that the fault handler for fatal errors is enabled and check the
traceback from the child process output.
...
...
@@ -110,10 +111,12 @@ class FaultHandlerTests(unittest.TestCase):
{header} \
(mos
t recent call first\
):
File "<string>", line {lineno} in <module>
"""
regex
=
dedent
(
regex
.
format
(
if
py_fatal_error
:
fatal_error
+=
"
\
n
Python runtime state: initialized"
regex
=
dedent
(
regex
).
format
(
lineno
=
line_number
,
fatal_error
=
fatal_error
,
header
=
header
)
)
.
strip
()
header
=
header
).
strip
()
if
other_regex
:
regex
+=
'|'
+
other_regex
output
,
exitcode
=
self
.
get_output
(
code
,
filename
=
filename
,
fd
=
fd
)
...
...
@@ -170,7 +173,8 @@ class FaultHandlerTests(unittest.TestCase):
"""
,
3
,
'in new thread'
,
know_current_thread
=
False
)
know_current_thread
=
False
,
py_fatal_error
=
True
)
def
test_sigabrt
(
self
):
self
.
check_fatal_error
(
"""
...
...
@@ -226,7 +230,8 @@ class FaultHandlerTests(unittest.TestCase):
faulthandler._fatal_error(b'xyz')
"""
,
2
,
'xyz'
)
'xyz'
,
py_fatal_error
=
True
)
def
test_fatal_error_without_gil
(
self
):
self
.
check_fatal_error
(
"""
...
...
@@ -234,7 +239,8 @@ class FaultHandlerTests(unittest.TestCase):
faulthandler._fatal_error(b'xyz', True)
"""
,
2
,
'xyz'
)
'xyz'
,
py_fatal_error
=
True
)
@
unittest
.
skipIf
(
sys
.
platform
.
startswith
(
'openbsd'
),
"Issue #12868: sigaltstack() doesn't work on "
...
...
Python/pylifecycle.c
View file @
1ce16fb0
...
...
@@ -1975,13 +1975,14 @@ done:
static
void
_Py_FatalError_DumpTracebacks
(
int
fd
)
_Py_FatalError_DumpTracebacks
(
int
fd
,
PyInterpreterState
*
interp
,
PyThreadState
*
tstate
)
{
fputc
(
'\n'
,
stderr
);
fflush
(
stderr
);
/* display the current Python stack */
_Py_DumpTracebackThreads
(
fd
,
NULL
,
NULL
);
_Py_DumpTracebackThreads
(
fd
,
interp
,
tstate
);
}
/* Print the current exception (if an exception is set) with its traceback,
...
...
@@ -2079,10 +2080,39 @@ fatal_output_debug(const char *msg)
}
#endif
static
void
fatal_error_dump_runtime
(
FILE
*
stream
,
_PyRuntimeState
*
runtime
)
{
fprintf
(
stream
,
"Python runtime state: "
);
if
(
runtime
->
finalizing
)
{
fprintf
(
stream
,
"finalizing (tstate=%p)"
,
runtime
->
finalizing
);
}
else
if
(
runtime
->
initialized
)
{
fprintf
(
stream
,
"initialized"
);
}
else
if
(
runtime
->
core_initialized
)
{
fprintf
(
stream
,
"core initialized"
);
}
else
if
(
runtime
->
preinitialized
)
{
fprintf
(
stream
,
"preinitialized"
);
}
else
if
(
runtime
->
preinitializing
)
{
fprintf
(
stream
,
"preinitializing"
);
}
else
{
fprintf
(
stream
,
"unknown"
);
}
fprintf
(
stream
,
"
\n
"
);
fflush
(
stream
);
}
static
void
_Py_NO_RETURN
fatal_error
(
const
char
*
prefix
,
const
char
*
msg
,
int
status
)
{
const
int
fd
=
fileno
(
stderr
);
FILE
*
stream
=
stderr
;
const
int
fd
=
fileno
(
stream
);
static
int
reentrant
=
0
;
if
(
reentrant
)
{
...
...
@@ -2092,45 +2122,48 @@ fatal_error(const char *prefix, const char *msg, int status)
}
reentrant
=
1
;
fprintf
(
st
derr
,
"Fatal Python error: "
);
fprintf
(
st
ream
,
"Fatal Python error: "
);
if
(
prefix
)
{
fputs
(
prefix
,
st
derr
);
fputs
(
": "
,
st
derr
);
fputs
(
prefix
,
st
ream
);
fputs
(
": "
,
st
ream
);
}
if
(
msg
)
{
fputs
(
msg
,
st
derr
);
fputs
(
msg
,
st
ream
);
}
else
{
fprintf
(
st
derr
,
"<message not set>"
);
fprintf
(
st
ream
,
"<message not set>"
);
}
fputs
(
"
\n
"
,
st
derr
);
fflush
(
st
derr
);
/* it helps in Windows debug build */
fputs
(
"
\n
"
,
st
ream
);
fflush
(
st
ream
);
/* it helps in Windows debug build */
/* Check if the current thread has a Python thread state
and holds the GIL */
PyThreadState
*
tss_tstate
=
PyGILState_GetThisThreadState
();
if
(
tss_tstate
!=
NULL
)
{
PyThreadState
*
tstate
=
_PyThreadState_GET
();
if
(
tss_tstate
!=
tstate
)
{
/* The Python thread does not hold the GIL */
tss_tstate
=
NULL
;
}
}
else
{
/* Py_FatalError() has been called from a C thread
which has no Python thread state. */
_PyRuntimeState
*
runtime
=
&
_PyRuntime
;
fatal_error_dump_runtime
(
stream
,
runtime
);
PyThreadState
*
tstate
=
_PyRuntimeState_GetThreadState
(
runtime
);
PyInterpreterState
*
interp
=
NULL
;
if
(
tstate
!=
NULL
)
{
interp
=
tstate
->
interp
;
}
int
has_tstate_and_gil
=
(
tss_tstate
!=
NULL
);
/* Check if the current thread has a Python thread state
and holds the GIL.
tss_tstate is NULL if Py_FatalError() is called from a C thread which
has no Python thread state.
tss_tstate != tstate if the current Python thread does not hold the GIL.
*/
PyThreadState
*
tss_tstate
=
PyGILState_GetThisThreadState
();
int
has_tstate_and_gil
=
(
tss_tstate
!=
NULL
&&
tss_tstate
==
tstate
);
if
(
has_tstate_and_gil
)
{
/* If an exception is set, print the exception with its traceback */
if
(
!
_Py_FatalError_PrintExc
(
fd
))
{
/* No exception is set, or an exception is set without traceback */
_Py_FatalError_DumpTracebacks
(
fd
);
_Py_FatalError_DumpTracebacks
(
fd
,
interp
,
tss_tstate
);
}
}
else
{
_Py_FatalError_DumpTracebacks
(
fd
);
_Py_FatalError_DumpTracebacks
(
fd
,
interp
,
tss_tstate
);
}
/* The main purpose of faulthandler is to display the traceback.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment