Title: BufferedReader may issue additional read, may cause hang when backed by blocking socket
Type: behavior Stage: resolved
Components: IO Versions: Python 3.1, Python 3.2, Python 2.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: jvmiller, pitrou
Priority: normal Keywords:

Created on 2010-08-09 22:08 by jvmiller, last changed 2010-08-11 13:41 by pitrou. This issue is now closed.

File name Uploaded Description Edit jvmiller, 2010-08-09 22:08 Test Case / Reproduce Server Hang
Messages (3)
msg113489 - (view) Author: Jason V. Miller (jvmiller) Date: 2010-08-09 22:08
While reading, BufferedReader can cause an additional read() to the underlying I/O object after the entire upper-layer byte count has been satisfied. This is unnecessary and troublesome in some cases.

In the event that the BufferedReader sits on top of a blocking socket (see the included test case,) certain network protocols (HTTP) can cause the server to hang in recv/recvfrom trying to read more data than will be available, causing a hang. I first ran into this issue running bottle on top of the core Python wsgi server.

It looks as though this may be present in 2.x as well, however an associate of mine wasn't able to trigger the issue in 2.x (even after implementing the makefile() code from 3.x)

To run the test case, start the server and then run the client against it. server client

I successfully patched this issue locally with the following:


    self->pos = 0;
    self->raw_pos = 0;
    self->read_end = 0;

+   if ( remaining == 0 )
+       return res;

    while (self->read_end < self->buffer_size) {

Which aborts execution of the second loop in the event that the last block-sized read satisfied the upper layer call exactly.
msg113495 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-08-09 22:30
Thanks for caring about such issues. This would also need unit tests and (and perhaps a similar change in, although the buffering logic there is different and may not exhibit the issue).
msg113599 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-08-11 13:41
The original patch wasn't good for all cases. I corrected it, added some tests and committed in r83944 (py3k), r83945 (3.1) and r83946 (2.7). Thank you!
Date User Action Args
2010-08-11 13:41:29pitrousetstatus: open -> closed
versions: + Python 2.7
messages: + msg113599

resolution: fixed
stage: needs patch -> resolved
2010-08-09 22:30:58pitrousetstage: needs patch
messages: + msg113495
versions: + Python 3.2
2010-08-09 22:26:54r.david.murraysetnosy: + pitrou
2010-08-09 22:08:59jvmillercreate