Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions frankenphp.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ static void *frankenphp_parent_death_watcher(void *arg) {
#endif

static void frankenphp_fork_child(void) {
// a fork of a fork (e.g. a daemonizer) must outlive its short-lived parent
if (is_forked_child) {
return;
}
frankenphp_mark_fork_child();
#if defined(__linux__)
// if the parent process dies between fork() and this prctl()
Expand Down
18 changes: 18 additions & 0 deletions frankenphp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,24 @@ func testPhpInfoForkChild(t *testing.T, opts *testOptions) {
}, opts)
}

func TestDoubleForkGrandchild_module(t *testing.T) { testDoubleForkGrandchild(t, nil) }
func TestDoubleForkGrandchild_worker(t *testing.T) {
testDoubleForkGrandchild(t, &testOptions{workerScript: "double-fork.php"})
}
func testDoubleForkGrandchild(t *testing.T, opts *testOptions) {
if opts == nil {
opts = &testOptions{}
}
opts.nbParallelRequests = 1
runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, _ int) {
body, _ := testGet("http://example.com/double-fork.php", handler, t)
if body == "pcntl-unavailable" {
t.Skip("pcntl/posix not fully loaded")
}
require.Equal(t, "grandchild-alive", body)
}, opts)
}

func TestPersistentObject_module(t *testing.T) { testPersistentObject(t, nil) }
func TestPersistentObject_worker(t *testing.T) {
testPersistentObject(t, &testOptions{workerScript: "persistent-object.php"})
Expand Down
43 changes: 43 additions & 0 deletions testdata/double-fork.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

require_once __DIR__.'/_executor.php';

return function () {
foreach (['pcntl_fork', 'pcntl_exec', 'pcntl_waitpid', 'posix_kill', 'posix_getpid'] as $function) {
if (!function_exists($function)) {
echo "pcntl-unavailable";
return;
}
}

$marker = tempnam(sys_get_temp_dir(), 'frankenphp-double-fork-');
unlink($marker);

$pid = pcntl_fork();
if ($pid === -1) {
echo "fork-failed";
return;
}
if ($pid === 0) {
if (pcntl_fork() === 0) {
pcntl_exec('/bin/sh', ['-c', 'sleep 0.2; touch "$1"', 'sh', $marker]);
}
// exit without PHP or Go teardown, like a daemonizer's intermediate process
posix_kill(posix_getpid(), SIGKILL);
}

pcntl_waitpid($pid, $status);

$deadline = microtime(true) + 3;
do {
clearstatcache();
if (file_exists($marker)) {
unlink($marker);
echo "grandchild-alive";
return;
}
usleep(10000);
} while (microtime(true) < $deadline);

echo "grandchild-killed";
};
Loading