[python-win32] file descriptor issues using msvcrt.open_osfhandle()+os.fdopen()

Scott Leerssen sleerssen at gmail.com
Mon Dec 19 23:37:09 CET 2011


adding python-win32 to the rest of the thread...  I missed a reply-all in here somewhere...

On Dec 19, 2011, at 5:06 PM, Scott Leerssen wrote:

> 
> On Dec 19, 2011, at 5:01 PM, Mark Hammond wrote:
> 
>> Note you are also closing h.  But more generally, I wouldn't be at all surprised to find that closing fd also closes the win32 handle associated with it, otherwise every CRT handle ever opened would leak the OS handle.
> 
> It does close the descriptor.  However, I still see screwy behavior with other sockets and descriptors in other threads.  It's almost like there are two sides of the descriptor, and one gets reused before the other is released, resulting in unexpected closure of descriptors or "stealing" of one threads descriptor when another thread opens a file or socket.  It is truly odd.
> 
>> IOW, I think you can just detach it and ignore closing it.
>> 
>> Mark
>> 
>> On 20/12/2011 8:43 AM, Scott Leerssen wrote:
>>> 
>>> On Dec 19, 2011, at 4:32 PM, Mark Hammond wrote:
>>> 
>>>> On 18/12/2011 4:34 AM, Scott Leerssen wrote:
>>>>> 
>>>>> On Dec 17, 2011, at 12:25 PM, Amaury Forgeot d'Arc wrote:
>>>>> 
>>>>>> 2011/12/17 Scott Leerssen<sleerssen at gmail.com
>>>>>> <mailto:sleerssen at gmail.com>>
>>>>>> 
>>>>>>   I did see that, but I interpreted that to mean that the PyHANDLE
>>>>>>   would be dereferenced from the underlying Windows file handle,
>>>>>> 
>>>>>> 
>>>>>> It's the other way round: the PyHandle just forgets the win32 handle
>>>>>> value, and will not close it in its __del__.
>>>>> 
>>>>> Sorry, I did write that backward. How is the win32 handle freed?
>>>> 
>>>> The .Detach() method returns an integer handle value which can then be closed using win32api.CloseHandle().
>>> 
>>> I just tried that, and after closing the file object and attempting to call win32api.ClosHandle() on the value returned by detach, I get an exception of "error: (6, 'CloseHandle', 'The handle is invalid.')"  Here's what the code looks like:
>>> 
>>>    h = win32file.CreateFile(
>>>        fname,
>>>        win32file.GENERIC_READ,
>>>        win32file.FILE_SHARE_READ,
>>>        None,
>>>        win32file.OPEN_EXISTING,
>>>        win32file.FILE_FLAG_BACKUP_SEMANTICS,
>>>        None
>>>        )
>>>    hd = h.Detach()
>>>    fd = msvcrt.open_osfhandle(hd, os.O_RDONLY)
>>>    f = os.fdopen(fd, 'rb')
>>>    f.read()
>>>    f.close()
>>>    h.Close()
>>>    win32file.CloseHandle(hd)
>>> 
>>> 
>> 
> 



More information about the python-win32 mailing list