facebook / facebook/folly

Call Future.wait() in other thread, throws folly::FutureInvalid: Future invalid, although the Future is valid!

Open
#1,966 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
30.5k
Forks
5.9k
PR merge metrics
No merged PRs in 30d

Description

My code:
```
void FollyIOPoolTask::Cancel() {
// call in thread A
if(!is_start_) {
return;
}
is_start_ = false;
LoggerHelper::LOG_D("FollyIOPoolTask::Cancel, before wait");
if(current_future_.valid()) {
current_future_.cancel();
current_future_.wait();
}
LoggerHelper::LOG_D("FollyIOPoolTask::Cancel, after wait");
}

void FollyIOPoolTask::Start(std::function callback) {
// call in thread B
is_start_ = true;
LoggerHelper::LOG_D("FollyIOPoolTask::task submit to pool!");
current_future_ = folly::via(folly::getGlobalIOExecutor()).then([=](auto&&) {
LoggerHelper::LOG_D("FollyIOPoolTask::task started!");
if(!is_start_) {
return;
}
callback();
LoggerHelper::LOG_D("FollyIOPoolTask::task finish!");
});
}
```
`current_future_` is a member of `FollyIOPoolTask`.
I call `Start()` in thread B, and then I call `Cancel()` in thread A. And it throws:
`terminating with uncaught exception of type folly::FutureInvalid: Future invalid`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.