Skip to content

Commit

Permalink
Use new PyOS_BeforeFork and PyOS_AfterFork_* 3.7 APIs when available
Browse files Browse the repository at this point in the history
  • Loading branch information
1st1 committed Oct 30, 2018
1 parent 0a31082 commit 75e7c32
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 18 deletions.
13 changes: 3 additions & 10 deletions uvloop/handles/process.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ cdef class UVProcess(UVHandle):
__forking = 1
__forking_loop = loop

_PyImport_AcquireLock()
PyOS_BeforeFork()

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

if _PyImport_ReleaseLock() < 0:
# See CPython/posixmodule.c for details
self._close_process_handle()
if err < 0:
self._abort_init()
else:
self._close()
raise RuntimeError('not holding the import lock')
PyOS_AfterFork_Parent()

if err < 0:
self._close_process_handle()
Expand Down Expand Up @@ -176,7 +169,7 @@ cdef class UVProcess(UVHandle):
if self._restore_signals:
_Py_RestoreSignals()

PyOS_AfterFork()
PyOS_AfterFork_Child()

err = uv.uv_loop_fork(self._loop.uvloop)
if err < 0:
Expand Down
23 changes: 23 additions & 0 deletions uvloop/includes/compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,26 @@ int PyContext_Exit(PyContext *ctx) {
return -1;
}
#endif


#if PY_VERSION_HEX < 0x03070000

void PyOS_BeforeFork(void)
{
_PyImport_AcquireLock();
}

void PyOS_AfterFork_Parent(void)
{
if (_PyImport_ReleaseLock() <= 0) {
Py_FatalError("failed releasing import lock after fork");
}
}


void PyOS_AfterFork_Child(void)
{
PyOS_AfterFork();
}

#endif
7 changes: 4 additions & 3 deletions uvloop/includes/python.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ cdef extern from "Python.h":
object PyUnicode_EncodeFSDefault(object)
void PyErr_SetInterrupt() nogil

void PyOS_AfterFork()
void _PyImport_AcquireLock()
int _PyImport_ReleaseLock()
void _Py_RestoreSignals()


Expand All @@ -20,3 +17,7 @@ cdef extern from "includes/compat.h":
PyContext* PyContext_CopyCurrent() except NULL
int PyContext_Enter(PyContext *) except -1
int PyContext_Exit(PyContext *) except -1

void PyOS_BeforeFork()
void PyOS_AfterFork_Parent()
void PyOS_AfterFork_Child()
7 changes: 3 additions & 4 deletions uvloop/loop.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ from .includes.python cimport PY_VERSION_HEX, \
PyMem_RawCalloc, PyMem_RawRealloc, \
PyUnicode_EncodeFSDefault, \
PyErr_SetInterrupt, \
PyOS_AfterFork, \
_PyImport_AcquireLock, \
_PyImport_ReleaseLock, \
_Py_RestoreSignals, \
PyContext, \
PyContext_CopyCurrent, \
PyContext_Enter, \
PyContext_Exit
PyContext_Exit, \
PyOS_AfterFork_Parent, PyOS_AfterFork_Child, \
PyOS_BeforeFork

from libc.stdint cimport uint64_t
from libc.string cimport memset, strerror, memcpy
Expand Down
2 changes: 1 addition & 1 deletion vendor/libuv
Submodule libuv updated 122 files

0 comments on commit 75e7c32

Please sign in to comment.