fork icon indicating copy to clipboard operation
fork copied to clipboard

Bug 1.2.2 doesn't finish

Open Globerada opened this issue 1 year ago • 4 comments

PHP 8.2.18 (cli)

Sorry for not giving more details, but my knowledge about threads is too low.

Version 1.2.2 never reach die('done');

use Spatie\Fork\Fork;

$results = Fork::new()
    ->run(
        function ()  {
  
            return 'result from task 1';
        },
        function ()  {

            return 'result from task 2';
        },
        function ()  {

            return 'result from task 3';
        },
    );
    
die('done'); 

The bug is not present in 1.2.1

https://github.com/spatie/fork/compare/1.2.1...1.2.2

Maybe is needed $this->exit() and also exit()?

Globerada avatar Apr 23 '24 16:04 Globerada

Works fine for me with 1.2.2. On PHP 8.3.6 but that shouldn't really matter.

redburn avatar May 05 '24 11:05 redburn

I think a timeout / max execution time per fork should be preferable too, to timeout to the main thread again if something is hanging in the forked process. This can be done through a signal (alarm)

remco-pc avatar May 14 '24 09:05 remco-pc

Probably better to use the more fleshed-out spatie/async if you need additional features like timeouts.

redburn avatar May 14 '24 09:05 redburn

Thanks for that one, will probably implement it in my framework. I have json #parallel property to execute in parallel and with timeout its way more production ready instead of 30 sec

remco-pc avatar May 14 '24 09:05 remco-pc

1.2.2 is also broken for me on latest PHP 8.3. Never returns.

taylorotwell avatar Jul 16 '24 21:07 taylorotwell

https://github.com/spatie/fork/blob/1.2.2/src/Fork.php#L132

This line terminates the entire web request. Any reason it's not $this->exit()?

https://github.com/spatie/fork/compare/1.2.1..1.2.2

taylorotwell avatar Jul 16 '24 21:07 taylorotwell

Correct me if I'm wrong, but I believe that particular exit() terminates the worker (from within the worker process) after it's finished executing its task. $this->exit() is a function of and for the parent process.

You're referring to a "web request". Be aware that Fork only works in a CLI context.

redburn avatar Jul 16 '24 21:07 redburn

I think @redburn might be right about the problem here.

I can't reproduce this problem in a CLI command. When you try the code example in a web request it does not work, however when you revert back to 1.2.1 it does reach the die statement. However the results are not reliable as its not an intended usecase: just refreshing the page gives me different results each time.

timvandijck avatar Jul 17 '24 08:07 timvandijck

Ah, disregard my issue then. 😅

taylorotwell avatar Jul 17 '24 12:07 taylorotwell