From: Andrii Grynenko Date: Mon, 16 May 2016 21:06:54 +0000 (-0700) Subject: Fix a race in Promise::setTry X-Git-Tag: 2016.07.26~230 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=87e5b4078f62e696ed901d64f0242ebc5fc5e62c;p=folly.git Fix a race in Promise::setTry Summary: This fixes a race which can only be exposed if Promise is owned by the same thread which was calling await and Promise is fulfilled from a different thread. What could happen then was: 1. Thread 2 fulfills the promise and thus saves the value and posts Baton. 2. Thread 1 wakes up, await returns and then Thread 1 destroys the Promise. 3. Promise destructor checks value_, which is still not null, so it tries to fulfil it with exception. Reviewed By: spalamarchuk Differential Revision: D3306969 fbshipit-source-id: adf799cfd7b75b0201fa675a9e44ac7c7c42ac95 --- diff --git a/folly/experimental/fibers/Promise-inl.h b/folly/experimental/fibers/Promise-inl.h index 95565d72..108a2ac4 100644 --- a/folly/experimental/fibers/Promise-inl.h +++ b/folly/experimental/fibers/Promise-inl.h @@ -61,9 +61,10 @@ void Promise::setTry(folly::Try&& t) { throwIfFulfilled(); *value_ = std::move(t); + value_ = nullptr; + baton_->post(); - value_ = nullptr; baton_ = nullptr; }