Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Fix race condition in NaiveEngine::PushAsync #19108

Merged
merged 2 commits into from
Sep 11, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/engine/naive_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
* \file naive_engine.cc
* \brief Implementation of NaiveEngine
*/
#include <memory>
#include <vector>
#include <atomic>
#include <future>
#include <memory>
#include <thread>
#include <vector>
#include "./engine_impl.h"
#include "../profiler/profiler.h"
#include "./openmp.h"
Expand Down Expand Up @@ -159,9 +160,10 @@ class NaiveEngine final : public Engine {
int priority = 0,
const char* opr_name = nullptr,
bool wait = false) override {
bool req_completed = false;
std::promise<void> promise;
std::future<void> future = promise.get_future();
CallbackOnComplete callback = CreateCallback(
NaiveEngine::OnComplete, &req_completed);
NaiveEngine::OnComplete, &promise);
profiler::Profiler *profiler = profiler::Profiler::Get();
auto opr_deleter = [this](NaiveOpr* p) {
this->DeleteOperator(p);
Expand Down Expand Up @@ -204,12 +206,11 @@ class NaiveEngine final : public Engine {
} else {
exec_fun(RunContext{exec_ctx, &cpu_stream_, nullptr, false}, callback);
}
future.wait();
// increment mutable var version
for (auto var : mutable_vars) {
++var->version_;
}
CHECK(req_completed)
<< "NaiveEngine only support synchronize Push so far";
if (profiling) {
opr->opr_profile->stop();
}
Expand Down Expand Up @@ -241,8 +242,7 @@ class NaiveEngine final : public Engine {
// callback to oncomplete
static void OnComplete(Engine *engine, void *param,
const dmlc::Error* error) {
bool *req_completed = static_cast<bool*>(param);
*req_completed = true;
static_cast<std::promise<void>*>(param)->set_value();
}
/*! \brief whether it is during shutdown phase*/
std::atomic<bool> shutdown_phase_{false};
Expand Down