kitech / php-go

Write PHP extension using go/golang. Zend API wrapper for go/golang.
882 stars 108 forks source link

Forking PHP while module is loaded causes deadlocks #47

Open SpencerMalone opened 5 years ago

SpencerMalone commented 5 years ago

I don't think (based on issues found in the golang repository) that we will see golang support for forking in shared c libraries anytime soon. If you fork and interact with golang objects and functions post-fork, you will eventually deadlock.

For example, in the current example.php:

Take...

test_funcs();
test_classes();
test_inis();

And rewrite it work a fork, based on the example @ https://www.php.net/manual/en/function.pcntl-fork.php

test_funcs();
$pid = pcntl_fork();
if ($pid == -1) {
     die('could not fork');
} else if ($pid) {
     // we are the parent
     pcntl_wait($status); //Protect against Zombie children
} else {
    $i=0;
    while(true) {
    test_classes();
    echo($i++);
    }
}

test_inis();

And you will see fairly quickly, it will deadlock. Here is a GIF: Screen Capture on 2019-05-23 at 22-04-29

Knowing that this probably won't fixable anytime soon, and from what I can tell, PHP doesn't offer support for unloading and reloading extensions during forking, maybe we should put up a notice about it in the README? Unless anybody know of a theoretical way around this, which I would be very grateful for.

xywf221 commented 5 years ago

The project seems to have been no longer maintained by the author.

ClosetGeek-Git commented 5 years ago

You can load extensions at runtime using dl() within child, just remove extension from php.ini

Note that dl() will not be available in ZTS builds (if php-go gets around to it for php7) and PHP-FPM doesn't allow dl().