From c63e5984979f55ae25059a1bb0ccc83fc64ea52a Mon Sep 17 00:00:00 2001 From: Eric Frias Date: Mon, 14 Apr 2014 16:27:08 -0400 Subject: [PATCH] Fix a race that occurred when notifying a thread that was blocked on a promise to wake up, but that thread simultaneously awoke for another reason (probably a timeout) --- src/thread/future.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/thread/future.cpp b/src/thread/future.cpp index ebf68a6..7783834 100644 --- a/src/thread/future.cpp +++ b/src/thread/future.cpp @@ -79,8 +79,15 @@ namespace fc { #endif } void promise_base::_notify(){ - if( _blocked_thread != nullptr ) - _blocked_thread->notify(ptr(this,true)); + // copy _blocked_thread into a local so that if the thread unblocks (e.g., + // because of a timeout) before we get a chance to notify it, we won't be + // calling notify on a null pointer + thread* blocked_thread; + { synchronized(_spin_yield) + blocked_thread = _blocked_thread; + } + if( blocked_thread ) + blocked_thread->notify(ptr(this,true)); } promise_base::~promise_base() { } void promise_base::_set_timeout(){