tests: fix toctou race in tinyproxy.py (issue3795)

test-http-proxy.t sometimes failed with:
    File ".../tests/tinyproxy.py", line 110, in _read_write
      data = i.recv(8192)
  error: (104, 'Connection reset by peer')

This might have started showing up with 9eb533d10f1a ... but it has apparently
also been seen before. I don't see anything in 9eb533d10f1a that can explain
it. It seems to be a race in test, in the tinyproxy helper:

Tinyproxy found an incoming socket using select(). It would break the loop if
an error had been detected on the socket, but there was no error and it tried
to recv() from the socket. That failed - apparently because it had been reset
after select().

Errors in the recv() will now be caught and will break the loop like errors
detected by select() would.

(send() could also fail in a similar way ... but using the same solution there
and losing data we have read doesn't feel right.)
This commit is contained in:
Mads Kiilerich 2013-01-31 19:13:13 +01:00
parent 5b37f126e0
commit d04afd8fe1

View File

@ -107,7 +107,10 @@ class ProxyHandler (BaseHTTPServer.BaseHTTPRequestHandler):
out = self.connection
else:
out = soc
data = i.recv(8192)
try:
data = i.recv(8192)
except socket.error:
break
if data:
out.send(data)
count = 0