]> granicus.if.org Git - python/commitdiff
bpo-30807: signal.setitimer() may disable the timer by mistake (#2493)
authorAntoine Pitrou <pitrou@free.fr>
Fri, 30 Jun 2017 08:01:05 +0000 (10:01 +0200)
committerGitHub <noreply@github.com>
Fri, 30 Jun 2017 08:01:05 +0000 (10:01 +0200)
* bpo-30807: signal.setitimer() may disable the timer by mistake

* Add NEWS blurb

Lib/test/test_signal.py
Misc/NEWS.d/next/Library/2017-06-29-22-04-44.bpo-30807.sLtjY-.rst [new file with mode: 0644]
Modules/signalmodule.c

index 0ddfe36718872a64b4d06da349ce2e904579605a..0e1d06706d65eaa1080339c23a948fa9393402df 100644 (file)
@@ -608,6 +608,15 @@ class ItimerTest(unittest.TestCase):
         # and the handler should have been called
         self.assertEqual(self.hndl_called, True)
 
+    def test_setitimer_tiny(self):
+        # bpo-30807: C setitimer() takes a microsecond-resolution interval.
+        # Check that float -> timeval conversion doesn't round
+        # the interval down to zero, which would disable the timer.
+        self.itimer = signal.ITIMER_REAL
+        signal.setitimer(self.itimer, 1e-6)
+        time.sleep(1)
+        self.assertEqual(self.hndl_called, True)
+
 
 class PendingSignalsTests(unittest.TestCase):
     """
diff --git a/Misc/NEWS.d/next/Library/2017-06-29-22-04-44.bpo-30807.sLtjY-.rst b/Misc/NEWS.d/next/Library/2017-06-29-22-04-44.bpo-30807.sLtjY-.rst
new file mode 100644 (file)
index 0000000..ce6f48a
--- /dev/null
@@ -0,0 +1,6 @@
+signal.setitimer() may disable the timer when passed a tiny value.
+
+Tiny values (such as 1e-6) are valid non-zero values for setitimer(), which
+is specified as taking microsecond-resolution intervals. However, on some
+platform, our conversion routine could convert 1e-6 into a zero interval,
+therefore disabling the timer instead of (re-)scheduling it.
index cbf0d543051e959743aba55a72f16520b353f397..b511d17c38b027c3f7bad423a5d65296f6e500bd 100644 (file)
@@ -139,6 +139,10 @@ timeval_from_double(double d, struct timeval *tv)
 {
     tv->tv_sec = floor(d);
     tv->tv_usec = fmod(d, 1.0) * 1000000.0;
+    /* Don't disable the timer if the computation above rounds down to zero. */
+    if (d > 0.0 && tv->tv_sec == 0 && tv->tv_usec == 0) {
+        tv->tv_usec = 1;
+    }
 }
 
 Py_LOCAL_INLINE(double)