Windows process ownership trouble

Tim Golden mail at timgolden.me.uk
Wed Jun 25 09:33:34 EDT 2008


geoffbache wrote:
> Am currently being very confused over the following code on Windows
> 
> import subprocess, os
> 
> file = open("filename", "w")
> try:
>     proc = subprocess.Popen("nosuchprogram", stdout=file)
> except OSError:
>     file.close()
>     os.remove("filename")
> 
> This produces the following exception:
> 
> Traceback (most recent call last):
>   File "C:\processown.py", line 10, in <module>
>     os.remove("filename")
> WindowsError: [Error 32] The process cannot access the file because it
> is being used by another process: 'filename'
> 
> How can it be in use by another process? The process didn't even
> start, right?

[slight aside: best not to use "file" as an identifier; it shadows the
builtin "file" factory function. Not fatal, but might trip you up
somewhere later.]

This is awkward. What's happening behind the scenes is that,
as it creates the Popen object, the subprocess module gets
hold of the Windows handle behind your file. Then it calls
the CreateProcess API to create the process. If that succeeds,
it does whatever it does, closes the file handles and returns.
If it fails -- and here it fails, obviously -- it reraises the error
as a WindowsError. WindowsError is a subclass of OSError so
is trapped by your exception handler.

But -- and this is the point -- this exception is raised before the
the internal handles are closed, and altho' I don't know exactly 
what effect this will have it clearly prevents the file from being 
removed. And, as far as I can see, will continue to prevent its 
being removed until the whole Python process exits and its 
handles released.

I'm happy to be corrected on this, but it looks to me like a
bug in the subprocess error handling. If I'm right, I'm a little 
surprised this hasn't bitten someone before but a quick search
of the bugs database doesn't seem to turn anything up.

TJG



More information about the Python-list mailing list