在使用swoole_process时在子进程使用复杂的正则子进程会crash掉,主进程没事。

在使用swoole_process时在子进程使用复杂的正则子进程会crash掉,主进程没事。

相关代码

<?php
class TestProcess{
    public $mpid=0;
    public $max_precess=8;
    public $task  = array();
    public $works = [];
    public $swoole_table = NULL;
    public $process_objs = [];
    
    function handle($index, $worker){
        while (true){
            $msg = "test_preg_replace_slave";
            $msg = preg_replace('/[ \r\n\t]*\n[ \r\n\t]*/', '', $msg);//使用会crash
            //$msg = preg_replace('/[ \r\n\t]*\n/', '', $msg);//正常
            var_dump($msg);
        }
    }

    public function __construct(){
        try {
            $this->swoole_table = new swoole_table(1024);
            $this->swoole_table->column('index', swoole_table::TYPE_INT);
            $this->swoole_table->create();
            $this->mpid = posix_getpid();
        }catch (\Exception $e){
            Logger::error($e);
        }
    }

    public function run(){
        try{
            for ($i=0; $i < $this->max_precess; $i++) {
                $this->createProcess();
            }
            $this->runProcess();
            $this->processWait();
        }  catch (Exception $e){
            Logger::error($e);
            exit();
        }
    }

    private function runProcess(){
        foreach($this->process_objs as $index => $process){
            $this->works[$index]=$process->start();
        }
    }

    public function createProcess($index=null){
        $msg = "test_preg_replace_master";
        $msg = preg_replace('/[ \r\n\t]*\n[ \r\n\t]*/', '', $msg);
        var_dump($msg);
        if(is_null($index)){
            $index=$this->swoole_table->get('index');
            if($index === false){
                $index = 0;
            }else{
                $index = $index['index']+1;
            }
        }
        $this->swoole_table->set('index',array('index'=>$index));
        $process = new swoole_process(function(swoole_process $worker)use($index){
            call_user_func_array(array($this, 'handle'),array($index, $worker));
        }, false, false);
        $this->process_objs[$index] = $process;
    }

    public function processWait(){
        $ret = array();
        while($ret = swoole_process::wait()) {
            $pid = $ret['pid'];
        }
    }
    
}
try{
    $obj = new TestProcess();
    $obj->run();
} catch (Exception $e) {
    Logger::error($e->getMessage());
}

swoole 版本 4.3.0-alpha
php 版本 7.3.0
开启coredump时候,发现文件巨大,每个子进程文件达1.4G

阅读 1.7k
1 个回答

这是因为pcre.jit导致,请修改php.ini关闭pcre.jit

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题