Skip to content

Commit 6b3408c

Browse files
committed
fix dead forked pthread_fork children
1 parent efbbfa3 commit 6b3408c

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

frankenphp.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ __thread uintptr_t thread_index;
8787
__thread bool is_worker_thread = false;
8888
__thread HashTable *sandboxed_env = NULL;
8989

90+
#ifndef PHP_WIN32
91+
static bool is_forked_child = false;
92+
static void frankenphp_fork_child(void) { is_forked_child = true; }
93+
#endif
94+
9095
void frankenphp_update_local_thread_context(bool is_worker) {
9196
is_worker_thread = is_worker;
9297

@@ -601,6 +606,12 @@ PHP_FUNCTION(frankenphp_handle_request) {
601606
}
602607
}
603608

609+
#ifndef PHP_WIN32
610+
if (is_forked_child) {
611+
_exit(0);
612+
}
613+
#endif
614+
604615
frankenphp_worker_request_shutdown();
605616
go_frankenphp_finish_worker_request(thread_index, callback_ret);
606617
if (result.r1 != NULL) {
@@ -699,6 +710,9 @@ PHP_FUNCTION(frankenphp_log) {
699710

700711
PHP_MINIT_FUNCTION(frankenphp) {
701712
register_frankenphp_symbols(module_number);
713+
#ifndef PHP_WIN32
714+
pthread_atfork(NULL, NULL, frankenphp_fork_child);
715+
#endif
702716

703717
zend_function *func;
704718

@@ -1086,6 +1100,11 @@ static void *php_thread(void *arg) {
10861100

10871101
/* Execute the PHP script, potential bailout to zend_catch */
10881102
php_execute_script(&file_handle);
1103+
#ifndef PHP_WIN32
1104+
if (is_forked_child) {
1105+
_exit(EG(exit_status));
1106+
}
1107+
#endif
10891108
zend_destroy_file_handle(&file_handle);
10901109
reset_sandboxed_environment();
10911110

@@ -1102,6 +1121,12 @@ static void *php_thread(void *arg) {
11021121
}
11031122
}
11041123
zend_catch {
1124+
#ifndef PHP_WIN32
1125+
if (is_forked_child) {
1126+
_exit(EG(exit_status));
1127+
}
1128+
#endif
1129+
11051130
/* Critical failure from php_execute_script or php_request_shutdown, mark
11061131
* the thread as unhealthy */
11071132
thread_is_healthy = false;

0 commit comments

Comments
 (0)