Closed vstinner closed 3 weeks ago
The bug can be reproduced just with these 2 tests:
$ cat bisect8
test.test_capi.test_misc.SubinterpreterTest.test_configured_settings
test.test_capi.test_run.CAPITest.test_run_fileexflags
$ ./python -m test test_capi -v --matchfile=bisect8
Sometimes, I get the glibc error message:
Fatal error: glibc detected an invalid stdio handle
The crash is more likely if PYC files are removed:
rm -rf ./Lib/__pycache__ ./Lib/test/test_capi/__pycache__
I cannot reproduce the issue with gcc -O0
and gcc -Og
, only with a release build: gcc -O3
.
This buildbot uses an old GCC version: gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
.
If it is only reproducible with a release build, would the following patch help?
diff --git a/Modules/_testcapi/run.c b/Modules/_testcapi/run.c
index 4fd98b82d76..b35a12e9424 100644
--- a/Modules/_testcapi/run.c
+++ b/Modules/_testcapi/run.c
@@ -74,6 +74,7 @@ run_fileexflags(PyObject *mod, PyObject *pos_args)
result = PyRun_FileExFlags(fp, filename, start, globals, locals, closeit, pflags);
+#ifndef NDEBUG
#if defined(__linux__) || defined(MS_WINDOWS) || defined(__APPLE__)
/* The behavior of fileno() after fclose() is undefined, but it is
* the only practical way to check whether the file was closed.
@@ -85,6 +86,7 @@ run_fileexflags(PyObject *mod, PyObject *pos_args)
return NULL;
}
#endif
+#endif // NDEBUG
if (!closeit && fileno(fp) < 0) {
PyErr_SetString(PyExc_AssertionError, "Bad file descriptor after excution");
Py_XDECREF(result);
Or maybe just remove this dangerous implementation depending code?
Or maybe just remove this dangerous implementation depending code?
I wrote PR gh-118429 to use fstat() instead of relying on an undefined behavior of fileno() on a closed file.
Bug report
build: https://buildbot.python.org/all/#/builders/43/builds/5305/steps/5/logs/stdio
Logs:
Linked PRs