[issue5542] Socket is closed prematurely in httplib, if server sends response before request body has been sent
report at bugs.python.org
Mon Mar 23 10:29:11 CET 2009
New submission from Jason Davies <jason at jasondavies.com>:
I came across this bug when trying to use CouchDB-Python to PUT an
attachment using HTTP authentication (using the httplib2 library).
Essentially what httplib2 does is this:
1. Attempt PUT with no auth credentials
2. If a 401 response is received, try again with credentials
However, CouchDB sends a 401 response as soon as it has consumed the
request headers, and before the request body has been fully sent by the
This triggers a bug in both httplib and httplib2, whereby they raise
exceptions due to a broken pipe being encountered when trying to finish
sending the request body.
It seems that Python's httplib checks for broken pipe errors and closes
the connection entirely when they occur when making a request. This is
unhelpful if a legitimate response was sent and the socket was closed
early by the server.
The offending try/except handler is in httplib.HttpConnection.send and
has a comment saying:
# send the data to the server. if we get a broken pipe, then close
# the socket. we want to reconnect when somebody tries to send again.
This is wrong, as someone might want to read the response before closing
For reference, the CouchDB-Python issue is:
This bug may also be related to: http://bugs.python.org/issue3566
components: Library (Lib)
title: Socket is closed prematurely in httplib, if server sends response before request body has been sent
versions: Python 2.6
Python tracker <report at bugs.python.org>
More information about the Python-bugs-list