Fix: timer_manager:send_after/3 returns an uncancellable timer#2358
Open
Qata wants to merge 1 commit into
Open
Fix: timer_manager:send_after/3 returns an uncancellable timer#2358Qata wants to merge 1 commit into
Qata wants to merge 1 commit into
Conversation
send_after/3 minted a fresh reference with erlang:make_ref/0 and returned
it, but registered the actual timer under a different reference created by
an intermediate proxy process. As a result the returned reference was never
present in the timer manager's table, so cancel_timer/1 always returned
false and the scheduled message was delivered even after a cancel. The proxy
process also had no cancel path, making send_after timers structurally
uncancellable. This affects erlang:send_after/3 and timer:send_after/2,3,
which delegate to it.
Route send_after/3 through the same gen_server timer path as start_timer/3,
tagging the timer with a delivery mode so it delivers the bare message
instead of a {timeout, Ref, Msg} tuple. The returned reference is now
registered and cancellable, and cancel_timer/1 returns the remaining
milliseconds and suppresses delivery.
Add regression tests covering cancellation of a send_after timer and that a
cancelled send_after timer does not deliver its message.
These changes are made under both the "Apache 2.0" and the "GNU Lesser General
Public License 2.1 or later" license terms (dual license).
SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
Signed-off-by: Made In Heaven <madeinheaven@madeinheaven.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What
Found while formalising OTP's timer semantics as a typed algebra for a dependently typed BEAM language:
send_aftertimers can't be cancelled.timer_manager:send_after/3returned a freshmake_ref()that was neverregistered with the timer manager. As a result:
timer_manager:cancel_timer/1on that reference always returnedfalse, andBecause
erlang:send_after/3andtimer:send_after/2,3delegate totimer_manager:send_after/3, they were affected too — asend_aftertimer wasstructurally impossible to cancel.
Why
send_after/3spawned an anonymous proxy process to deliver the bare message,but never inserted
{TimerRef, Pid}into the manager's timer table and nevergave the proxy a
{cancel, _}clause.cancel_timer/1looks the ref up in thattable, found nothing, and returned
false; the proxy had no way to be told tostop, so it always fired.
How
send_after/3now routes through the same gen_server path asstart_timer/3,so its timer is registered and cancellable. A delivery
Mode(wrapped|bare) is threaded throughdo_start_timer/4andrun_timer/6to preserve theone real behavioural difference between the two entry points:
start_timer/3delivers{timeout, TimerRef, Msg}(wrapped)send_after/3delivers the bareMsg(bare)The throwaway proxy and
send_after_timer/3are gone; there is now a singletimer lifecycle for both APIs.
Testing
Two regression tests added to
tests/libs/eavmlib/test_timer_manager.erl:test_cancel_send_after/0— asend_afterref is present inget_timer_refs/0,cancel_timer/1returns the remaining time (a positiveinteger), the ref is then gone, and a second cancel returns
false.test_cancel_send_after_suppresses_message/0— after cancelling, the messagedoes not arrive within the timeout window.
Both fail on
mainand pass with this change. Verified red→green on host OTP andon the AtomVM VM via the rebuilt
test_eavmlib.avm. Existingtest_timer_managercases (start_timer,cancel_timer,cancel_timer_after_expiry,erlang_cancel_timer,send_after) still pass.A
### Fixedentry has been added to the[0.7.0-beta.0] - Unreleasedsectionof
CHANGELOG.md.