Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: task lifecycle and final_suspend #193

Open
obhi-d opened this issue Feb 21, 2021 · 0 comments
Open

Question: task lifecycle and final_suspend #193

obhi-d opened this issue Feb 21, 2021 · 0 comments

Comments

@obhi-d
Copy link

obhi-d commented Feb 21, 2021

Hello,

First thanks for the awesome library.
I have a question regarding lifecycle of cppcoro::task<> object.
Imagine a scenario where a function is producing a task, and launching them in a thread pool:

cppcoro::task<void> do_something_on_threadpool(cppcoro::static_thread_pool& tp)
{
  // First schedule the coroutine onto the threadpool.
  co_await tp.schedule();

  // When it resumes, this coroutine is now running on the threadpool.
  do_something();
}

// executing in MainThread
void producer(cppcoro::static_thread_pool& tp)
{
  cppcoro::task<void> task0 = do_something_on_threadpool(tp);
  // wait for the tasks
  co_await task0;
}

In this case, lets assume for task0, we enter final_suspend from some other thread T0, and reach final_awaiter. This would mean the coroutine .done() will return true,
Now lets say we are in MainThread, and try to execute co_await task0;
So now, if I want to set producer() as the continuation in co_await, await_ready() will return true, and the continuation will not be set. Lets say this happens, and we return from the function i.e. we exit the scope of cppcoro::task and the task coroutine gets destroyed.
Now lets say, thread T0 starts working on final_suspend(...), it will try to access the task state that was already destroyed in MainThread resulting in SEGFAULT.

Please correct me if I am wrong about this.

Thanks,
obhi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant