From: Timofey Kukushkin Date: Fri, 26 Aug 2022 19:05:22 +0000 (+0300) Subject: Fix contextvars.Context propagation to first yield with native coroutines (#3176) X-Git-Tag: v6.3.0b1~28 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=217d57690eb00a86d9eeaee6fe6280fced683f60;p=thirdparty%2Ftornado.git Fix contextvars.Context propagation to first yield with native coroutines (#3176) * Fix contextvars.Context propagation to first yield with native coroutines --- diff --git a/tornado/gen.py b/tornado/gen.py index 1946ab917..f3e6e72f1 100644 --- a/tornado/gen.py +++ b/tornado/gen.py @@ -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) diff --git a/tornado/test/gen_test.py b/tornado/test/gen_test.py index 73c338780..2b0253869 100644 --- a/tornado/test/gen_test.py +++ b/tornado/test/gen_test.py @@ -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()