Skip to content

Commit

Permalink
bpo-40882: Fix a memory leak in SharedMemory on Windows (GH-20684)
Browse files Browse the repository at this point in the history
In multiprocessing.shared_memory.SharedMemory(), the temporary view
returned by MapViewOfFile() should be unmapped when it is no longer
needed.
(cherry picked from commit 85c128e)

Co-authored-by: Zackery Spytz <zspytz@gmail.com>
  • Loading branch information
lukegarland and ZackerySpytz committed Dec 2, 2022
1 parent 64dae2e commit b027dd7
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
5 changes: 4 additions & 1 deletion Lib/multiprocessing/shared_memory.py
Expand Up @@ -173,7 +173,10 @@ def __init__(self, name=None, create=False, size=0):
)
finally:
_winapi.CloseHandle(h_map)
size = _winapi.VirtualQuerySize(p_buf)
try:
size = _winapi.VirtualQuerySize(p_buf)
finally:
_winapi.UnmapViewOfFile(p_buf)
self._mmap = mmap.mmap(-1, size, tagname=name)

self._size = size
Expand Down
@@ -0,0 +1,2 @@
Fix a memory leak in :class:`multiprocessing.shared_memory.SharedMemory` on
Windows.
25 changes: 25 additions & 0 deletions Modules/_winapi.c
Expand Up @@ -1402,6 +1402,30 @@ _winapi_MapViewOfFile_impl(PyObject *module, HANDLE file_map,
return address;
}

/*[clinic input]
_winapi.UnmapViewOfFile
address: LPCVOID
/
[clinic start generated code]*/

static PyObject *
_winapi_UnmapViewOfFile_impl(PyObject *module, LPCVOID address)
/*[clinic end generated code: output=4f7e18ac75d19744 input=8c4b6119ad9288a3]*/
{
BOOL success;

Py_BEGIN_ALLOW_THREADS
success = UnmapViewOfFile(address);
Py_END_ALLOW_THREADS

if (!success) {
return PyErr_SetFromWindowsErr(0);
}

Py_RETURN_NONE;
}

/*[clinic input]
_winapi.OpenFileMapping -> HANDLE
Expand Down Expand Up @@ -2095,6 +2119,7 @@ static PyMethodDef winapi_functions[] = {
_WINAPI_READFILE_METHODDEF
_WINAPI_SETNAMEDPIPEHANDLESTATE_METHODDEF
_WINAPI_TERMINATEPROCESS_METHODDEF
_WINAPI_UNMAPVIEWOFFILE_METHODDEF
_WINAPI_VIRTUALQUERYSIZE_METHODDEF
_WINAPI_WAITNAMEDPIPE_METHODDEF
_WINAPI_WAITFORMULTIPLEOBJECTS_METHODDEF
Expand Down
28 changes: 27 additions & 1 deletion Modules/clinic/_winapi.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 comments on commit b027dd7

@neo-michelepiperni
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow this is a really astute change. Props to the authors

@dylanjrae
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been bugging me for a long time, thanks so much @lukegarland! Always appreciate a good open source contribution😁

@neo-michelepiperni
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reminds me of a bugfix that was done in cpython. Awesome work again!

@cmrnfaith
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work @lukegarland! Good to see one of best devs working on Open Source code.

@dylanjrae
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lukegarland could you please look at an issue I have open here? I think you might be able to help with resolving it

Please sign in to comment.