Skip to content

Commit bb9593a

Browse files
sorciobenjaminp
authored andcommitted
closes bpo-36139: release GIL around munmap(). (GH-12073)
1 parent b71e28e commit bb9593a

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Release GIL when closing :class:`~mmap.mmap` objects.

Modules/mmapmodule.c

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ typedef struct {
117117
static void
118118
mmap_object_dealloc(mmap_object *m_obj)
119119
{
120+
Py_BEGIN_ALLOW_THREADS
120121
#ifdef MS_WINDOWS
121122
if (m_obj->data != NULL)
122123
UnmapViewOfFile (m_obj->data);
@@ -135,6 +136,7 @@ mmap_object_dealloc(mmap_object *m_obj)
135136
munmap(m_obj->data, m_obj->size);
136137
}
137138
#endif /* UNIX */
139+
Py_END_ALLOW_THREADS
138140

139141
if (m_obj->weakreflist != NULL)
140142
PyObject_ClearWeakRefs((PyObject *) m_obj);
@@ -157,28 +159,37 @@ mmap_close_method(mmap_object *self, PyObject *unused)
157159
again.
158160
TODO - should we check for errors in the close operations???
159161
*/
160-
if (self->data != NULL) {
161-
UnmapViewOfFile(self->data);
162-
self->data = NULL;
162+
HANDLE map_handle = self->map_handle;
163+
HANDLE file_handle = self->file_handle;
164+
char *data = self->data;
165+
self->map_handle = NULL;
166+
self->file_handle = INVALID_HANDLE_VALUE;
167+
self->data = NULL;
168+
Py_BEGIN_ALLOW_THREADS
169+
if (data != NULL) {
170+
UnmapViewOfFile(data);
163171
}
164-
if (self->map_handle != NULL) {
165-
CloseHandle(self->map_handle);
166-
self->map_handle = NULL;
172+
if (map_handle != NULL) {
173+
CloseHandle(map_handle);
167174
}
168-
if (self->file_handle != INVALID_HANDLE_VALUE) {
169-
CloseHandle(self->file_handle);
170-
self->file_handle = INVALID_HANDLE_VALUE;
175+
if (file_handle != INVALID_HANDLE_VALUE) {
176+
CloseHandle(file_handle);
171177
}
178+
Py_END_ALLOW_THREADS
172179
#endif /* MS_WINDOWS */
173180

174181
#ifdef UNIX
175-
if (0 <= self->fd)
176-
(void) close(self->fd);
182+
int fd = self->fd;
183+
char *data = self->data;
177184
self->fd = -1;
178-
if (self->data != NULL) {
179-
munmap(self->data, self->size);
180-
self->data = NULL;
185+
self->data = NULL;
186+
Py_BEGIN_ALLOW_THREADS
187+
if (0 <= fd)
188+
(void) close(fd);
189+
if (data != NULL) {
190+
munmap(data, self->size);
181191
}
192+
Py_END_ALLOW_THREADS
182193
#endif
183194

184195
Py_RETURN_NONE;

0 commit comments

Comments
 (0)