fix: write_env breaks task's affinity#1980
Conversation
write_env breaks tasks's affinitywrite_env breaks task's affinity
565a54a to
e5fb79f
Compare
|
could you pls post some code that exhibits the problem, and tell me what behavior you're expecting? a godbolt link would be great. i think there is more going on here, and i'd like to get to the bottom of it. |
|
@ericniebler You can have a look at the test I wrote in this PR. In template <ex::scheduler Worker>
ex::task<int> inner_task(Worker worker)
{
CHECK(get_id() == 0);
int i = co_await ex::starts_on(worker, just_int(42) | ...);
CHECK(get_id() != 0);
co_return i;
}
template <ex::scheduler Worker>
ex::task<int> test_task_affinity_with_write_env(Worker worker)
{
CHECK(get_id() == 0);
auto task = inner_task(worker)
| ex::write_env(ex::prop{ex::get_scheduler, ex::inline_scheduler{}});
int i = co_await std::move(task);
CHECK(get_id() == 0); // <-------- I expect it resumes at main thread.
co_return i;
}
TEST_CASE("test task scheduler affinity works with write_env", "[types][task]")
{
exec::single_thread_context ctx;
auto t = test_task_affinity_with_write_env(ctx.get_scheduler());
auto [i] = ex::sync_wait(std::move(t)).value();
CHECK(i == 42);
} |
|
@ericniebler BTW, if I put the env directly in the |
|
i think what is happening here is easier to see with a second worker thread instead of https://godbolt.org/z/axzhe6cz4 note that if you use i don't think there's a bug here. |
|
@ericniebler thank you for the explaination! I'm still curious: in outer task's I'd very appreciate it if you can say more🙏 attached some statements from standard for references: https://eel.is/c++draft/exec#task.promise-9
https://eel.is/c++draft/task.state#4.3
|
|
That's a good question. The reason the outer task doesn't transition back to the main thread after awaiting the inner task is because the inner task claimed to be affine already. |
|
Thanks for the hint! I understand now - So we shouldn't use env(neither write_env nor Env param) to specify a fallback scheduler for My essential requirement is to specify a fallback scheduler for Anyway, this PR can be closed. Thanks again for your detailed explaination @ericniebler ! I've learned a lot :) |
|
this might interest you. we decided last week at the C++ committee meeting that we wanted a separate |
\When a
taskis wrapped withwrite_env(prop{get_scheduler, inline_scheduler{}}), the outeraffine_on(added by the outer task'sawait_transform) was incorrectly skipping the rescheduling step.