Skip to content

Commit 6f3cb05

Browse files
authored
[3.6] bpo-30807: signal.setitimer() may disable the timer by mistake (GH-2493) (#2497)
* bpo-30807: signal.setitimer() may disable the timer by mistake * Add NEWS blurb (cherry picked from commit 729780a)
1 parent 6f31717 commit 6f3cb05

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
@@ -611,6 +611,15 @@ def test_itimer_prof(self):
611611
# and the handler should have been called
612612
self.assertEqual(self.hndl_called, True)
613613

614+
def test_setitimer_tiny(self):
615+
# bpo-30807: C setitimer() takes a microsecond-resolution interval.
616+
# Check that float -> timeval conversion doesn't round
617+
# the interval down to zero, which would disable the timer.
618+
self.itimer = signal.ITIMER_REAL
619+
signal.setitimer(self.itimer, 1e-6)
620+
time.sleep(1)
621+
self.assertEqual(self.hndl_called, True)
622+
614623

615624
class PendingSignalsTests(unittest.TestCase):
616625
"""
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)