Skip to content

Commit 75e7c32

Browse files
committed
Use new PyOS_BeforeFork and PyOS_AfterFork_* 3.7 APIs when available
1 parent 0a31082 commit 75e7c32

File tree

5 files changed

+34
-18
lines changed

5 files changed

+34
-18
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
@@ -50,3 +50,26 @@ int PyContext_Exit(PyContext *ctx) {
5050
return -1;
5151
}
5252
#endif
53+
54+
55+
#if PY_VERSION_HEX < 0x03070000
56+
57+
void PyOS_BeforeFork(void)
58+
{
59+
_PyImport_AcquireLock();
60+
}
61+
62+
void PyOS_AfterFork_Parent(void)
63+
{
64+
if (_PyImport_ReleaseLock() <= 0) {
65+
Py_FatalError("failed releasing import lock after fork");
66+
}
67+
}
68+
69+
70+
void PyOS_AfterFork_Child(void)
71+
{
72+
PyOS_AfterFork();
73+
}
74+
75+
#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

@@ -20,3 +17,7 @@ cdef extern from "includes/compat.h":
2017
PyContext* PyContext_CopyCurrent() except NULL
2118
int PyContext_Enter(PyContext *) except -1
2219
int PyContext_Exit(PyContext *) except -1
20+
21+
void PyOS_BeforeFork()
22+
void PyOS_AfterFork_Parent()
23+
void PyOS_AfterFork_Child()

uvloop/loop.pyx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@ 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
PyContext, \
1916
PyContext_CopyCurrent, \
2017
PyContext_Enter, \
21-
PyContext_Exit
18+
PyContext_Exit, \
19+
PyOS_AfterFork_Parent, PyOS_AfterFork_Child, \
20+
PyOS_BeforeFork
2221

2322
from libc.stdint cimport uint64_t
2423
from libc.string cimport memset, strerror, memcpy

vendor/libuv

Submodule libuv updated 122 files

0 commit comments

Comments
 (0)