From 3e6a0430f64d3fef2378f169bda911da425b4c5e Mon Sep 17 00:00:00 2001 From: Yury Selivanov Date: Thu, 24 Oct 2019 22:51:19 -0400 Subject: [PATCH] Use higher-level wrappers for the __forkHandler global --- uvloop/handles/process.pyx | 4 ++-- uvloop/includes/fork_handler.h | 14 +++++++++++++- uvloop/includes/system.pxd | 8 ++++++++ uvloop/loop.pyx | 7 +------ 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/uvloop/handles/process.pyx b/uvloop/handles/process.pyx index 4f85968..bbeaff9 100644 --- a/uvloop/handles/process.pyx +++ b/uvloop/handles/process.pyx @@ -77,7 +77,7 @@ cdef class UVProcess(UVHandle): loop.active_process_handler = self __forking = 1 __forking_loop = loop - __forkHandler = &__get_fork_handler + system.setForkHandler(&__get_fork_handler) PyOS_BeforeFork() @@ -87,7 +87,7 @@ cdef class UVProcess(UVHandle): __forking = 0 __forking_loop = None - __forkHandler = NULL + system.resetForkHandler() loop.active_process_handler = None PyOS_AfterFork_Parent() diff --git a/uvloop/includes/fork_handler.h b/uvloop/includes/fork_handler.h index 828d10f..19a016d 100644 --- a/uvloop/includes/fork_handler.h +++ b/uvloop/includes/fork_handler.h @@ -8,8 +8,20 @@ OnForkHandler __forkHandler = NULL; Note: Fork handler needs to be in C (not cython) otherwise it would require GIL to be present, but some forks can exec non-python processes. */ -void handleAtFork() { +void handleAtFork(void) { if (__forkHandler != NULL) { __forkHandler(); } } + + +void setForkHandler(OnForkHandler handler) +{ + __forkHandler = handler; +} + + +void resetForkHandler(void) +{ + __forkHandler = NULL; +} diff --git a/uvloop/includes/system.pxd b/uvloop/includes/system.pxd index f2906ed..63b9efa 100644 --- a/uvloop/includes/system.pxd +++ b/uvloop/includes/system.pxd @@ -81,3 +81,11 @@ cdef extern from "includes/compat.h" nogil: int EPOLL_CTL_DEL int epoll_ctl(int epfd, int op, int fd, epoll_event *event) object MakeUnixSockPyAddr(sockaddr_un *addr) + + +cdef extern from "includes/fork_handler.h": + + ctypedef void (*OnForkHandler)() + void handleAtFork() + void setForkHandler(OnForkHandler handler) + void resetForkHandler() diff --git a/uvloop/loop.pyx b/uvloop/loop.pyx index 51e6fb2..fe79165 100644 --- a/uvloop/loop.pyx +++ b/uvloop/loop.pyx @@ -3178,11 +3178,6 @@ cdef vint __atfork_installed = 0 cdef vint __forking = 0 cdef Loop __forking_loop = None -cdef extern from "includes/fork_handler.h": - - ctypedef void (*OnForkHandler)() - cdef OnForkHandler __forkHandler - void handleAtFork() cdef void __get_fork_handler() nogil: global __forking @@ -3201,7 +3196,7 @@ cdef __install_atfork(): cdef int err - err = system.pthread_atfork(NULL, NULL, &handleAtFork) + err = system.pthread_atfork(NULL, NULL, &system.handleAtFork) if err: __atfork_installed = 0 raise convert_error(-err)