Skip to content
This repository was archived by the owner on Jun 10, 2024. It is now read-only.

Commit a1ee15d

Browse files
committed
Fix underflow in timer cancel
Same problem as in timer read, so use `milliseconds_remaining` in both to handle the checked sub.
1 parent e94a360 commit a1ee15d

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

lumen_runtime/src/timer.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl Hierarchy {
106106
LongTerm => self.long_term.cancel(timer_reference_number),
107107
};
108108

109-
arc_timer.monotonic_time_milliseconds - monotonic::time_in_milliseconds()
109+
arc_timer.milliseconds_remaining()
110110
})
111111
}
112112

@@ -132,18 +132,7 @@ impl Hierarchy {
132132
self.timer_by_reference_number
133133
.get(&timer_reference_number)
134134
.and_then(|weak_timer| weak_timer.upgrade())
135-
.map(|rc_timer| {
136-
// The timer may be read when it is past its timeout, but it has not been timed-out
137-
// by the scheduler. Without this, an underflow would occur.
138-
// `0` is returned on underflow because that is what Erlang returns.
139-
match rc_timer
140-
.monotonic_time_milliseconds
141-
.checked_sub(monotonic::time_in_milliseconds())
142-
{
143-
Some(difference) => difference,
144-
None => 0,
145-
}
146-
})
135+
.map(|rc_timer| rc_timer.milliseconds_remaining())
147136
}
148137

149138
fn start(
@@ -388,6 +377,19 @@ struct Timer {
388377
}
389378

390379
impl Timer {
380+
fn milliseconds_remaining(&self) -> Milliseconds {
381+
// The timer may be read when it is past its timeout, but it has not been timed-out
382+
// by the scheduler. Without this, an underflow would occur.
383+
// `0` is returned on underflow because that is what Erlang returns.
384+
match self
385+
.monotonic_time_milliseconds
386+
.checked_sub(monotonic::time_in_milliseconds())
387+
{
388+
Some(difference) => difference,
389+
None => 0,
390+
}
391+
}
392+
391393
fn timeout(self) -> Result<(), Alloc> {
392394
let option_destination_arc_process = match &self.destination {
393395
Destination::Name(ref name) => registry::atom_to_process(name),

0 commit comments

Comments
 (0)