[issue9550] BufferedReader may issue additional read, may cause hang when backed by blocking socket

Jason V. Miller report at bugs.python.org
Tue Aug 10 00:09:00 CEST 2010


New submission from Jason V. Miller <jason_miller at symantec.com>:

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.

bufio.py server
bufio.py client

I successfully patched this issue locally with the following:

---
Modules/_io/bufferedio.c:

    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.

----------
components: IO
files: bufio.py
messages: 113489
nosy: jvmiller
priority: normal
severity: normal
status: open
title: BufferedReader may issue additional read, may cause hang when backed by blocking socket
type: behavior
versions: Python 3.1
Added file: http://bugs.python.org/file18459/bufio.py

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue9550>
_______________________________________


More information about the Python-bugs-list mailing list