| 1 |
"""Issue #143 - Socket timeouts in wsgi server not caught. |
| 2 |
https://bitbucket.org/eventlet/eventlet/issue/143/ |
| 3 |
|
| 4 |
This file intentionally ignored by nose. |
| 5 |
Caller process (tests.wsgi_test.TestWsgiConnTimeout) handles success / failure |
| 6 |
|
| 7 |
|
| 8 |
Simulate server connection socket timeout without actually waiting. |
| 9 |
Logs 'timed out' if server debug=True (similar to 'accepted' logging) |
| 10 |
|
| 11 |
FAIL: if log (ie, _spawn_n_impl 'except:' catches timeout, logs TB) |
| 12 |
NOTE: timeouts are NOT on server_sock, but on the conn sockets produced |
| 13 |
by the socket.accept() call |
| 14 |
|
| 15 |
server's socket.listen() sock - NaughtySocketAcceptWrap |
| 16 |
/ | \ |
| 17 |
| | | (1 - many) |
| 18 |
V V V |
| 19 |
server / client accept() conn - ExplodingConnectionWrap |
| 20 |
/ | \ |
| 21 |
| | | (1 - many) |
| 22 |
V V V |
| 23 |
connection makefile() file objects - ExplodingSocketFile <-- these raise |
| 24 |
""" |
| 25 |
import socket |
| 26 |
|
| 27 |
import eventlet |
| 28 |
import tests.wsgi_test |
| 29 |
|
| 30 |
|
| 31 |
# no standard tests in this file, ignore |
| 32 |
__test__ = False |
| 33 |
|
| 34 |
|
| 35 |
TAG_BOOM = "=== ~* BOOM *~ ===" |
| 36 |
|
| 37 |
output_buffer = [] |
| 38 |
|
| 39 |
|
| 40 |
class BufferLog: |
| 41 |
@staticmethod |
| 42 |
def write(s): |
| 43 |
output_buffer.append(s.rstrip()) |
| 44 |
return len(s) |
| 45 |
|
| 46 |
|
| 47 |
# This test might make you wince |
| 48 |
class NaughtySocketAcceptWrap: |
| 49 |
# server's socket.accept(); patches resulting connection sockets |
| 50 |
|
| 51 |
def __init__(self, sock): |
| 52 |
self.sock = sock |
| 53 |
self.sock._really_accept = self.sock.accept |
| 54 |
self.sock.accept = self |
| 55 |
self.conn_reg = [] |
| 56 |
|
| 57 |
def unwrap(self): |
| 58 |
self.sock.accept = self.sock._really_accept |
| 59 |
del self.sock._really_accept |
| 60 |
for conn_wrap in self.conn_reg: |
| 61 |
conn_wrap.unwrap() |
| 62 |
|
| 63 |
def arm(self): |
| 64 |
output_buffer.append("ca-click") |
| 65 |
for i in self.conn_reg: |
| 66 |
i.arm() |
| 67 |
|
| 68 |
def __call__(self): |
| 69 |
output_buffer.append(self.__class__.__name__ + ".__call__") |
| 70 |
conn, addr = self.sock._really_accept() |
| 71 |
self.conn_reg.append(ExplodingConnectionWrap(conn)) |
| 72 |
return conn, addr |
| 73 |
|
| 74 |
|
| 75 |
class ExplodingConnectionWrap: |
| 76 |
# new connection's socket.makefile |
| 77 |
# eventlet *tends* to use socket.makefile, not raw socket methods. |
| 78 |
# need to patch file operations |
| 79 |
|
| 80 |
def __init__(self, conn): |
| 81 |
self.conn = conn |
| 82 |
self.conn._really_makefile = self.conn.makefile |
| 83 |
self.conn.makefile = self |
| 84 |
self.armed = False |
| 85 |
self.file_reg = [] |
| 86 |
|
| 87 |
def unwrap(self): |
| 88 |
self.conn.makefile = self.conn._really_makefile |
| 89 |
del self.conn._really_makefile |
| 90 |
|
| 91 |
def arm(self): |
| 92 |
output_buffer.append("tick") |
| 93 |
for i in self.file_reg: |
| 94 |
i.arm() |
| 95 |
|
| 96 |
def __call__(self, mode='r', bufsize=-1): |
| 97 |
output_buffer.append(self.__class__.__name__ + ".__call__") |
| 98 |
# file_obj = self.conn._really_makefile(*args, **kwargs) |
| 99 |
file_obj = ExplodingSocketFile(self.conn._sock, mode, bufsize) |
| 100 |
self.file_reg.append(file_obj) |
| 101 |
return file_obj |
| 102 |
|
| 103 |
|
| 104 |
class ExplodingSocketFile(eventlet.greenio._fileobject): |
| 105 |
|
| 106 |
def __init__(self, sock, mode='rb', bufsize=-1, close=False): |
| 107 |
super(self.__class__, self).__init__(sock, mode) |
| 108 |
self.armed = False |
| 109 |
|
| 110 |
def arm(self): |
| 111 |
output_buffer.append("beep") |
| 112 |
self.armed = True |
| 113 |
|
| 114 |
def _fuse(self): |
| 115 |
if self.armed: |
| 116 |
output_buffer.append(TAG_BOOM) |
| 117 |
raise socket.timeout("timed out") |
| 118 |
|
| 119 |
def readline(self, *args, **kwargs): |
| 120 |
output_buffer.append(self.__class__.__name__ + ".readline") |
| 121 |
self._fuse() |
| 122 |
return super(self.__class__, self).readline(*args, **kwargs) |
| 123 |
|
| 124 |
|
| 125 |
def step(debug): |
| 126 |
output_buffer[:] = [] |
| 127 |
|
| 128 |
server_sock = eventlet.listen(('localhost', 0)) |
| 129 |
server_addr = server_sock.getsockname() |
| 130 |
sock_wrap = NaughtySocketAcceptWrap(server_sock) |
| 131 |
|
| 132 |
eventlet.spawn_n( |
| 133 |
eventlet.wsgi.server, |
| 134 |
debug=debug, |
| 135 |
log=BufferLog, |
| 136 |
max_size=128, |
| 137 |
site=tests.wsgi_test.Site(), |
| 138 |
sock=server_sock, |
| 139 |
) |
| 140 |
|
| 141 |
try: |
| 142 |
# req #1 - normal |
| 143 |
sock1 = eventlet.connect(server_addr) |
| 144 |
sock1.settimeout(0.1) |
| 145 |
fd1 = sock1.makefile('rwb') |
| 146 |
fd1.write(b'GET / HTTP/1.1\r\nHost: localhost\r\n\r\n') |
| 147 |
fd1.flush() |
| 148 |
tests.wsgi_test.read_http(sock1) |
| 149 |
|
| 150 |
# let the server socket ops catch up, set bomb |
| 151 |
eventlet.sleep(0) |
| 152 |
output_buffer.append("arming...") |
| 153 |
sock_wrap.arm() |
| 154 |
|
| 155 |
# req #2 - old conn, post-arm - timeout |
| 156 |
fd1.write(b'GET / HTTP/1.1\r\nHost: localhost\r\n\r\n') |
| 157 |
fd1.flush() |
| 158 |
try: |
| 159 |
tests.wsgi_test.read_http(sock1) |
| 160 |
assert False, 'Expected ConnectionClosed exception' |
| 161 |
except tests.wsgi_test.ConnectionClosed: |
| 162 |
pass |
| 163 |
|
| 164 |
fd1.close() |
| 165 |
sock1.close() |
| 166 |
finally: |
| 167 |
# reset streams, then output trapped tracebacks |
| 168 |
sock_wrap.unwrap() |
| 169 |
# check output asserts in tests.wsgi_test.TestHttpd |
| 170 |
# test_143_server_connection_timeout_exception |
| 171 |
|
| 172 |
return output_buffer[:] |
| 173 |
|
| 174 |
|
| 175 |
def main(): |
| 176 |
output_normal = step(debug=False) |
| 177 |
output_debug = step(debug=True) |
| 178 |
|
| 179 |
assert "timed out" in output_debug[-1], repr(output_debug) |
| 180 |
# if the BOOM check fails, it's because our timeout didn't happen |
| 181 |
# (if eventlet stops using file.readline() to read HTTP headers, |
| 182 |
# for instance) |
| 183 |
assert TAG_BOOM == output_debug[-2], repr(output_debug) |
| 184 |
assert TAG_BOOM == output_normal[-1], repr(output_normal) |
| 185 |
assert "Traceback" not in output_debug, repr(output_debug) |
| 186 |
assert "Traceback" not in output_normal, repr(output_normal) |
| 187 |
print("pass") |
| 188 |
|
| 189 |
|
| 190 |
if __name__ == '__main__': |
| 191 |
main() |