This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Python 3.0 halts on shutdown when settrace is set
Type: crash Stage:
Components: Interpreter Core, Library (Lib) Versions: Python 3.0
Status: closed Resolution: duplicate
Dependencies: Superseder: possible deadlock in python IO implementation
View: 3618
Assigned To: Nosy List: fabioz, ggenellina, pitrou
Priority: normal Keywords:

Created on 2008-12-22 11:09 by fabioz, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit fabioz, 2008-12-22 11:09 Code showing halting problem on shutdown
Messages (4)
msg78169 - (view) Author: Fabio Zadrozny (fabioz) * Date: 2008-12-22 11:09
In Python 3.0, the interpreter will not shutdown properly after setting
a tracing function and something goes into stdout.

The code attached shows the problem in action: just execute it and
notice how the interpreter will be kept running after the code has been

There are some more things that need to be considered:
- If the print('here') is not called, it will shutdown
- If BOTH the print('here') and the sys.settrace(None) is not called, it
will NOT shutdown

Note: I've marked the bug as crash because it's halting, but maybe there
could be a better alternative for it...
msg78174 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2008-12-22 11:50
I haven't investigated, but in py3k print() has currently bits written
in Python, which means it will get (recursively) traced when called from
the trace function. It can of course have all kinds of funny implications!
msg78315 - (view) Author: Gabriel Genellina (ggenellina) Date: 2008-12-26 23:03
Yes, this is exactly the problem. The execution never goes beyond print
('here'); if you print frame.f_lineno you'll see it blocks at 
line 1036, waiting for a Lock for the second time.

So the trace function cannot use print, not write to regular files 
(because is written in Python). This is a severe limitation.

As a workaround, you can use the _fileio module (written in C):

import _fileio
f = _fileio._FileIO("output.txt", "w", True)

def tracing_func(frame, event, arg):
    f.write('%s %s %d\n' % (frame.f_code.co_filename, frame.f_code.co_
name, frame.f_lineno))
    return tracing_func

A possible fix would be to use an RLock instead of a Lock object, but 
I haven't investigated it.
msg78316 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2008-12-26 23:17
Thanks for the investigation! The problem has already been reported in
#3618. Closing this one.
Date User Action Args
2022-04-11 14:56:43adminsetgithub: 48966
2008-12-26 23:17:34pitrousetstatus: open -> closed
resolution: duplicate
superseder: possible deadlock in python IO implementation
messages: + msg78316
2008-12-26 23:03:22ggenellinasetnosy: + ggenellina
messages: + msg78315
components: + Library (Lib)
2008-12-22 11:50:38pitrousetnosy: + pitrou
messages: + msg78174
2008-12-22 11:09:05fabiozcreate