From c39afff8e04a9488e39577ef72bb851698c7747c Mon Sep 17 00:00:00 2001 From: Jens Jorgensen Date: Tue, 21 Jun 2022 16:53:09 -0500 Subject: [PATCH] Fix segfault in TimerHandle.when() after cleared Fixes #469 Closes #475 Co-authored-by: Fantix King --- .gitignore | 1 + tests/test_base.py | 8 ++++++++ uvloop/cbhandles.pxd | 1 + uvloop/cbhandles.pyx | 3 ++- 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 3dad090c..3b8ea62a 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,4 @@ uvloop/loop.*.pyd /.eggs /.venv* /wheelhouse +/uvloop-dev diff --git a/tests/test_base.py b/tests/test_base.py index 7414a310..ef8feb2a 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -860,6 +860,14 @@ def test_loop_call_later_handle_when(self): self.assertAlmostEqual(handle.when(), loop_t + delay, places=2) handle.cancel() self.assertTrue(handle.cancelled()) + self.assertAlmostEqual(handle.when(), loop_t + delay, places=2) + + def test_loop_call_later_handle_when_after_fired(self): + fut = self.loop.create_future() + handle = self.loop.call_later(0.05, fut.set_result, None) + when = handle.when() + self.loop.run_until_complete(fut) + self.assertEqual(handle.when(), when) class TestBaseAIO(_TestBase, AIOTestCase): diff --git a/uvloop/cbhandles.pxd b/uvloop/cbhandles.pxd index ba65d9d0..e594b133 100644 --- a/uvloop/cbhandles.pxd +++ b/uvloop/cbhandles.pxd @@ -32,6 +32,7 @@ cdef class TimerHandle: object context tuple _debug_info object __weakref__ + object _when cdef _run(self) cdef _cancel(self) diff --git a/uvloop/cbhandles.pyx b/uvloop/cbhandles.pyx index 6d6a0b04..bc67b73d 100644 --- a/uvloop/cbhandles.pyx +++ b/uvloop/cbhandles.pyx @@ -193,6 +193,7 @@ cdef class TimerHandle: loop, self._run, self, delay) self.timer.start() + self._when = self.timer.get_when() * 1e-3 # Only add to loop._timers when `self.timer` is successfully created loop._timers.add(self) @@ -309,7 +310,7 @@ cdef class TimerHandle: self._cancel() def when(self): - return self.timer.get_when() * 1e-3 + return self._when cdef format_callback_name(func):