]> granicus.if.org Git - python/commitdiff
bpo-31620: have asyncio/queues not leak memory when you've exceptions during waiting...
authorSuren Nihalani <SurenNihalani@users.noreply.github.com>
Tue, 7 Nov 2017 17:35:23 +0000 (09:35 -0800)
committerAndrew Svetlov <andrew.svetlov@gmail.com>
Tue, 7 Nov 2017 17:35:23 +0000 (20:35 +0300)
Lib/asyncio/queues.py
Lib/test/test_asyncio/test_queues.py
Misc/NEWS.d/next/Library/2017-10-06-04-35-31.bpo-31620.gksLA1.rst [new file with mode: 0644]

index 2d38972c0de29d70f88c651ae3cf7e29f2f3249e..1c66d67b041304fc7f569acab652b0b8587bd19c 100644 (file)
@@ -167,6 +167,12 @@ class Queue:
                 yield from getter
             except:
                 getter.cancel()  # Just in case getter is not done yet.
+
+                try:
+                    self._getters.remove(getter)
+                except ValueError:
+                    pass
+
                 if not self.empty() and not getter.cancelled():
                     # We were woken up by put_nowait(), but can't take
                     # the call.  Wake up the next in line.
index fe5a6dbfe34ecc019ee6d23389569682e546645d..2137cde6f480b7b78af702208d212463d3acf089 100644 (file)
@@ -295,6 +295,23 @@ class QueueGetTests(_QueueTestBase):
                            loop=self.loop),
             )
 
+    def test_cancelled_getters_not_being_held_in_self_getters(self):
+        def a_generator():
+            yield 0.1
+            yield 0.2
+
+        self.loop = self.new_test_loop(a_generator)
+        @asyncio.coroutine
+        def consumer(queue):
+            try:
+                item = yield from asyncio.wait_for(queue.get(), 0.1, loop=self.loop)
+            except asyncio.TimeoutError:
+                pass
+
+        queue = asyncio.Queue(loop=self.loop, maxsize=5)
+        self.loop.run_until_complete(self.loop.create_task(consumer(queue)))
+        self.assertEqual(len(queue._getters), 0)
+
 
 class QueuePutTests(_QueueTestBase):
 
diff --git a/Misc/NEWS.d/next/Library/2017-10-06-04-35-31.bpo-31620.gksLA1.rst b/Misc/NEWS.d/next/Library/2017-10-06-04-35-31.bpo-31620.gksLA1.rst
new file mode 100644 (file)
index 0000000..7874bef
--- /dev/null
@@ -0,0 +1,2 @@
+an empty asyncio.Queue now doesn't leak memory when queue.get pollers
+timeout