classification
Title: httplib.response is not closed after all data has been read
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.4, Python 3.2, Python 3.3, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: nikratio, orsenthil, piotr.dobrogost, pitrou, python-dev, r.david.murray
Priority: normal Keywords: patch

Created on 2012-08-13 01:31 by nikratio, last changed 2013-02-03 03:39 by nikratio. This issue is now closed.

Files
File name Uploaded Description Edit
http_truncated_body.patch pitrou, 2013-01-30 20:57
Messages (14)
msg168068 - (view) Author: Nikolaus Rath (nikratio) * Date: 2012-08-13 01:31
Occasionally, the isclosed() method of HTTPResponse instances returns False, even though a preceding read() call returned '' (or something else that evalues to False).

This is a problem, because then the corresponding HTTPConnection can still be used to send another request, but an attempt to retrieve its response result it in ResponseNotReady() exception.

I have not found anything special about the requests for which this happens. I also looked at the httplib code and found no way for this to happen, but obviously I am missing something. Maybe someone more familiar with the codebase can tell what's happening there.

The problem happens to rarely to be able to run packet capture, but I can provide attributes of the relevant httplib instances if desired.
msg168095 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-08-13 12:42
Without a reproducible test case I doubt we are going to be able to solve this, but yes please provide what information you can for the record, in case someone else runs in to it in a more reproducible situation.
msg168147 - (view) Author: Nikolaus Rath (nikratio) * Date: 2012-08-13 21:30
The problem seems to occur regularly, just with large intervals in between. Is there any specific information that might help here? I am not quite sure what to save when the problem happens the next time. Can the response object be pickled?
msg168153 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-08-13 22:06
Do you call read() or read(<some length>)?

Please inspect at least the "fp", "length", "chunked" and "chunk_left" attributes.
msg168197 - (view) Author: Nikolaus Rath (nikratio) * Date: 2012-08-14 12:56
The call should be read(size), with size a number. I will make sure to check size, "fp", "length", "chunked" and "chunk_left" when it happens the next time.
msg173514 - (view) Author: Nikolaus Rath (nikratio) * Date: 2012-10-22 13:22
This is just a heads-up that I'm still trying to debug this. So far, the problem doesn't seem to have shown up again (for reference, the original report for this is http://code.google.com/p/s3ql/issues/detail?id=358).
msg173515 - (view) Author: Piotr Dobrogost (piotr.dobrogost) Date: 2012-10-22 13:29
@Nikolaus
Issue 16298 was raised today describing what could be the cause of what you observe.
msg180959 - (view) Author: Nikolaus Rath (nikratio) * Date: 2013-01-30 01:37
Alright, it *finally* happened again.

Attributes of the response object are:

._method: GET, 
.chunked: 0, 
.length: 9369540
.chunk_left: UNKNOWN, 
.status: 200 
.reason "OK", 
.version: 11, 
.will_close: False
msg180966 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-01-30 07:23
"length: 9369540" indicates you haven't read the whole advertised Content-Length. Is it possible the server shuts down the TCP connection even before the whole Content-Length has been sent?
msg180992 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-01-30 20:57
Here is a patch for 2.7.
Ideally, we would raise IncompleteRead in this situation, but this would break existing programs.
msg181219 - (view) Author: Roundup Robot (python-dev) Date: 2013-02-02 21:52
New changeset 8dcf94c2fef5 by Antoine Pitrou in branch '2.7':
Issue #15633: httplib.HTTPResponse is now mark closed when the server sends less than the advertised Content-Length.
http://hg.python.org/cpython/rev/8dcf94c2fef5
msg181220 - (view) Author: Roundup Robot (python-dev) Date: 2013-02-02 22:10
New changeset 03c536afeb7e by Antoine Pitrou in branch '3.2':
Issue #15633: httplib.HTTPResponse is now mark closed when the server sends less than the advertised Content-Length.
http://hg.python.org/cpython/rev/03c536afeb7e

New changeset 7d504068bc58 by Antoine Pitrou in branch '3.3':
Issue #15633: httplib.HTTPResponse is now mark closed when the server sends less than the advertised Content-Length.
http://hg.python.org/cpython/rev/7d504068bc58

New changeset 9f9287357af9 by Antoine Pitrou in branch 'default':
Issue #15633: httplib.HTTPResponse is now mark closed when the server sends less than the advertised Content-Length.
http://hg.python.org/cpython/rev/9f9287357af9
msg181221 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-02-02 22:14
I've committed the patch into 2.7 and then ported it to 3.x. Please reopen if the problem still occurs with the patch.
msg181238 - (view) Author: Nikolaus Rath (nikratio) * Date: 2013-02-03 03:39
On 01/29/2013 11:23 PM, Antoine Pitrou wrote:
> "length: 9369540" indicates you haven't read the whole advertised Content-Length. Is it possible the server shuts down the TCP connection even before the whole Content-Length has been sent?

The remote server isn't under my control, so I can't say for sure. But I
guess the connection could be terminated prematurely for quite a number
of reasons, so if that causes trouble for httplib then this might well
be the problem.

Best,

   -Nikolaus

-- 
 »Time flies like an arrow, fruit flies like a Banana.«

  PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6  02CF A9AD B7F8 AE4E 425C
History
Date User Action Args
2013-02-03 03:39:05nikratiosetmessages: + msg181238
2013-02-02 22:14:07pitrousetstatus: open -> closed
resolution: fixed
messages: + msg181221

stage: patch review -> resolved
2013-02-02 22:10:31python-devsetmessages: + msg181220
2013-02-02 21:52:37python-devsetnosy: + python-dev
messages: + msg181219
2013-01-30 20:57:44pitrousetfiles: + http_truncated_body.patch
versions: + Python 3.2, Python 3.3, Python 3.4
messages: + msg180992

keywords: + patch
stage: patch review
2013-01-30 07:23:44pitrousetmessages: + msg180966
2013-01-30 01:37:06nikratiosetmessages: + msg180959
2012-10-22 13:29:59piotr.dobrogostsetmessages: + msg173515
2012-10-22 13:22:47nikratiosetmessages: + msg173514
2012-10-22 08:41:52piotr.dobrogostsetnosy: + piotr.dobrogost
2012-08-14 12:56:01nikratiosetmessages: + msg168197
2012-08-13 22:06:38pitrousetnosy: + pitrou
messages: + msg168153
2012-08-13 21:58:20pitrousetnosy: + orsenthil
2012-08-13 21:30:35nikratiosetmessages: + msg168147
2012-08-13 12:42:52r.david.murraysetnosy: + r.david.murray
messages: + msg168095
2012-08-13 01:31:03nikratiocreate