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

Fix CoreJob race condition #1027

Merged
merged 3 commits into from
Oct 28, 2017
Merged

Fix CoreJob race condition #1027

merged 3 commits into from
Oct 28, 2017

Conversation

gittyupagain
Copy link
Contributor

It was possible for current event handler to be deleted twice, and also for it to inadvertently be started multiple times, causing a few user functions to be run in different threads.

One of the main issues was that, although CoreJob's OnPended function expected a lock, two different locks were being passed from different threads to the function, nullifying exclusivity. Also, the wait functions could be called from different threads, potentially causing a double free.

A test (CoreJobTest.PendFromMultipleThreads) has been added, but it doesn't fail every time without this fix. To see the failure case, the test should be repeated multiple times (using --gtest_repeat, specifying the number of runs, and --gtest_throw_on_failure, to stop on failure).

@@ -52,8 +52,10 @@ void CoreJob::OnPended(std::unique_lock<std::mutex>&& lk){
// Need to ask the thread pool to handle our events again:
m_curEventInTeardown = false;

if (m_curEvent)
delete static_cast<std::future<void>*>(m_curEvent);
std::future<void>* future = static_cast<std::future<void>*>(std::atomic_exchange<void*>(&m_curEvent, nullptr));
Copy link
Contributor

Choose a reason for hiding this comment

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

fancy

Copy link
Contributor

@jdonald jdonald left a comment

Choose a reason for hiding this comment

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

This is awesome. Confirming our downstream tests have those random crashes fixed, whether on Windows, Mac, or Linux.

@jdonald jdonald merged commit 0ee2cc8 into master Oct 28, 2017
@jdonald jdonald deleted the fix-corejob-race branch October 28, 2017 04:45
@hanzou-x hanzou-x mentioned this pull request Oct 30, 2017
@jdonald jdonald added this to the v1.0.5 milestone Oct 31, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants