diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2010-09-27 17:52:25 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2010-09-27 17:52:25 (GMT) |
commit | 6d7df638370b7298a78af1e55e6cbb36f77b42ec (patch) | |
tree | a0eeb8015c3376cfcab50c491b0373a5af319dbe /Lib/test/test_socket.py | |
parent | 0ae33611faba57f650b5083df6eebd655457f687 (diff) | |
download | cpython-6d7df638370b7298a78af1e55e6cbb36f77b42ec.zip cpython-6d7df638370b7298a78af1e55e6cbb36f77b42ec.tar.gz cpython-6d7df638370b7298a78af1e55e6cbb36f77b42ec.tar.bz2 |
Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
received. Now sendall() properly calls signal handlers if necessary,
and retries sending if these returned successfully, including on sockets
with a timeout.
Diffstat (limited to 'Lib/test/test_socket.py')
-rw-r--r-- | Lib/test/test_socket.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index 3ef0850..ccb9cd0 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -16,6 +16,7 @@ import array import contextlib from weakref import proxy import signal +import math def try_address(host, port=0, family=socket.AF_INET): """Try to bind a socket on the given host:port and return True @@ -655,6 +656,42 @@ class GeneralModuleTests(unittest.TestCase): # have a reverse entry yet # socket.gethostbyaddr('испытание.python.org') + def check_sendall_interrupted(self, with_timeout): + # socketpair() is not stricly required, but it makes things easier. + if not hasattr(signal, 'alarm') or not hasattr(socket, 'socketpair'): + self.skipTest("signal.alarm and socket.socketpair required for this test") + # Our signal handlers clobber the C errno by calling a math function + # with an invalid domain value. + def ok_handler(*args): + self.assertRaises(ValueError, math.acosh, 0) + def raising_handler(*args): + self.assertRaises(ValueError, math.acosh, 0) + 1 // 0 + c, s = socket.socketpair() + old_alarm = signal.signal(signal.SIGALRM, raising_handler) + try: + if with_timeout: + # Just above the one second minimum for signal.alarm + c.settimeout(1.5) + with self.assertRaises(ZeroDivisionError): + signal.alarm(1) + c.sendall(b"x" * (1024**2)) + if with_timeout: + signal.signal(signal.SIGALRM, ok_handler) + signal.alarm(1) + self.assertRaises(socket.timeout, c.sendall, b"x" * (1024**2)) + finally: + signal.signal(signal.SIGALRM, old_alarm) + c.close() + s.close() + + def test_sendall_interrupted(self): + self.check_sendall_interrupted(False) + + def test_sendall_interrupted_with_timeout(self): + self.check_sendall_interrupted(True) + + @unittest.skipUnless(thread, 'Threading required for this test.') class BasicTCPTest(SocketConnectedTest): |