New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Different 3.0a1 exit behavior #45670
Comments
Python 3.0a1 on Linux and MacOS X 10.4.10 exits differently than Python With previous Python binaries the destructor** function of any pre- A workaround is to install a SIGABRT signal handler from the library and Python 3.0a1 was built from source using the standard build sequence --- |
Thanks for testing 3.0. Do you have any idea why they are no longer called? I don't recall any |
Here is one thought, maybe 3.0a calls _exit() while 2.x uses exit() to That would explain the difference but only if destructor functions are |
Sorry, premature submit. I will try using atexit() and report what |
Using atexit() to install the destructor function does not help. The Btw, that would mean that any C extension which uses atexit() directly Running python with the debugger shows that 3.0a1 and 2.5.1 both exit There is a new atexitmodule.c in 3.0a1 which did not exits in 2.5.1. This man page <http://linux.die.net/man/3/atexit\> mentions that all That is as far as I got. |
Can you provide a very small shared library that demonstrates this |
Yes, I will make a small library. But first, here is another piece of As I mentioned, using std atexit does not work on 3.0a1. But There must something in Py_Terminate or Py_Finalize which is different |
Attached is a simple test case which demonstrates the problem on Linux See the comments for build steps and example output with 3 different If you need another test case which uses Py_AtExit, let me know. |
Here is the same file with an #if to use to Py_AtExit or destructor case. |
I can build it just fine on Ubuntu dapper, but I can't run it. The $ env LD_PRELOAD dlibtest.so ~/p3/python
env: LD_PRELOAD: No such file or directory
$ When I modify it slightly I get another error: $ env LD_PRELOAD=dlibtest.so ~/p3/python
ERROR: ld.so: object 'dlibtest.so' from LD_PRELOAD cannot be preloaded: ignored.
Python 3.0a1+ (py3k, Oct 26 2007, 12:30:11)
[GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> ^D
$ |
My fault, sorry. The command line to run should be env LD_PRELOAD=./dlibtest.so ... i.e. with '=' sign and no spaces. And the library file may have to be |
OK, confirmed. But no insignt in what happened yet... Do you know where |
The Py_AtExit function is in Python/pythonrun.c. The calls to all I am just getting down there now and Py_Finalize is called and reaches |
One more thing. Stepping with the debugger thru Py_Finalize looks exactly |
I put a bunch of printf's in the Py_Finalize function of 2.5.1 and All those show up when 2.5.1 exists including the call to But in 3.0a1 only a few show up and the last one is just before the call |
Maybe that's because site and io get cleaned up and then there is some |
This is quite bizarre and difficult to reproduce. With gdb, 3.0a1 does Also, the call to static function call_all_exitfuncs is inlined, its |
I suggest you configure --with-pydebug. That will disable On Oct 26, 2007 4:55 PM, Jean Brouwers <report@bugs.python.org> wrote:
|
OK, I try that. |
The 3.0a1 build --with-pydebug behaves the same as before (on Linux). The |
It looks like the problem may indeed just be that I/O is being shut down |
Attached is an updated dlibtest.c file. It prints a message in the con- Compile and run it as before and check the exit status. If the latter This version can also be used with gdb, either by pre-loading the gdb .../python or to preload before gdb use setenv LD_PRELOAD ./dlibtest.so Lastly, my previous observations about this issue were clearly a "trompe |
When I run with the attached patch, I see the message: *** dtor called in python ... Is that the behavior you expect? |
Yes, that is the expected behavior in this case. |
One final comment as confirmation. If the messages are written to a file, |
So is there even a bug? Arguably you shouldn't be writing anything that |
It is quite common to pre-load libraries into existing binaries e.g. for For a while, the initial symptoms looked like the different exit Therefore, 3.0 should probably not do that either. But that is really |
One more argument. Without a fix, 3.0 would not even print a C debug |
Can you try this patch, please? It has the same effect as the other Index: Modules/_fileio.c --- Modules/_fileio.c (Revision 58699)
+++ Modules/_fileio.c (Arbeitskopie)
@@ -270,7 +270,8 @@
if (self->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *) self); - if (self->fd >= 0) {
+ /* Don't close stdout and stderr */
+ if (self->fd == 0 || self->fd > 2) {
errno = internal_close(self);
if (errno < 0) {
#ifdef HAVE_STRERROR |
I could not try Neal's patch since it does not seem to apply to the $ env LD_PRELOAD=./dlibtest4.so ~/Python-3dbg/python
*** ctor called in python ...
*** atexit OK in python ...
Python 3.0a1 (py3k, Oct 28 2007, 10:23:59)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
[36000 refs]
[21985 refs]
*** dtor called in python ...
$ Most interesting is that this Python build --with-pydebug now prints 2 Also, attached is another version of my test case renamed to dlibtest4. |
Perhaps, the proper behavior is the following. After calling all functions/methods installed at the Python level with However, the C level files stdin, stdout and stderr are *never* closed |
Right. I think the right solution is to add an option to _FileIO that |
Here you go, Guido! |
Thanks!! Code review: Shouldn't closefd be passed as 1 in import.c? I don't see the point of distinguishing between -1 and +1. The block In io.py, you should document that closefd must not be False when a I think in _fileio.c, you can insist that the closefd argument is an int I don't think we should warn when trying to close an unclosable fd; it Please don't add trailing whitespace. |
Guido van Rossum wrote:
I used -1 as default to keep it consistent with buffer=-1. I figured out
Done
Thanks, it makes the code a bit easier.
I think we should keep the warning. The warning made me aware of a minor
I've reconfigured my editor to remove trailing spaces. I've attached a combined patch for closefd and preliminary stderr. Christian |
OK, thanks. The closefd part is good, but the stderrprinter part has a $ ./python -S
python: Modules/gcmodule.c:336: visit_reachable: Assertion `gc_refs > 0
|| gc_refs == (-3) || gc_refs == (-2)' failed.
Aborted
$ If I comment out the PySys_SetObject() call everything seems fine, but I |
I've checked the closefd patch (which minor changes) into the py3k branch. Committed revision 58711. Please take the stdprinter patch to the original issue (bug 1352). |
Guido van Rossum wrote:
I may have found the problem. I forgot th remove Py_TPFLAGS_HAVE_GC from $ ./python
Fatal Python error: Py_Initialize: can't initialize sys standard streams
Traceback (most recent call last):
File "/home/heimes/dev/python/py3k/Lib/io.py", line 22, in <module>
test
NameError: name 'test' is not defined
Aborted $ vi Lib/io.py $ ./python -S
Python 3.0a1+ (py3k:58715M, Oct 30 2007, 19:02:47)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
>>> import sys
[33116 refs]
>>> sys.maxunicode
1114111
[33127 refs]
>>>
[33128 refs]
[23233 refs] $ python2.5 -c "import sys; print sys.maxunicode"
1114111 |
Thanks, I've closed bpo-1352 too now. |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: