Skip to content

Commit 729780a

Browse files
authored
bpo-30807: signal.setitimer() may disable the timer by mistake (#2493)
* bpo-30807: signal.setitimer() may disable the timer by mistake * Add NEWS blurb
1 parent 42bc8be commit 729780a

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

Lib/test/test_signal.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,15 @@ def test_itimer_prof(self):
608608
# and the handler should have been called
609609
self.assertEqual(self.hndl_called, True)
610610

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+
611620

612621
class PendingSignalsTests(unittest.TestCase):
613622
"""
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff 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.

Modules/signalmodule.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ timeval_from_double(double d, struct timeval *tv)
139139
{
140140
tv->tv_sec = floor(d);
141141
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+
}
142146
}
143147

144148
Py_LOCAL_INLINE(double)

0 commit comments

Comments
 (0)