bpo-30807: signal.setitimer() may disable the timer by mistake (#2493) · python/cpython@729780a (original) (raw)

3 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -608,6 +608,15 @@ def test_itimer_prof(self):
608 608 # and the handler should have been called
609 609 self.assertEqual(self.hndl_called, True)
610 610
611 +def test_setitimer_tiny(self):
612 +# bpo-30807: C setitimer() takes a microsecond-resolution interval.
613 +# Check that float -> timeval conversion doesn't round
614 +# the interval down to zero, which would disable the timer.
615 +self.itimer = signal.ITIMER_REAL
616 +signal.setitimer(self.itimer, 1e-6)
617 +time.sleep(1)
618 +self.assertEqual(self.hndl_called, True)
619 +
611 620
612 621 class PendingSignalsTests(unittest.TestCase):
613 622 """
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
1 +signal.setitimer() may disable the timer when passed a tiny value.
2 +
3 +Tiny values (such as 1e-6) are valid non-zero values for setitimer(), which
4 +is specified as taking microsecond-resolution intervals. However, on some
5 +platform, our conversion routine could convert 1e-6 into a zero interval,
6 +therefore disabling the timer instead of (re-)scheduling it.
Original file line number Diff line number Diff line change
@@ -139,6 +139,10 @@ timeval_from_double(double d, struct timeval *tv)
139 139 {
140 140 tv->tv_sec = floor(d);
141 141 tv->tv_usec = fmod(d, 1.0) * 1000000.0;
142 +/* Don't disable the timer if the computation above rounds down to zero. */
143 +if (d > 0.0 && tv->tv_sec == 0 && tv->tv_usec == 0) {
144 +tv->tv_usec = 1;
145 + }
142 146 }
143 147
144 148 Py_LOCAL_INLINE(double)