Skip to content

Commit d086792

Browse files
authored
gh-104372: Drop the GIL around the vfork() call. (#104782)
On Linux where the `subprocess` module can use the `vfork` syscall for faster spawning, prevent the parent process from blocking other threads by dropping the GIL while it waits for the vfork'ed child process `exec` outcome. This prevents spawning a binary from a slow filesystem from blocking the rest of the application. Fixes #104372.
1 parent 0888865 commit d086792

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

Doc/library/subprocess.rst

+8-5
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,13 @@ underlying :class:`Popen` interface can be used directly.
5757
and combine both streams into one, use ``stdout=PIPE`` and ``stderr=STDOUT``
5858
instead of *capture_output*.
5959

60-
The *timeout* argument is passed to :meth:`Popen.communicate`. If the timeout
61-
expires, the child process will be killed and waited for. The
62-
:exc:`TimeoutExpired` exception will be re-raised after the child process
63-
has terminated.
60+
A *timeout* may be specified in seconds, it is internally passed on to
61+
:meth:`Popen.communicate`. If the timeout expires, the child process will be
62+
killed and waited for. The :exc:`TimeoutExpired` exception will be
63+
re-raised after the child process has terminated. The initial process
64+
creation itself cannot be interrupted on many platform APIs so you are not
65+
guaranteed to see a timeout exception until at least after however long
66+
process creation takes.
6467

6568
The *input* argument is passed to :meth:`Popen.communicate` and thus to the
6669
subprocess's stdin. If used it must be a byte sequence, or a string if
@@ -734,7 +737,7 @@ arguments.
734737
code.
735738

736739
All of the functions and methods that accept a *timeout* parameter, such as
737-
:func:`call` and :meth:`Popen.communicate` will raise :exc:`TimeoutExpired` if
740+
:func:`run` and :meth:`Popen.communicate` will raise :exc:`TimeoutExpired` if
738741
the timeout expires before the process exits.
739742

740743
Exceptions defined in this module all inherit from :exc:`SubprocessError`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
On Linux where :mod:`subprocess` can use the ``vfork()`` syscall for faster
2+
spawning, prevent the parent process from blocking other threads by dropping
3+
the GIL while it waits for the vfork'ed child process ``exec()`` outcome.
4+
This prevents spawning a binary from a slow filesystem from blocking the
5+
rest of the application.

Modules/_posixsubprocess.c

+18-1
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ reset_signal_handlers(const sigset_t *child_sigmask)
559559
* required by POSIX but not supported natively on Linux. Another reason to
560560
* avoid this family of functions is that sharing an address space between
561561
* processes running with different privileges is inherently insecure.
562-
* See bpo-35823 for further discussion and references.
562+
* See https://bugs.python.org/issue35823 for discussion and references.
563563
*
564564
* In some C libraries, setrlimit() has the same thread list/signalling
565565
* behavior since resource limits were per-thread attributes before
@@ -798,14 +798,30 @@ do_fork_exec(char *const exec_array[],
798798
pid_t pid;
799799

800800
#ifdef VFORK_USABLE
801+
PyThreadState *vfork_tstate_save;
801802
if (child_sigmask) {
802803
/* These are checked by our caller; verify them in debug builds. */
803804
assert(uid == (uid_t)-1);
804805
assert(gid == (gid_t)-1);
805806
assert(extra_group_size < 0);
806807
assert(preexec_fn == Py_None);
807808

809+
/* Drop the GIL so that other threads can continue execution while this
810+
* thread in the parent remains blocked per vfork-semantics on the
811+
* child's exec syscall outcome. Exec does filesystem access which
812+
* can take an arbitrarily long time. This addresses GH-104372.
813+
*
814+
* The vfork'ed child still runs in our address space. Per POSIX it
815+
* must be limited to nothing but exec, but the Linux implementation
816+
* is a little more usable. See the child_exec() comment - The child
817+
* MUST NOT re-acquire the GIL.
818+
*/
819+
vfork_tstate_save = PyEval_SaveThread();
808820
pid = vfork();
821+
if (pid != 0) {
822+
// Not in the child process, reacquire the GIL.
823+
PyEval_RestoreThread(vfork_tstate_save);
824+
}
809825
if (pid == (pid_t)-1) {
810826
/* If vfork() fails, fall back to using fork(). When it isn't
811827
* allowed in a process by the kernel, vfork can return -1
@@ -819,6 +835,7 @@ do_fork_exec(char *const exec_array[],
819835
}
820836

821837
if (pid != 0) {
838+
// Parent process.
822839
return pid;
823840
}
824841

0 commit comments

Comments
 (0)