trungn1992 / httplib2

Automatically exported from code.google.com/p/httplib2
0 stars 0 forks source link

ECONNREFUSED is mishandled #147

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
This is using latest httplib2 (cf721c1), *not* 0.6.0.

Use httplib2 to connect to a server which returns ECONNREFUSED (eg, talk to a 
port with no server on it.) Consider the following code:

h = httplib2.Http()
try:
    h.request(url)
except socket.error:
    time.sleep(5)
    h.request(url)

As it stands this results in the familiar error "AttributeError: 'NoneType' 
object has no attribute 'makefile'".

When the first connection attempt fails, httplib generates an ECONNREFUSED 
error; httplib2 catches the error (httplib2/__init__.py:895) and simply 
re-raises it. However, it doesn't close the connection.

This means that when the second connection attempt is made, then the connection 
has been left in an improper state. Retrying the exception does *not* result in 
a second socket.error from httplib. Instead, httplib raises a CannotSendRequest 
exception (a subclass of ImproperConnectionState, itself a subclass of 
HTTPException).

httplib2 treats all HTTPException errors by simply ignoring them 
(httlib2/__init__.py:897). As a result, _conn_request thinks a successful 
connection has been made, and the non-existent connection is passed on and 
mishandled.

The correct behaviour is to close the connection immediately after the first 
ECONNREFUSED. The attached patch does this, and works for me.

(NB the reason why I want to do this is because the service in question is 
under my control - I know there is a chance that the service might be 
accessible within 5 seconds of the first failure.)

Original issue reported on code.google.com by toby.o.h...@gmail.com on 11 May 2011 at 11:46

Attachments:

GoogleCodeExporter commented 8 years ago
Sorry, despite what I said, it turns out I was looking at the wrong version of 
the code.

Please close this issue - it's not a bug as of cf721c1.

Original comment by toby.o.h...@gmail.com on 11 May 2011 at 11:51

GoogleCodeExporter commented 8 years ago

Original comment by joe.gregorio@gmail.com on 6 Jun 2011 at 8:14