Skip to content

Commit 149e14e

Browse files
committed
Use new PyOS_BeforeFork and PyOS_AfterFork_* 3.7 APIs when available
1 parent cfe6527 commit 149e14e

File tree

4 files changed

+33
-17
lines changed

4 files changed

+33
-17
lines changed

uvloop/handles/process.pyx

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ cdef class UVProcess(UVHandle):
7777
__forking = 1
7878
__forking_loop = loop
7979

80-
_PyImport_AcquireLock()
80+
PyOS_BeforeFork()
8181

8282
err = uv.uv_spawn(loop.uvloop,
8383
<uv.uv_process_t*>self._handle,
@@ -87,14 +87,7 @@ cdef class UVProcess(UVHandle):
8787
__forking_loop = None
8888
loop.active_process_handler = None
8989

90-
if _PyImport_ReleaseLock() < 0:
91-
# See CPython/posixmodule.c for details
92-
self._close_process_handle()
93-
if err < 0:
94-
self._abort_init()
95-
else:
96-
self._close()
97-
raise RuntimeError('not holding the import lock')
90+
PyOS_AfterFork_Parent()
9891

9992
if err < 0:
10093
self._close_process_handle()
@@ -176,7 +169,7 @@ cdef class UVProcess(UVHandle):
176169
if self._restore_signals:
177170
_Py_RestoreSignals()
178171

179-
PyOS_AfterFork()
172+
PyOS_AfterFork_Child()
180173

181174
err = uv.uv_loop_fork(self._loop.uvloop)
182175
if err < 0:

uvloop/includes/compat.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,26 @@ int Context_Exit(PyObject *ctx) {
7979
}
8080

8181
#endif
82+
83+
84+
#if PY_VERSION_HEX < 0x03070000
85+
86+
void PyOS_BeforeFork(void)
87+
{
88+
_PyImport_AcquireLock();
89+
}
90+
91+
void PyOS_AfterFork_Parent(void)
92+
{
93+
if (_PyImport_ReleaseLock() <= 0) {
94+
Py_FatalError("failed releasing import lock after fork");
95+
}
96+
}
97+
98+
99+
void PyOS_AfterFork_Child(void)
100+
{
101+
PyOS_AfterFork();
102+
}
103+
104+
#endif

uvloop/includes/python.pxd

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ cdef extern from "Python.h":
99
object PyUnicode_EncodeFSDefault(object)
1010
void PyErr_SetInterrupt() nogil
1111

12-
void PyOS_AfterFork()
13-
void _PyImport_AcquireLock()
14-
int _PyImport_ReleaseLock()
1512
void _Py_RestoreSignals()
1613

1714
object PyMemoryView_FromMemory(char *mem, ssize_t size, int flags)
@@ -26,3 +23,7 @@ cdef extern from "includes/compat.h":
2623
object Context_CopyCurrent()
2724
int Context_Enter(object) except -1
2825
int Context_Exit(object) except -1
26+
27+
void PyOS_BeforeFork()
28+
void PyOS_AfterFork_Parent()
29+
void PyOS_AfterFork_Child()

uvloop/loop.pyx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@ from .includes.python cimport PY_VERSION_HEX, \
1111
PyMem_RawCalloc, PyMem_RawRealloc, \
1212
PyUnicode_EncodeFSDefault, \
1313
PyErr_SetInterrupt, \
14-
PyOS_AfterFork, \
15-
_PyImport_AcquireLock, \
16-
_PyImport_ReleaseLock, \
1714
_Py_RestoreSignals, \
1815
Context_CopyCurrent, \
1916
Context_Enter, \
2017
Context_Exit, \
2118
PyMemoryView_FromMemory, PyBUF_WRITE, \
22-
PyMemoryView_FromObject, PyMemoryView_Check
19+
PyMemoryView_FromObject, PyMemoryView_Check, \
20+
PyOS_AfterFork_Parent, PyOS_AfterFork_Child, \
21+
PyOS_BeforeFork
2322
from .includes.flowcontrol cimport add_flowcontrol_defaults
2423

2524
from libc.stdint cimport uint64_t

0 commit comments

Comments
 (0)