classification
Title: Crash on shutdown after os.fdopen(2) in debug builds
Type: Stage:
Components: Windows Versions: Python 2.7, Python 2.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: krisvale Nosy List: amaury.forgeotdarc, krisvale, pitrou
Priority: normal Keywords:

Created on 2009-04-16 12:39 by amaury.forgeotdarc, last changed 2009-04-17 21:19 by pitrou.

Messages (5)
msg86026 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2009-04-16 12:39
With a 2.6 or 2.7 debug build:
    python_d -c "import os; os.fdopen(2)"

Displays the famous Debug Assertion Failure:
    ...
    Expression: (_osfile(fh) & FOPEN)
    ...

The error occurs when closing pyhon, in call_ll_exitfuncs() there is a
call to fflush(stderr)
msg86027 - (view) Author: Kristján Valur Jónsson (krisvale) * Date: 2009-04-16 12:51
Well, the short answer is don´t do that!

Do os.fdopen(os.fdup(2))
You are stealing the fd 2 away from stderr, then closing it, and stderr 
now holds a defunct file descriptor.
msg86035 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2009-04-16 16:18
Sure. But we tried hard to remove all these assertion failures, so that 
python never shows a popup dialog.
msg86065 - (view) Author: Kristján Valur Jónsson (krisvale) * Date: 2009-04-17 10:18
Yes.  We have tried to accomodate the most reckless use of file 
descriptors.
I'm not sure how to proceed here though, since the builtin streams are 
beyond our control.

I think you would have the same effect if you called os.close(2).

Perhaps the only really good solution is to allow for disabling the fd 
checks after all, but to make it optional, somehow.  Ideally something 
that python.exe sets up, it being the prime motivator....

Really, this fd checking in mscrt is becoming really annoying.  I'm 
inclined to start lobbying MS to remove at least that part of the 
runtime checks by default.  Not that it will do us any good.

Or, perhaps, we should just document the caveats with using file 
descriptors directly in python.  That one has to be careful with their 
use, for example, to not steal them from streams that may be using 
them, not closing them when someone else is, and so on.  Use at your 
own risk.  After all, there are numerous ways to crash python through 
reckless use of code, e.g. by using ctypes.
msg86092 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2009-04-17 21:19
I thought there was a _PyVerify_Fd() just for that, why couldn't it be
used here too?
(not that I think sparkling _PyVerify_Fd() everywhere in our code base
is reasonable and maintainable, but I didn't make that choice :-))
History
Date User Action Args
2009-04-17 21:19:07pitrousetnosy: + pitrou
messages: + msg86092
2009-04-17 10:18:09krisvalesetmessages: + msg86065
2009-04-16 16:18:45amaury.forgeotdarcsetmessages: + msg86035
2009-04-16 12:51:49krisvalesetmessages: + msg86027
2009-04-16 12:39:08amaury.forgeotdarccreate