message: Only set last_graph_ after the run calls finish. Otherwise subsequent calls will fail to reset the session and may collide with the timed-out run still alive on the background thread.
diff: @@ -145,7 +145,6 @@ Status SingleMachine::Run(const GraphDef& graph_def,
  const std::vector<std::pair<string, Tensor>>& feed,
  const std::vector<string>& fetch,
  RunMetadata* metadata) {
  - {
  mutex_lock l(this->last_graph_mu_);
  if (last_graph_ != &graph_def) {
  TF_RETURN_IF_ERROR(ResetSession());
  @@ -175,10 +174,9 @@ Status SingleMachine::Run(const GraphDef& graph_def,
  TF_RETURN_IF_ERROR(QueueRunner::New(queue_runner_defs_[i],
  coordinator_.get(), &queue_runner));
  - TF_RETURN_IF_ERROR(queue_runner->StartAndCollectCostGraph(
  - session_.get(), queue_options));
  - TF_RETURN_IF_ERROR(
  - coordinator_->RegisterRunner(std::move(queue_runner)));
  + TF_RETURN_IF_ERROR(queue_runner->StartAndCollectCostGraph(session_.get(),
  + queue_options));
  + TF_RETURN_IF_ERROR(coordinator_->RegisterRunner(std::move(queue_runner)));
  TF_RETURN_IF_ERROR(coordinator_->GetStatus());
  }
  @@ -187,9 +185,6 @@ Status SingleMachine::Run(const GraphDef& graph_def,
  i < options_.config.graph_options().build_cost_model_after(); ++i) {
  TF_RETURN_IF_ERROR(RunWithTimeout(feed, fetch, nullptr));
  }
  -
  - last_graph_ = &graph_def;
  - }
  }
  if (metadata) {
  @@ -200,8 +195,11 @@ Status SingleMachine::Run(const GraphDef& graph_def,
  MergeCosts(metadata->mutable_cost_graph(), init_metadata_.cost_graph(),
  queue_costs);
  } else {
  - return RunWithTimeout(feed, fetch, nullptr);
  + TF_RETURN_IF_ERROR(RunWithTimeout(feed, fetch, nullptr));
  }
  +
  + last_graph_ = &graph_def;
  +
  return Status::OK();
  }
  