简体   繁体   中英

Bus error on curl_multi_close() call

PHP-script is for downloading files from FTP-servers. But when I close multi-handler, php cli terminal show me "Bus error" and terminate process.

Which lines can cause this error? How to fix it?

function download ($host, array $files, $threadsCount = 5) {
    $filesCount = count($fileNames);
    $threadsCount = $filesCount < $threadsCount ? $filesCount : $threadsCount;

    $mh = curl_multi_init();
    $fh = [];

    // Default options for all threads
    $options = [
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_FOLLOWLOCATION => 1,
        CURLOPT_MAXREDIRS      => 5,
        CURLOPT_HEADER         => 0,
        CURLOPT_BINARYTRANSFER => 1,
        CURLOPT_TIMEOUT        => 200,
        CURLOPT_CONNECTTIMEOUT => 200,
    ];

    // Init first threads
    for ($i = 0; $i < $threadsCount; $i++) {
        $src = $fileNames[$i];

        // Path to file on server
        $url = $host . '/' . $src;

        // Where to store this file
        $tmp = '/tmp/' . $src;

        $ch = curl_init();
        $fh[$url] = fopen($tmp, 'wb');

        $files[$src] = $tmp;

        curl_setopt_array($ch, $options + [
            CURLOPT_URL  => $url,
            CURLOPT_FILE => $fh[$url]
        ]);

        curl_multi_add_handle($mh, $ch);
    }

    // Process threads
    do {
        while (($state = curl_multi_exec($mh, $running)) == CURLM_CALL_MULTI_PERFORM);

        if ($state != CURLM_OK) {
            break;
        }

        // Request is completed
        while ($done = curl_multi_info_read($mh)) {
            // Start a new request before killing old
            if ($i < $filesCount) {
                $src = $fileNames[$i];

                $url = $host . '/' . $src;
                $tmp = '/tmp/' . $src;

                $ch = curl_init();
                $fh[$url] = fopen($tmp, 'wb');

                curl_setopt_array($ch, $options + [
                    CURLOPT_URL  => $url,
                    CURLOPT_FILE => $fh[$url]
                ]);

                curl_multi_add_handle($mh, $ch);
                $i++;
            }

            // Close handlers for files and cURL
            fclose($fh[ curl_getinfo($done['handle'], CURLINFO_EFFECTIVE_URL) ]);

            curl_multi_remove_handle($mh, $done['handle']);
            curl_close($done['handle']);
        }

        if ($running) {
            curl_multi_select($mh, 10);
        }

    } while ($running);

    // AFTER THIS "BUS ERROR" IS FIRING!
    curl_multi_close($mh);
}

For some servers this error doesn't appear.

It sounds like a bug in the libcurl library or in the PHP/CURL binding. If you're not using recent versions you thus have a reason to upgrade.

If you do use recent versions, you have a reason to take this issue with the projects directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM