Skip to content

Commit 0b58b7a

Browse files
ZackerySpytzlukegarland
authored andcommitted
bpo-40882: fix merge conflict for 310 backport
1 parent 3baaa97 commit 0b58b7a

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

Lib/multiprocessing/shared_memory.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,10 @@ def __init__(self, name=None, create=False, size=0):
173173
)
174174
finally:
175175
_winapi.CloseHandle(h_map)
176-
size = _winapi.VirtualQuerySize(p_buf)
176+
try:
177+
size = _winapi.VirtualQuerySize(p_buf)
178+
finally:
179+
_winapi.UnmapViewOfFile(p_buf)
177180
self._mmap = mmap.mmap(-1, size, tagname=name)
178181

179182
self._size = size
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a memory leak in :class:`multiprocessing.shared_memory.SharedMemory` on
2+
Windows.

Modules/_winapi.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,6 +1402,30 @@ _winapi_MapViewOfFile_impl(PyObject *module, HANDLE file_map,
14021402
return address;
14031403
}
14041404

1405+
/*[clinic input]
1406+
_winapi.UnmapViewOfFile
1407+
1408+
address: LPCVOID
1409+
/
1410+
[clinic start generated code]*/
1411+
1412+
static PyObject *
1413+
_winapi_UnmapViewOfFile_impl(PyObject *module, LPCVOID address)
1414+
/*[clinic end generated code: output=4f7e18ac75d19744 input=8c4b6119ad9288a3]*/
1415+
{
1416+
BOOL success;
1417+
1418+
Py_BEGIN_ALLOW_THREADS
1419+
success = UnmapViewOfFile(address);
1420+
Py_END_ALLOW_THREADS
1421+
1422+
if (!success) {
1423+
return PyErr_SetFromWindowsErr(0);
1424+
}
1425+
1426+
Py_RETURN_NONE;
1427+
}
1428+
14051429
/*[clinic input]
14061430
_winapi.OpenFileMapping -> HANDLE
14071431
@@ -2095,6 +2119,7 @@ static PyMethodDef winapi_functions[] = {
20952119
_WINAPI_READFILE_METHODDEF
20962120
_WINAPI_SETNAMEDPIPEHANDLESTATE_METHODDEF
20972121
_WINAPI_TERMINATEPROCESS_METHODDEF
2122+
_WINAPI_UNMAPVIEWOFFILE_METHODDEF
20982123
_WINAPI_VIRTUALQUERYSIZE_METHODDEF
20992124
_WINAPI_WAITNAMEDPIPE_METHODDEF
21002125
_WINAPI_WAITFORMULTIPLEOBJECTS_METHODDEF

Modules/clinic/_winapi.c.h

Lines changed: 27 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)