Skip to content

Commit 217d576

Browse files
authored
Fix contextvars.Context propagation to first yield with native coroutines (#3176)
* Fix contextvars.Context propagation to first yield with native coroutines
1 parent 1a4c8e5 commit 217d576

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

tornado/gen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ def __init__(
743743
self.running = False
744744
self.finished = False
745745
self.io_loop = IOLoop.current()
746-
if self.handle_yield(first_yielded):
746+
if self.ctx_run(self.handle_yield, first_yielded):
747747
gen = result_future = first_yielded = None # type: ignore
748748
self.ctx_run(self.run)
749749

tornado/test/gen_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,16 @@ def test_reset(self):
11141114
# so we must make sure that we maintain that property across yield.
11151115
ctx_var.reset(token)
11161116

1117+
@gen_test
1118+
def test_propagate_to_first_yield_with_native_async_function(self):
1119+
x = 10
1120+
1121+
async def native_async_function():
1122+
self.assertEquals(ctx_var.get(), x)
1123+
1124+
ctx_var.set(x)
1125+
yield native_async_function()
1126+
11171127

11181128
if __name__ == "__main__":
11191129
unittest.main()

0 commit comments

Comments
 (0)