Title: Crash due to I/O in __del__
Type: crash Stage:
Components: Versions: Python 3.4
Status: closed Resolution: duplicate
Dependencies: Superseder: Calling traceback.format_exception() during Pyhon shutdown does crash Python
View: 20037
Assigned To: Nosy List: gvanrossum, pitrou, vstinner
Priority: normal Keywords:

Created on 2013-12-21 01:35 by gvanrossum, last changed 2013-12-21 11:57 by pitrou. This issue is now closed.

File name Uploaded Description Edit
@bt.txt gvanrossum, 2013-12-21 01:35
Messages (2)
msg206721 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2013-12-21 01:35
I was writing a new Tulip example (a cache client and server, not yet public) and I noticed that when I interrupted the client with ^C I got a traceback (expected) followed by a segfault (unexpected).  This is on OSX 10.8 but I don't think it is platform dependent.

A little experiment showed that this only happened with Python 3.4 and only with the latest Tulip, where Future has a __del__ method.  

According to gdb, the segfault happens on the first line of PyModule_GetState(), because the argument 'm' is NULL.

Putting a NULL check in this function averts the segfault but give the following disturbing extra traceback:

--- Logging error ---
Traceback (most recent call last):
Exception ignored in: <bound method Task.__del__ of Task(<testing>)<exception=KeyboardInterrupt()>>
Traceback (most recent call last):
  File "/Users/guido/tulip/asyncio/", line 177, in __del__
  File "/Users/guido/cpython/Lib/logging/", line 1278, in error
  File "/Users/guido/cpython/Lib/logging/", line 1384, in _log
  File "/Users/guido/cpython/Lib/logging/", line 1394, in handle
  File "/Users/guido/cpython/Lib/logging/", line 1456, in callHandlers
  File "/Users/guido/cpython/Lib/logging/", line 835, in handle
  File "/Users/guido/cpython/Lib/logging/", line 959, in emit
  File "/Users/guido/cpython/Lib/logging/", line 888, in handleError
  File "/Users/guido/cpython/Lib/", line 169, in print_exception
  File "/Users/guido/cpython/Lib/", line 153, in _format_exception_iter
  File "/Users/guido/cpython/Lib/", line 18, in _format_list_iter
  File "/Users/guido/cpython/Lib/", line 65, in _extract_tb_or_stack_iter
  File "/Users/guido/cpython/Lib/", line 15, in getline
  File "/Users/guido/cpython/Lib/", line 41, in getlines
  File "/Users/guido/cpython/Lib/", line 126, in updatecache
  File "/Users/guido/cpython/Lib/", line 431, in open
TypeError: bad argument type for built-in operation

This suggests the problem is triggered by some I/O due to the exception logging in the __del__ method.

The gdb traceback (too big to post here) tells me that this PyModule_GetState() call is in the IO_STATE macro in textiowrapper_init().  The whole thing seems to be in a GC run called from Py_Finalize().  Check out the attached @bt.txt.

Any ideas?  (The TypeError is simply what PyModule_GetState() returns for a non-module argument -- I made it take the same exit path for NULL.)
msg206724 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2013-12-21 11:04
> This issue is a duplicate of issue #20037.
Date User Action Args
2013-12-21 11:57:55pitrousetstatus: open -> closed
superseder: Calling traceback.format_exception() during Pyhon shutdown does crash Python
resolution: duplicate
2013-12-21 11:04:52vstinnersetmessages: + msg206724
2013-12-21 01:35:32gvanrossumcreate