]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
ioloop: Make clear_current work if called first in a thread
authorBen Darnell <ben@bendarnell.com>
Sat, 13 Jan 2018 18:55:32 +0000 (13:55 -0500)
committerBen Darnell <ben@bendarnell.com>
Sat, 13 Jan 2018 23:58:45 +0000 (18:58 -0500)
Fixes #2240

tornado/ioloop.py
tornado/test/ioloop_test.py

index 138a61b5c2984d68432f03c71d8709dd0918c8ca..c829afcf53f838dcaee1bc1a131da1d42a509474 100644 (file)
@@ -321,7 +321,7 @@ class IOLoop(Configurable):
         .. versionchanged:: 5.0
            This method also clears the current `asyncio` event loop.
         """
-        old = IOLoop._current.instance
+        old = getattr(IOLoop._current, "instance", None)
         if old is not None:
             old._clear_current_hook()
         IOLoop._current.instance = None
index b761a3f54a0918bd60a149b84a3b9c031e38cad6..7cae9f1ab77a32a1bb5fe89d8deae70dd532420f 100644 (file)
@@ -1,5 +1,6 @@
 from __future__ import absolute_import, division, print_function
 
+from concurrent.futures import ThreadPoolExecutor
 import contextlib
 import datetime
 import functools
@@ -495,6 +496,16 @@ class TestIOLoopCurrent(unittest.TestCase):
         self.assertIs(self.io_loop, IOLoop.current())
 
 
+class TestIOLoopCurrentAsync(AsyncTestCase):
+    @gen_test
+    def test_clear_without_current(self):
+        # If there is no current IOLoop, clear_current is a no-op (but
+        # should not fail). Use a thread so we see the threading.Local
+        # in a pristine state.
+        with ThreadPoolExecutor(1) as e:
+            yield e.submit(IOLoop.clear_current)
+
+
 class TestIOLoopAddCallback(AsyncTestCase):
     def setUp(self):
         super(TestIOLoopAddCallback, self).setUp()