]> granicus.if.org Git - python/commitdiff
Issue #9550: a BufferedReader could issue an additional read when the
authorAntoine Pitrou <solipsis@pitrou.net>
Wed, 11 Aug 2010 13:31:33 +0000 (13:31 +0000)
committerAntoine Pitrou <solipsis@pitrou.net>
Wed, 11 Aug 2010 13:31:33 +0000 (13:31 +0000)
original read request had been satisfied, which can block indefinitely
when the underlying raw IO channel is e.g. a socket.  Report and original
patch by Jason V. Miller.

Lib/test/test_io.py
Misc/ACKS
Misc/NEWS
Modules/_io/bufferedio.c

index aa1679b973222b3267979854a025dcb73a0c7d4b..c584b92040997e7aa94a80a96578b1a8511a6b1e 100644 (file)
@@ -52,12 +52,14 @@ class MockRawIO:
         self._read_stack = list(read_stack)
         self._write_stack = []
         self._reads = 0
+        self._extraneous_reads = 0
 
     def read(self, n=None):
         self._reads += 1
         try:
             return self._read_stack.pop(0)
         except:
+            self._extraneous_reads += 1
             return b""
 
     def write(self, b):
@@ -88,6 +90,7 @@ class MockRawIO:
         try:
             data = self._read_stack[0]
         except IndexError:
+            self._extraneous_reads += 1
             return 0
         if data is None:
             del self._read_stack[0]
@@ -824,6 +827,27 @@ class BufferedReaderTest(unittest.TestCase, CommonBufferedTests):
         self.assertRaises(IOError, bufio.seek, 0)
         self.assertRaises(IOError, bufio.tell)
 
+    def test_no_extraneous_read(self):
+        # Issue #9550; when the raw IO object has satisfied the read request,
+        # we should not issue any additional reads, otherwise it may block
+        # (e.g. socket).
+        bufsize = 16
+        for n in (2, bufsize - 1, bufsize, bufsize + 1, bufsize * 2):
+            rawio = self.MockRawIO([b"x" * n])
+            bufio = self.tp(rawio, bufsize)
+            self.assertEqual(bufio.read(n), b"x" * n)
+            # Simple case: one raw read is enough to satisfy the request.
+            self.assertEqual(rawio._extraneous_reads, 0,
+                             "failed for {}: {} != 0".format(n, rawio._extraneous_reads))
+            # A more complex case where two raw reads are needed to satisfy
+            # the request.
+            rawio = self.MockRawIO([b"x" * (n - 1), b"x"])
+            bufio = self.tp(rawio, bufsize)
+            self.assertEqual(bufio.read(n), b"x" * n)
+            self.assertEqual(rawio._extraneous_reads, 0,
+                             "failed for {}: {} != 0".format(n, rawio._extraneous_reads))
+
+
 class CBufferedReaderTest(BufferedReaderTest):
     tp = io.BufferedReader
 
index 26501478307f5f44498877c5999e4fe6b8cbfb97..26feba34e9dcf0e89ef5ab91320e79036b48b924 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -548,6 +548,7 @@ Trent Mick
 Aristotelis Mikropoulos
 Damien Miller
 Chad Miller
+Jason V. Miller
 Jay T. Miller
 Roman Milner
 Andrii V. Mishkovskyi
index b89f89d1ce462ed5cd8286e461c5e6153d138c72..80d07f514a8ae735de8321137138a94dd0f23899 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -70,6 +70,11 @@ Extensions
 Library
 -------
 
+- Issue #9550: a BufferedReader could issue an additional read when the
+  original read request had been satisfied, which could block indefinitely
+  when the underlying raw IO channel was e.g. a socket.  Report and original
+  patch by Jason V. Miller.
+
 - Issue #3757: thread-local objects now support cyclic garbage collection.
   Thread-local objects involved in reference cycles will be deallocated
   timely by the cyclic GC, even if the underlying thread is still running.
index 1dd1ec4ba94c9dc3aff409de0df209ea4e5fbad0..4678da91c97c1ac13910045897b9d532d1a7f8d4 100644 (file)
@@ -1383,7 +1383,10 @@ _bufferedreader_read_generic(buffered *self, Py_ssize_t n)
     self->pos = 0;
     self->raw_pos = 0;
     self->read_end = 0;
-    while (self->read_end < self->buffer_size) {
+    /* NOTE: when the read is satisfied, we avoid issuing any additional
+       reads, which could block indefinitely (e.g. on a socket).
+       See issue #9550. */
+    while (remaining > 0 && self->read_end < self->buffer_size) {
         Py_ssize_t r = _bufferedreader_fill_buffer(self);
         if (r == -1)
             goto error;