From 484f68765c08531d505d5f103d6702377307dca6 Mon Sep 17 00:00:00 2001 From: Edward Oakes Date: Thu, 30 Apr 2020 18:55:54 -0500 Subject: [PATCH] Fix resource_ids_ data race (#8253) --- src/ray/core_worker/core_worker.cc | 12 ++++++++---- src/ray/core_worker/core_worker.h | 4 ++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/ray/core_worker/core_worker.cc b/src/ray/core_worker/core_worker.cc index d6a4a4965..4a6ec84ed 100644 --- a/src/ray/core_worker/core_worker.cc +++ b/src/ray/core_worker/core_worker.cc @@ -1352,6 +1352,11 @@ Status CoreWorker::GetActorHandle(const ActorID &actor_id, return Status::OK(); } +const ResourceMappingType CoreWorker::GetResourceIDs() const { + absl::MutexLock lock(&mutex_); + return *resource_ids_; +} + std::unique_ptr CoreWorker::CreateProfileEvent( const std::string &event_type) { return std::unique_ptr( @@ -1413,10 +1418,6 @@ Status CoreWorker::ExecuteTask(const TaskSpecification &task_spec, task_queue_length_ -= 1; num_executed_tasks_ += 1; - if (resource_ids != nullptr) { - resource_ids_ = resource_ids; - } - if (!options_.is_local_mode) { worker_context_.SetCurrentTask(task_spec); SetCurrentTaskId(task_spec.TaskId()); @@ -1424,6 +1425,9 @@ Status CoreWorker::ExecuteTask(const TaskSpecification &task_spec, { absl::MutexLock lock(&mutex_); current_task_ = task_spec; + if (resource_ids) { + resource_ids_ = resource_ids; + } } RayFunction func{task_spec.GetLanguage(), task_spec.FunctionDescriptor()}; diff --git a/src/ray/core_worker/core_worker.h b/src/ray/core_worker/core_worker.h index 97d1419a0..520a8bf6c 100644 --- a/src/ray/core_worker/core_worker.h +++ b/src/ray/core_worker/core_worker.h @@ -634,7 +634,7 @@ class CoreWorker : public rpc::CoreWorkerServiceHandler { const ActorID &GetActorId() const { return actor_id_; } // Get the resource IDs available to this worker (as assigned by the raylet). - const ResourceMappingType GetResourceIDs() const { return *resource_ids_; } + const ResourceMappingType GetResourceIDs() const; /// Create a profile event with a reference to the core worker's profiler. std::unique_ptr CreateProfileEvent(const std::string &event_type); @@ -1028,7 +1028,7 @@ class CoreWorker : public rpc::CoreWorkerServiceHandler { /// A map from resource name to the resource IDs that are currently reserved /// for this worker. Each pair consists of the resource ID and the fraction /// of that resource allocated for this worker. This is set on task assignment. - std::shared_ptr resource_ids_; + std::shared_ptr resource_ids_ GUARDED_BY(mutex_); // Interface that receives tasks from the raylet. std::unique_ptr raylet_task_receiver_;