-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
bpo-36139: release GIL around munmap() #12073
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Hello, and thanks for your contribution! I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA). Unfortunately we couldn't find an account corresponding to your GitHub username on bugs.python.org (b.p.o) to verify you have signed the CLA (this might be simply due to a missing "GitHub Name" entry in your b.p.o account settings). This is necessary for legal reasons before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue. You can check yourself to see if the CLA has been received. Thanks again for your contribution, we look forward to reviewing it! |
Should we also be doing this around the |
It would make sense but I wasn't sure if we should do it separately or in one block, and if it should go inside the |
I think it makes sense to stick them both in the same block. We' certainly release the GIL when closing files. |
@benjaminp I extended the GIL release to include the fd close, both on dealloc and |
Modules/mmapmodule.c
Outdated
@@ -180,6 +183,7 @@ mmap_close_method(mmap_object *self, PyObject *unused) | |||
self->data = NULL; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it's safe to not be holding the GIL here. Someone could be trying to read or write the mmap object while calling close
.
@benjaminp may I ask for another look? If this is still not ok for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good modulo minor formatting issues.
Please also add a NEWS note.
Modules/mmapmodule.c
Outdated
self->data = NULL; | ||
HANDLE map_handle = self->map_handle; | ||
HANDLE file_handle = self->file_handle; | ||
char * data = self->data; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
char *data
Modules/mmapmodule.c
Outdated
if (0 <= self->fd) | ||
(void) close(self->fd); | ||
int fd = self->fd; | ||
char * data = self->data; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
char *data
|
@@ -117,6 +117,7 @@ typedef struct { | |||
static void | |||
mmap_object_dealloc(mmap_object *m_obj) | |||
{ | |||
Py_BEGIN_ALLOW_THREADS | |||
#ifdef MS_WINDOWS | |||
if (m_obj->data != NULL) | |||
UnmapViewOfFile (m_obj->data); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PyMem_Free(m_obj->tagname) is called below without holding the GIL: that's illegal. Other move the call to free when the GIL is hold again, or use PyMem_RawFree().
"Warning: The GIL must be held when using these functions. "
https://docs.python.org/dev/c-api/memory.html#memory-interface
Note: You can use PYTHONMALLOC=debug or -X dev to reproduce the issue on a Python compiled in release mode.
|
|
This seems pretty straightforward. The issue mentions other calls in mmapmodule that we could release the GIL on, but those are in methods where we'd need to be careful to ensure that something sensible happens if those are called concurrently. In prior art, note that python#12073 released the GIL for munmap. In a toy benchmark, I see the speed up you'd expect from doing this.
This seems pretty straightforward. The issue mentions other calls in mmapmodule that we could release the GIL on, but those are in methods where we'd need to be careful to ensure that something sensible happens if those are called concurrently. In prior art, note that #12073 released the GIL for munmap. In a toy benchmark, I see the speedup you'd expect from doing this. Automerge-Triggered-By: GH:gvanrossum
https://bugs.python.org/issue36139