doukuizuo1795 2014-10-09 18:14
浏览 64

使用cURL进行的SSL请求在进程分叉后失败

I have run into some pretty strange behaviour with curl

  1. If I make an SSL request using curl in the parent process and then fork the process and try to make another SSL request in the child process the attempt fails with error no. 35 (SSL connect error).
  2. If I do not make the SSL request in the parent, the one in the child process succeeds.
  3. I can make any number of non SSL requests in the parent and SSL requests in the child succeed.

It appears that this is a bug in libcurl related question and the answerer has a work around for it.

My questions are:

  1. Is curl_global_cleanup exposed by some other name in the PHP API?
  2. If not is there some other work around?

$ch = curl_init('https://www.google.ca/');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$success = curl_exec($ch);
var_dump($success !== false); // true
curl_close($ch); 

$pid = pcntl_fork();

if ($pid === 0) {
    $ch = curl_init('http://www.google.ca/');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $success = curl_exec($ch);
    var_dump($success !== false); // true
    curl_close($ch);

    $ch = curl_init('https://www.google.ca/');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $success = curl_exec($ch);
    var_dump($success !== false); // false

    $errno = curl_errno($ch); // 35
    $error = curl_error($ch); // SSL connect error
    curl_close($ch);
} else if ($pid > 0) {
    // wait for child process
    pcntl_wait($status);
} else {
    // handel fork error
}

If this is not a bug with libcurl and it is something I am doing wrong please let me know.

  • 写回答

3条回答 默认 最新

  • douzaipou3327 2014-11-23 00:00
    关注

    I have not found a way to directly deal with the issue out lined in the question. However, I have developed a couple of workarounds. Both solve the process fork problem with (guess what) another fork.

    Please note: This is not intended to be any kind of performance improvement. It is simply a workaround for the issue highlighted in the question.

    function fetch_page($page) {
        $tmp_file = tempnam(sys_get_temp_dir(), 'curl_tmp_file');
        $pid = pcntl_fork();
        if ($pid === 0) {
            $ch = curl_init($page);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $content = curl_exec($ch);
            curl_close($ch);
            if ($content) {
                file_put_contents($tmp_file, $content);
                exit(0);
            }
            else {
                exit(1);
            }
        }
        else if ($pid > 0) {
            pcntl_wait($status);
            $content = false;
            if (pcntl_wexitstatus($status) === 0) {
                $content = file_get_contents($tmp_file);
            }
            unlink($tmp_file);
            return $content;
        }
        else {
            unlink($tmp_file);
            return false;
        }
    }
    

    Letter on I can make https connections in a child process.

    fetch_page('https://www.google.ca/');
    
    $pid = pcntl_fork();
    if ($pid === 0) {   
        $ch = curl_init('https://www.google.ca/');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $success = curl_exec($ch);
        var_dump($success !== false); // true
    } else if ($pid > 0) {
        // wait for child process
        pcntl_wait($status);
    } else {
        // handel fork error
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?