RequestContext changed after co_await.
- Dominant language
- C++
- Stars
- 30.5k
- Forks
- 5.9k
- PR merge metrics
- No merged PRs in 30d
Description
According to documentation, task coroutine is RequestContext-aware and will capture the current RequestContext at the time the coroutine function is either awaited or explicitly started and will save/restore the current RequestContext whenever the coroutine suspends and resumes at a co_await expression.
However, for tasks using the default `RequestContext`, the `RequestContext` may change after `co_await`.
```cpp
TEST(RequestContextTest, Baton) {
folly::CPUThreadPoolExecutor exec(2);
folly::coro::Baton baton;
auto producer = folly::coro::co_invoke([&]() -> folly::coro::Task {
co_await folly::coro::sleep(std::chrono::seconds(1));
folly::RequestContext::create(); // create request context
fmt::print("{}\n", (void*)folly::RequestContext::get());
baton.post();
co_return;
}).scheduleOn(&exec).start();
auto consumer = folly::coro::co_invoke([&]() -> folly::coro::Task {
auto ctx = folly::RequestContext::get(); // default request context
co_await baton;
EXPECT_EQ(ctx, folly::RequestContext::get()); // request context changed here
}).scheduleOn(&exec).start();
producer.wait();
consumer.wait();
}
```
Test result:
```bash
[ RUN ] RequestContextTest.Baton
0xffff94008410
/home/parallels/folly/folly/experimental/coro/test/RequestContextTest.cpp:300: Failure
Expected equality of these values:
ctx
Which is: 0xaaaae529f050
folly::RequestContext::get()
Which is: 0xffff94008410
[ FAILED ] RequestContextTest.Baton (1008 ms)
```
In this test, consumer and producer communicate through baton. The consumer's `RequestContext` is changed after `co_await` on the baton. The same problem exists when using `fiber::semaphore` and `coro::UnboundedQueue`.
Contributor guide
Assessment
This issue has not been assessed yet.