Skip to content

Commit

Permalink
Fix crash in the destructor of shutdown phase when curl fatal error o…
Browse files Browse the repository at this point in the history
…ccurs
  • Loading branch information
matyhtf committed Oct 27, 2023
1 parent 2cf0afd commit 267eee5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
48 changes: 48 additions & 0 deletions tests/swoole_curl/fatal_error_in_callback.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
--TEST--
swoole_curl: error
--SKIPIF--
<?php
require __DIR__ . '/../include/skipif.inc';
?>
--FILE--
<?php
require __DIR__ . '/../include/bootstrap.php';

use Swoole\Runtime;

use function Swoole\Coroutine\run;

Runtime::enableCoroutine(SWOOLE_HOOK_NATIVE_CURL);
$s = microtime(true);
run(function () {
$ch = curl_init();
$code = uniqid('swoole_');
if (IS_IN_CI) {
$domain = 'www.google.com';
} else {
$domain = 'www.baidu.com';
}
$url = "https://{$domain}/?code=" . urlencode($code);

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $strHeader) {
trigger_error("test", E_USER_ERROR);
return strlen($strHeader);
});

register_shutdown_function(function () use ($ch) {
curl_close($ch);
});

curl_exec($ch);
echo "Exec\n";
curl_close($ch);
});
echo "Done\n";
?>
--EXPECTF--
Fatal error: test in %s on line %d

Warning: curl_close(): Attempt to close cURL handle from a callback in %s on line %d
7 changes: 4 additions & 3 deletions thirdparty/php/curl/interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ php_curl *swoole_curl_get_handle(zval *zid, bool exclusive, bool required) {
if (SWOOLE_G(req_status) == PHP_SWOOLE_RSHUTDOWN_END) {
exclusive = false;
}
if (exclusive) {
if (exclusive && swoole_coroutine_is_in()) {
auto handle = swoole::curl::get_handle(ch->cp);
if (handle && handle->multi && handle->multi->check_bound_co() == nullptr) {
return nullptr;
Expand Down Expand Up @@ -1953,8 +1953,9 @@ PHP_FUNCTION(swoole_native_curl_exec) {
swoole_curl_verify_handlers(ch, 1);
swoole_curl_cleanup_handle(ch);

Multi multi{};
error = multi.exec(swoole::curl::get_handle(ch->cp));
Multi *multi = new Multi();
error = multi->exec(swoole::curl::get_handle(ch->cp));
delete multi;
SAVE_CURL_ERROR(ch, error);

if (error != CURLE_OK) {
Expand Down

0 comments on commit 267eee5

Please sign in to comment.