]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Fix contextvars.Context propagation to first yield with native coroutines (#3176)
authorTimofey Kukushkin <tima@kukushkin.me>
Fri, 26 Aug 2022 19:05:22 +0000 (22:05 +0300)
committerGitHub <noreply@github.com>
Fri, 26 Aug 2022 19:05:22 +0000 (15:05 -0400)
* Fix contextvars.Context propagation to first yield with native coroutines

tornado/gen.py
tornado/test/gen_test.py

index 1946ab91744577a729cacbda71a7c8705e5a96bc..f3e6e72f1dac795697cc45b9d2da999bb70fc484 100644 (file)
@@ -743,7 +743,7 @@ class Runner(object):
         self.running = False
         self.finished = False
         self.io_loop = IOLoop.current()
-        if self.handle_yield(first_yielded):
+        if self.ctx_run(self.handle_yield, first_yielded):
             gen = result_future = first_yielded = None  # type: ignore
             self.ctx_run(self.run)
 
index 73c33878030bd2d63b9003402e842d38b3afc54b..2b0253869241208231fffc8ebc058536250cb80b 100644 (file)
@@ -1114,6 +1114,16 @@ class ContextVarsTest(AsyncTestCase):
         # so we must make sure that we maintain that property across yield.
         ctx_var.reset(token)
 
+    @gen_test
+    def test_propagate_to_first_yield_with_native_async_function(self):
+        x = 10
+
+        async def native_async_function():
+            self.assertEquals(ctx_var.get(), x)
+
+        ctx_var.set(x)
+        yield native_async_function()
+
 
 if __name__ == "__main__":
     unittest.main()