-
-
Notifications
You must be signed in to change notification settings - Fork 36.1k
gh-89977: Avoid releasing the GIL in nonblocking socket operations #29579
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -357,15 +357,16 @@ | |
| } | ||
|
|
||
| do { | ||
| Py_BEGIN_ALLOW_THREADS | ||
| int timeout_is_zero = (tvp != NULL && tvp->tv_sec == 0 && tvp->tv_usec == 0); | ||
| _Py_BEGIN_ALLOW_THREADS_COND(!timeout_is_zero) | ||
| errno = 0; | ||
| n = select( | ||
| max, | ||
| imax ? &ifdset : NULL, | ||
| omax ? &ofdset : NULL, | ||
| emax ? &efdset : NULL, | ||
| tvp); | ||
| Py_END_ALLOW_THREADS | ||
| _Py_END_ALLOW_THREADS_COND | ||
|
|
||
| if (errno != EINTR) | ||
| break; | ||
|
|
@@ -698,14 +699,14 @@ | |
| /* call poll() */ | ||
| async_err = 0; | ||
| do { | ||
| Py_BEGIN_ALLOW_THREADS | ||
| _Py_BEGIN_ALLOW_THREADS_COND(ms != 0) | ||
|
Check failure on line 702 in Modules/selectmodule.c
|
||
| errno = 0; | ||
| #ifdef HAVE_PPOLL | ||
| poll_result = ppoll(self->ufds, self->ufd_len, ts_p, NULL); | ||
| #else | ||
| poll_result = poll(self->ufds, self->ufd_len, (int)ms); | ||
| #endif | ||
| Py_END_ALLOW_THREADS | ||
| _Py_END_ALLOW_THREADS_COND | ||
|
|
||
| if (errno != EINTR) | ||
| break; | ||
|
|
@@ -1034,10 +1035,10 @@ | |
|
|
||
| do { | ||
| /* call devpoll() */ | ||
| Py_BEGIN_ALLOW_THREADS | ||
| _Py_BEGIN_ALLOW_THREADS_COND(ms != 0) | ||
| errno = 0; | ||
| poll_result = ioctl(self->fd_devpoll, DP_POLL, &dvp); | ||
| Py_END_ALLOW_THREADS | ||
| _Py_END_ALLOW_THREADS_COND | ||
|
|
||
| if (errno != EINTR) | ||
| break; | ||
|
|
@@ -1513,17 +1514,13 @@ | |
| case EPOLL_CTL_MOD: | ||
| ev.events = events; | ||
| ev.data.fd = fd; | ||
| Py_BEGIN_ALLOW_THREADS | ||
| result = epoll_ctl(epfd, op, fd, &ev); | ||
| Py_END_ALLOW_THREADS | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I can tell |
||
| break; | ||
| case EPOLL_CTL_DEL: | ||
| /* In kernel versions before 2.6.9, the EPOLL_CTL_DEL | ||
| * operation required a non-NULL pointer in event, even | ||
| * though this argument is ignored. */ | ||
| Py_BEGIN_ALLOW_THREADS | ||
| result = epoll_ctl(epfd, op, fd, &ev); | ||
| Py_END_ALLOW_THREADS | ||
| break; | ||
| default: | ||
| result = -1; | ||
|
|
@@ -1673,10 +1670,10 @@ | |
| } | ||
|
|
||
| do { | ||
| Py_BEGIN_ALLOW_THREADS | ||
| _Py_BEGIN_ALLOW_THREADS_COND(ms != 0) | ||
| errno = 0; | ||
| nfds = epoll_wait(self->epfd, evs, maxevents, (int)ms); | ||
| Py_END_ALLOW_THREADS | ||
| _Py_END_ALLOW_THREADS_COND | ||
|
|
||
| if (errno != EINTR) | ||
| break; | ||
|
|
@@ -2429,11 +2426,14 @@ | |
| } | ||
|
|
||
| do { | ||
| Py_BEGIN_ALLOW_THREADS | ||
| int timeout_is_zero = (ptimeoutspec != NULL && | ||
| ptimeoutspec->tv_sec == 0 && | ||
| ptimeoutspec->tv_nsec == 0); | ||
| _Py_BEGIN_ALLOW_THREADS_COND(!timeout_is_zero) | ||
| errno = 0; | ||
| gotevents = kevent(self->kqfd, chl, nchanges, | ||
| evl, maxevents, ptimeoutspec); | ||
| Py_END_ALLOW_THREADS | ||
| _Py_END_ALLOW_THREADS_COND | ||
|
|
||
| if (errno != EINTR) | ||
| break; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -976,7 +976,7 @@ internal_select(PySocketSockObject *s, int writing, PyTime_t interval, | |
| When the function is retried, recompute the timeout using a monotonic clock. | ||
|
|
||
| sock_call_ex() must be called with the GIL held. The socket function is | ||
| called with the GIL released. */ | ||
| called with the GIL released if the socket is blocking. */ | ||
| static int | ||
| sock_call_ex(PySocketSockObject *s, | ||
| int writing, | ||
|
|
@@ -1059,9 +1059,9 @@ sock_call_ex(PySocketSockObject *s, | |
| /* inner loop to retry sock_func() when sock_func() is interrupted | ||
| by a signal */ | ||
| while (1) { | ||
| Py_BEGIN_ALLOW_THREADS | ||
| _Py_BEGIN_ALLOW_THREADS_COND(s->sock_timeout) | ||
| res = sock_func(s, data); | ||
| Py_END_ALLOW_THREADS | ||
| _Py_END_ALLOW_THREADS_COND | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are likely other socket operations where releasing the GIL could be avoided, but this gets the bulk of those done frequently in a loop ( |
||
|
|
||
| if (res) { | ||
| /* sock_func() succeeded */ | ||
|
|
@@ -3655,9 +3655,9 @@ _socket_socket_close_impl(PySocketSockObject *s) | |
| http://lwn.net/Articles/576478/ and | ||
| http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html | ||
| for more details. */ | ||
| Py_BEGIN_ALLOW_THREADS | ||
| _Py_BEGIN_ALLOW_THREADS_COND(s->sock_timeout) | ||
| res = SOCKETCLOSE(fd); | ||
| Py_END_ALLOW_THREADS | ||
| _Py_END_ALLOW_THREADS_COND | ||
|
Comment on lines
-3658
to
+3660
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't change here. |
||
| /* bpo-30319: The peer can already have closed the connection. | ||
| Python ignores ECONNRESET on close(). */ | ||
| if (res < 0 && errno != ECONNRESET) { | ||
|
|
||
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 added some helper macros here, but am not sure if that's best practice. Happy to change this if others have a better suggestion.