classification
Title: Memory leak in os.path.isdir under Windows
Type: resource usage Stage: resolved
Components: Extension Modules, Windows Versions: Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: brian.curtin, nneonneo, python-dev, serhiy.storchaka
Priority: normal Keywords:

Created on 2013-01-27 09:10 by nneonneo, last changed 2013-01-28 19:30 by brian.curtin. This issue is now closed.

Messages (7)
msg180754 - (view) Author: Robert Xiao (nneonneo) * Date: 2013-01-27 09:10
[From http://stackoverflow.com/questions/12648737/huge-memory-leak-in-repeated-os-path-isdir-calls]

os.path.isdir() leaks memory under Windows in Python 2.7. The core cause is this snippet:

Modules/posixmodule.c:4226:
    if (!PyArg_ParseTuple(args, "et:_isdir",
                          Py_FileSystemDefaultEncoding, &path))
        return NULL;

    attributes = GetFileAttributesA(path);
    if (attributes == INVALID_FILE_ATTRIBUTES)
        Py_RETURN_FALSE;

check:
    if (attributes & FILE_ATTRIBUTE_DIRECTORY)
        Py_RETURN_TRUE;
    else
        Py_RETURN_FALSE;


The buffer returned by the "et" ParseTuple format must be freed after use. Since it isn't freed, we get a memory leak here.

Patch:

--- a/Modules/posixmodule.c	Mon Apr 09 19:04:04 2012 -0400
+++ b/Modules/posixmodule.c	Sun Jan 27 04:10:34 2013 -0500
@@ -4226,6 +4226,7 @@
         return NULL;
 
     attributes = GetFileAttributesA(path);
+    PyMem_Free(path);
     if (attributes == INVALID_FILE_ATTRIBUTES)
         Py_RETURN_FALSE;
msg180762 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-01-27 11:59
Good catch. Thank you, Robert Xiao.

Can you please submit a contributor form?

http://python.org/psf/contrib/contrib-form/
http://python.org/psf/contrib/
msg180772 - (view) Author: Robert Xiao (nneonneo) * Date: 2013-01-27 15:33
The PSF form really needs a PDF version. Anyway, 'tis duly submitted.
msg180869 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-01-28 18:22
New changeset 4deb294ff567 by Serhiy Storchaka in branch '2.7':
Issue #17051: Fix a memory leak in os.path.isdir() on Windows. Patch by Robert Xiao.
http://hg.python.org/cpython/rev/4deb294ff567
msg180870 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-01-28 18:25
Committed. Thank you for patch.
msg180872 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-01-28 18:29
New changeset 51173aba06eb by Serhiy Storchaka in branch '2.7':
Add Robert Xiao to Misc/ACKS for issue17051.
http://hg.python.org/cpython/rev/51173aba06eb
msg180874 - (view) Author: Brian Curtin (brian.curtin) * (Python committer) Date: 2013-01-28 19:30
Robert, thanks a lot for this fix and your contributor agreement. We're currently working on making it easier to submit the contributor agreement so you won't have to print it out and mail/fax/scan it...even though you already did it and you're now covered :)
History
Date User Action Args
2013-01-28 19:30:36brian.curtinsetnosy: + brian.curtin
messages: + msg180874
2013-01-28 18:29:21python-devsetmessages: + msg180872
2013-01-28 18:25:27serhiy.storchakasetstatus: open -> closed
resolution: fixed
messages: + msg180870

stage: commit review -> resolved
2013-01-28 18:22:50python-devsetnosy: + python-dev
messages: + msg180869
2013-01-27 15:33:30nneonneosetmessages: + msg180772
2013-01-27 11:59:20serhiy.storchakasetassignee: serhiy.storchaka
components: + Extension Modules, Windows, - Library (Lib)
versions: - Python 2.6
nosy: + serhiy.storchaka

messages: + msg180762
stage: commit review
2013-01-27 09:10:56nneonneocreate