Skip to content

Commit

Permalink
thread resource destruction order fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
klemens-morgenstern committed Nov 28, 2023
1 parent 8001cfb commit 7884039
Showing 1 changed file with 32 additions and 29 deletions.
61 changes: 32 additions & 29 deletions src/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ thread_promise::thread_promise()
}

void run_thread(
std::shared_ptr<thread_state> st,
std::shared_ptr<thread_state> st_,
unique_handle<thread_promise> h)
{

Expand All @@ -35,36 +35,39 @@ void run_thread(
h->resource = &resource;
#endif

h->reset_cancellation_source(st->signal.slot());
h->set_executor(st->ctx.get_executor());
boost::cobalt::this_thread::set_executor(st->ctx.get_executor());

asio::post(
st->ctx.get_executor(),
[st, h = std::move(h)]() mutable
{
std::lock_guard<std::mutex> lock{h->mtx};
std::move(h).resume();
});

std::exception_ptr ep;

try
{
st->ctx.run();
auto st = std::move(st_);
h->reset_cancellation_source(st->signal.slot());
h->set_executor(st->ctx.get_executor());
boost::cobalt::this_thread::set_executor(st->ctx.get_executor());

asio::post(
st->ctx.get_executor(),
[st, h = std::move(h)]() mutable
{
std::lock_guard<std::mutex> lock{h->mtx};
std::move(h).resume();
});

std::exception_ptr ep;

try
{
st->ctx.run();
}
catch(...)
{
ep = std::current_exception();
}

st->done = true;
st->signal.slot().clear();
std::lock_guard<std::mutex> lock(st->mtx);
if (!st->waitor && ep) // nobodies waiting, so unhandled exception
std::rethrow_exception(ep);
else if (st->waitor)
asio::post(asio::append(*std::exchange(st->waitor, std::nullopt), ep));
}
catch(...)
{
ep = std::current_exception();
}

st->done = true;
st->signal.slot().clear();
std::lock_guard<std::mutex> lock(st->mtx);
if (!st->waitor && ep) // nobodies waiting, so unhandled exception
std::rethrow_exception(ep);
else if (st->waitor)
asio::post(asio::append(*std::exchange(st->waitor, std::nullopt), ep));
}


Expand Down

1 comment on commit 7884039

@mclow
Copy link

@mclow mclow commented on 7884039 Nov 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What an amazingly unhelpful diff.

Please sign in to comment.