[Python-bugs-list] [Bug #125891] windows popen4 crashes python when not closed correctly

noreply@sourceforge.net noreply@sourceforge.net
Fri, 19 Jan 2001 09:44:02 -0800


Bug #125891, was updated on 2000-Dec-15 05:53
Here is a current snapshot of the bug.

Project: Python
Category: Windows
Status: Open
Resolution: None
Bug Group: Platform-specific
Priority: 5
Submitted by: aisaksen
Assigned to : mhammond
Summary: windows popen4 crashes python when not closed correctly

Details: If you don't close both the istream file and ostream file return
values after calling popen4, then it crashes in somewhere in MSVCRT.DLL

Try the code included in this file.  If you call Crash(), then python will
crash after about 500 times through the loop.  NoCrash() works ok, because
you close both of the results.  This bug happens on both the www.python.org
release, as well as the ActivePython build.  I'm running Windows 2000, with
Visual Studio 6.0 installed.

This seems to be a Windows bug.  It dies in a call to setvbuf.  Recompiling
with the HAS_SETVBUF undefined still causes the same crash.  It would be
nice if python prevented this from happening.  Ideally, you should be able
to close the pipes, because there is no longer a reference to them.

-Aaron Isaksen

-- begin code --

import os

def Crash():
    n = 0
    while 1:
        p = os.popen4('dir')
        p[0].close()
        n +=1
        print n

def NoCrash():
    n = 0
    while 1:
        p = os.popen4('dir')
        p[0].close()
        p[1].close()
        n +=1
        print n

-- end code --

Follow-Ups:

Date: 2001-Jan-19 09:44
By: mhammond

Comment:
I can repro this.  It appears Python _never_ closes the file handles (the
Win2k task manager shows the handle count for the process growing at a
great rate).  It happens for all popen calls.

Although Python doesnt guarantee closing, the current implementation should
not allow this to happen.  I will look into this!
-------------------------------------------------------

Date: 2001-Jan-19 00:11
By: tim_one

Comment:
Mark, got a clue?  This is too painful for me:  the NoCrash() case on my
Win98SE box takes a full second for each popen4 call, while the Crash()
case tries to pop up an infinite sequence of conagent.exe error boxes after
just a few dozen (but much quicker <wink>) calls; at that point the system
gets too low on low-level resource to even open the Ctrl+Alt+Del box, and a
reboot is the only way out.

aisaksen, hate to say it, but I don't believe the popen family of functions
will ever be robust under Windows; popen isn't a native Windows concept (it
came from Unix), and MS's base support for it is simply hosed.  Note that
while Python may grow code to automagically close stuff when the object
goes away, Python makes no guarantees about when (or even if) object
destructors are called.  So you should never rely on Python closing things
for you.

-------------------------------------------------------

For detailed info, follow this link:
http://sourceforge.net/bugs/?func=detailbug&bug_id=125891&group_id=5470