Fix clearTimeout and linux timeout #1138
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
fix #1135
It seems that there were lots of bugs for
setTimeout
/setInterval
. So I'll try to clarify what I'm trying to do in this PR, in case it introduces some new bugs :pThe rationale is, for each timeout event, the
vm.active_tasks
will be added twice:IOTask
(IOTask.createOnJSThread
);Timer.set
, which is an additional count for timer.active_tasks
will prevent bun from exit: bun will loop to wait untilactive_task
is zero.And correpsond to the 2 add, there will be 2 minus:
active_task
Timer
, when a timer is triggered, and it won't happen again,active_task
need to be-|= 1
ed.The reason timer events need 2 adds instead of 1 is that, when a timer event will repeat, we can just not do the second minus and keep bun alive.
There were several problem with the origin implementation.
clearTimer
), we should do-|= 2
, so that bun could exit immediately. Instead, currently it only settimer.cancelled
totrue
.active_task
when repeating, not minusing is good enough, otherwise, thesetInterval
cannot be cancelled.And in this pr, I changed the code into:
active_tasks -|= 2
inclearTimer
active_tasks +|= 1
for repeat timeractive_tasks -|= 1
inclear
, as the timer may be canceled already.Also, for linux timeout, we should not set the count field in
io_uring_prep_timeout
, as it has a special function. For more information, please check: axboe/liburing#232Thank you for your time on this PR :)