]> granicus.if.org Git - python/commitdiff
Issue #7978: socketserver now restarts the select() call when EINTR is returned.
authorAntoine Pitrou <solipsis@pitrou.net>
Sun, 8 Apr 2012 22:47:24 +0000 (00:47 +0200)
committerAntoine Pitrou <solipsis@pitrou.net>
Sun, 8 Apr 2012 22:47:24 +0000 (00:47 +0200)
This avoids crashing the server loop when a signal is received.
Patch by Jerzy Kozera.

Lib/SocketServer.py
Lib/test/test_socketserver.py
Misc/ACKS
Misc/NEWS

index 2ff388836a0eacb8739c346b629d2b922d8d3443..a44e1378547570176336d27307b41f17205e4f70 100644 (file)
@@ -133,6 +133,7 @@ import socket
 import select
 import sys
 import os
+import errno
 try:
     import threading
 except ImportError:
@@ -147,6 +148,15 @@ if hasattr(socket, "AF_UNIX"):
                     "ThreadingUnixStreamServer",
                     "ThreadingUnixDatagramServer"])
 
+def _eintr_retry(func, *args):
+    """restart a system call interrupted by EINTR"""
+    while True:
+        try:
+            return func(*args)
+        except OSError as e:
+            if e.errno != errno.EINTR:
+                raise
+
 class BaseServer:
 
     """Base class for server classes.
@@ -222,7 +232,8 @@ class BaseServer:
                 # connecting to the socket to wake this up instead of
                 # polling. Polling reduces our responsiveness to a
                 # shutdown request and wastes cpu at all other times.
-                r, w, e = select.select([self], [], [], poll_interval)
+                r, w, e = _eintr_retry(select.select, [self], [], [],
+                                       poll_interval)
                 if self in r:
                     self._handle_request_noblock()
         finally:
@@ -262,7 +273,7 @@ class BaseServer:
             timeout = self.timeout
         elif self.timeout is not None:
             timeout = min(timeout, self.timeout)
-        fd_sets = select.select([self], [], [], timeout)
+        fd_sets = _eintr_retry(select.select, [self], [], [], timeout)
         if not fd_sets[0]:
             self.handle_timeout()
             return
index 08fb03350d6c1fad3b0a98601cad7cf2c9ebab87..07b0c193ea869c7ffbfcb952fd99c854982edcf5 100644 (file)
@@ -8,6 +8,8 @@ import os
 import select
 import signal
 import socket
+import select
+import errno
 import tempfile
 import unittest
 import SocketServer
@@ -225,6 +227,38 @@ class SocketServerTest(unittest.TestCase):
                                 SocketServer.DatagramRequestHandler,
                                 self.dgram_examine)
 
+    @contextlib.contextmanager
+    def mocked_select_module(self):
+        """Mocks the select.select() call to raise EINTR for first call"""
+        old_select = select.select
+
+        class MockSelect:
+            def __init__(self):
+                self.called = 0
+
+            def __call__(self, *args):
+                self.called += 1
+                if self.called == 1:
+                    # raise the exception on first call
+                    raise OSError(errno.EINTR, os.strerror(errno.EINTR))
+                else:
+                    # Return real select value for consecutive calls
+                    return old_select(*args)
+
+        select.select = MockSelect()
+        try:
+            yield select.select
+        finally:
+            select.select = old_select
+
+    def test_InterruptServerSelectCall(self):
+        with self.mocked_select_module() as mock_select:
+            pid = self.run_server(SocketServer.TCPServer,
+                                  SocketServer.StreamRequestHandler,
+                                  self.stream_examine)
+            # Make sure select was called again:
+            self.assertGreater(mock_select.called, 1)
+
     # Alas, on Linux (at least) recvfrom() doesn't return a meaningful
     # client address so this cannot work:
 
index dc6d749190ca1a4dcf6314dcc05355b463e98a24..73afc97ec9fd7cd6ec05d6016c5e9e0184148c25 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -461,6 +461,7 @@ Greg Kochanski
 Damon Kohler
 Marko Kohtala
 Joseph Koshy
+Jerzy Kozera
 Maksim Kozyarchuk
 Stefan Krah
 Bob Kras
index 330477e6557689b1b8022c35901b32dfa24c627c..f4da7e7785c2ab1fe718ff347b491a8863106f85 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -44,6 +44,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #7978: SocketServer now restarts the select() call when EINTR is
+  returned.  This avoids crashing the server loop when a signal is received.
+  Patch by Jerzy Kozera.
+
 - Issue #14409: IDLE now properly executes commands in the Shell window
   when it cannot read the normal config files on startup and
   has to use the built-in default key bindings.