Skip to content

Commit

Permalink
Fix timer (#4042)
Browse files Browse the repository at this point in the history
* Fix timer

* Optimize

* Add test

* Optimize warning info
  • Loading branch information
Yurunsoft authored Feb 4, 2021
1 parent 2a741fe commit e678bba
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
18 changes: 16 additions & 2 deletions src/wrapper/timer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,18 @@ TimerNode *swoole_timer_add(long ms, bool persistent, const TimerCallback &callb
}

bool swoole_timer_del(TimerNode *tnode) {
if (!swoole_timer_is_available()) {
swWarn("timer is not available");
return false;
}
return SwooleTG.timer->remove(tnode);
}

void swoole_timer_delay(TimerNode *tnode, long delay_ms) {
if (!swoole_timer_is_available()) {
swWarn("timer is not available");
return;
}
return SwooleTG.timer->delay(tnode, delay_ms);
}

Expand Down Expand Up @@ -77,27 +85,32 @@ long swoole_timer_tick(long ms, const TimerCallback &callback, void *private_dat

bool swoole_timer_exists(long timer_id) {
if (!swoole_timer_is_available()) {
swWarn("timer[%ld] is not exists", timer_id);
swWarn("timer is not available");
return false;
}
TimerNode *tnode = SwooleTG.timer->get(timer_id);
return (tnode && !tnode->removed);
}

bool swoole_timer_clear(long timer_id) {
if (!swoole_timer_is_available()) {
swWarn("timer is not available");
return false;
}
return SwooleTG.timer->remove(SwooleTG.timer->get(timer_id));
}

TimerNode *swoole_timer_get(long timer_id) {
if (!swoole_timer_is_available()) {
swWarn("timer[%ld] is not exists", timer_id);
swWarn("timer is not available");
return nullptr;
}
return SwooleTG.timer->get(timer_id);
}

void swoole_timer_free() {
if (!swoole_timer_is_available()) {
swWarn("timer is not available");
return;
}
delete SwooleTG.timer;
Expand All @@ -107,6 +120,7 @@ void swoole_timer_free() {

int swoole_timer_select() {
if (!swoole_timer_is_available()) {
swWarn("timer is not available");
return SW_ERR;
}
return SwooleTG.timer->select();
Expand Down
34 changes: 34 additions & 0 deletions tests/swoole_runtime/curl_native/timer_coredump.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--TEST--
swoole_runtime/curl_native: timer coredump
--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);

function test()
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://httpbin.org/');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_exec($curl);
curl_close($curl);
}

run('test');
run('test');

echo "Done\n";
?>
--EXPECT--
Done
2 changes: 1 addition & 1 deletion thirdparty/php/curl/curl_multi.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class cURLMulti {
}

void del_timer() {
if (timer && swoole_event_is_available()) {
if (timer) {
swoole_timer_del(timer);
}
}
Expand Down

0 comments on commit e678bba

Please sign in to comment.