pmmp / ext-pmmpthread

Fork of https://github.com/krakjoe/pthreads with a revamped API and PHP 8.1+ support
Other
81 stars 17 forks source link

Static modification causes ``zend_mm_heap corrupted`` #104

Closed ShockedPlot7560 closed 1 year ago

ShockedPlot7560 commented 1 year ago

Environment

Summary

After much research, I found the source of a problem that happens to me by reproducing it at will.

It seems that there is a strange behavior with the modification of a static variable, through a callable in another thread than the one that created this callable.

The example code will speak better than my explanations which can be shaky

Reproducing Code

A class :

class AsyncEvilTask extends AsyncTask {
    private $onRun;
    private $onCompletion;

    public function __construct(callable $onRun, callable $onCompletion) {
        $this->onRun = $onRun;
        $this->onCompletion = $onCompletion;
    }

    public function onRun() : void {
        $callable = $this->onRun;
        $callable($this);
    }

    public function onCompletion(): void {
        $callable = $this->onCompletion;
        $callable($this);
    }
}

In a PocketMine-MP plugin class:

class Main extends PluginBase {
     public static $test = [
        "Angel"
     ];

    public function onEnable(): void{
        $this->getServer()->getAsyncPool()->submitTask(new AsyncEvilTask(
            function() {
                self::$test[0] = "Evil";
            }, function() {
                var_dump(self::$test);
            }
        ));
    }
}

Expected Output

A var_dump of an array containing an element: a string equal to Angel.

Actual Output

zend_mm_heap corrupted error with no more explanation

dktapps commented 1 year ago

This is caused by the same issue reported here: https://github.com/pmmp/pthreads/issues/59

This issue was fixed in 5.1.4.