Skip to content

Commit

Permalink
Fix potential race condition in lightweight_manual_reset_event.
Browse files Browse the repository at this point in the history
Need to hold the mutex while calling `cv.notify_all()` to prevent
it from potentially calling `notify_all()` on a destroyed
`condition_variable` object. A thread inside `.wait()` might see
the write to `m_isSet` after the lock is released within `.set()`
but before `notify_all()` is called and then continue executing and
go on to destroy the event object (and thus the condition_variable)
leaving the thread that is about to call `cv.notify_all()` with a
dangling reference.
  • Loading branch information
lewissbaker committed Jan 1, 2019
1 parent c6bdd25 commit 0a768bf
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions lib/lightweight_manual_reset_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,8 @@ cppcoro::detail::lightweight_manual_reset_event::~lightweight_manual_reset_event

void cppcoro::detail::lightweight_manual_reset_event::set() noexcept
{
{
std::lock_guard<std::mutex> lock(m_mutex);
m_isSet = true;
}
std::lock_guard<std::mutex> lock(m_mutex);
m_isSet = true;
m_cv.notify_all();
}

Expand Down

0 comments on commit 0a768bf

Please sign in to comment.