File tree 5 files changed +33
-15
lines changed
5 files changed +33
-15
lines changed Original file line number Diff line number Diff line change @@ -92,12 +92,12 @@ def spawn_process():
92
92
ctypes .c_size_t , ctypes .c_void_p )
93
93
fread .restype = ctypes .c_size_t
94
94
95
- for iteration in range (10000 ):
95
+ for iteration in range (1000 ):
96
96
t = Thread (target = run_echo ,
97
97
args = (popen , fread , pclose ),
98
98
daemon = True )
99
99
t .start ()
100
- t .join (timeout = 20 .0 )
100
+ t .join (timeout = 10 .0 )
101
101
if t .is_alive ():
102
102
raise Exception ('process freeze detected at {}'
103
103
.format (iteration ))
Original file line number Diff line number Diff line change @@ -28,6 +28,7 @@ cdef class UVProcess(UVHandle):
28
28
29
29
global __forking
30
30
global __forking_loop
31
+ global __forkHandler
31
32
32
33
cdef int err
33
34
@@ -76,6 +77,7 @@ cdef class UVProcess(UVHandle):
76
77
loop.active_process_handler = self
77
78
__forking = 1
78
79
__forking_loop = loop
80
+ __forkHandler = < OnForkHandler> & __get_fork_handler
79
81
80
82
PyOS_BeforeFork()
81
83
@@ -85,6 +87,7 @@ cdef class UVProcess(UVHandle):
85
87
86
88
__forking = 0
87
89
__forking_loop = None
90
+ __forkHandler = NULL
88
91
loop.active_process_handler = None
89
92
90
93
PyOS_AfterFork_Parent()
Original file line number Diff line number Diff line change
1
+
2
+ typedef void (* OnForkHandler )();
3
+
4
+ OnForkHandler __forkHandler = NULL ;
5
+
6
+ /* Auxiliary function to call global fork handler if defined.
7
+
8
+ Note: Fork handler needs to be in C (not cython) otherwise it would require
9
+ GIL to be present, but some forks can exec non-python processes.
10
+ */
11
+ void handleAtFork () {
12
+ if (__forkHandler != NULL ) {
13
+ __forkHandler ();
14
+ }
15
+ }
Original file line number Diff line number Diff line change @@ -59,12 +59,12 @@ cdef extern from "unistd.h" nogil:
59
59
void _exit(int status)
60
60
61
61
62
- cdef extern from " pthread.h" nogil :
62
+ cdef extern from " pthread.h" :
63
63
64
64
int pthread_atfork(
65
- void (* prepare)() nogil ,
66
- void (* parent)() nogil ,
67
- void (* child)() nogil )
65
+ void (* prepare)(),
66
+ void (* parent)(),
67
+ void (* child)())
68
68
69
69
70
70
cdef extern from " includes/compat.h" nogil:
Original file line number Diff line number Diff line change @@ -3117,21 +3117,21 @@ cdef vint __atfork_installed = 0
3117
3117
cdef vint __forking = 0
3118
3118
cdef Loop __forking_loop = None
3119
3119
3120
+ cdef extern from " includes/fork_handler.h" :
3120
3121
3121
- cdef void __atfork_child() nogil:
3122
- # See CPython/posixmodule.c for details
3123
- global __forking
3122
+ ctypedef void ( * OnForkHandler)()
3123
+ cdef OnForkHandler __forkHandler
3124
+ void handleAtFork()
3124
3125
3125
- if __forking:
3126
- __run_fork_handler()
3126
+ cdef void __get_fork_handler() nogil:
3127
+ global __forking
3128
+ global __forking_loop
3127
3129
3128
- cdef void __run_fork_handler() nogil:
3129
3130
with gil:
3130
- if (__forking_loop is not None and
3131
+ if (__forking and __forking_loop is not None and
3131
3132
__forking_loop.active_process_handler is not None ):
3132
3133
__forking_loop.active_process_handler._after_fork()
3133
3134
3134
-
3135
3135
cdef __install_atfork():
3136
3136
global __atfork_installed
3137
3137
if __atfork_installed:
@@ -3140,7 +3140,7 @@ cdef __install_atfork():
3140
3140
3141
3141
cdef int err
3142
3142
3143
- err = system.pthread_atfork(NULL , NULL , & __atfork_child )
3143
+ err = system.pthread_atfork(NULL , NULL , & handleAtFork )
3144
3144
if err:
3145
3145
__atfork_installed = 0
3146
3146
raise convert_error(- err)
You can’t perform that action at this time.
0 commit comments