[New-bugs-announce] [issue17634] Win32: shutil.copy leaks file handles to child processes

Andrej Krpic report at bugs.python.org
Thu Apr 4 18:23:18 CEST 2013

New submission from Andrej Krpic:

Function shutil.copy opens source file with open('rb') and destination file with open('wb') and then proceeds to copy the content.

If you create child process from the same python process during that time, that process will inherit open file handles.

This could lead to situation when parent process tries to delete the file, but child process prevents that by holding an open handle to the file (Windows XP, Windows 7).
This is not expected side effect for copy file operation.
Win32's native CopyFile API call doesn't leak file handles to child processes.

Python's open function behavior is defined by platform's implementation, which translates C function fopen with 'rb' and 'wb' modes to CRT open call without O_NOINHERIT flag, which creates inheritable Win32 HANDLEs.

Possible fix would be to add 'N' to mode string in calls to open function in shutil.copy.

components: Library (Lib), Windows
messages: 186041
nosy: akrpic77
priority: normal
severity: normal
status: open
title: Win32: shutil.copy leaks file handles to child processes
type: security
versions: Python 2.6, Python 2.7, Python 3.1, Python 3.2, Python 3.3

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list