Skip to content

Commit

Permalink
Add a lock to avoid race condition on memoized_resource_estimate_.
Browse files Browse the repository at this point in the history
As the potential contention is very low, adding a lock here won't impact any performance.

PiperOrigin-RevId: 419902187
  • Loading branch information
shadowdragon89 authored and tensorflow-copybara committed Jan 5, 2022
1 parent 1683062 commit 83959e3
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions tensorflow_serving/core/simple_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ limitations under the License.
#include "tensorflow/core/lib/core/status.h"
#include "tensorflow/core/platform/macros.h"
#include "tensorflow/core/platform/mem.h"
#include "tensorflow/core/platform/mutex.h"
#include "tensorflow/core/platform/types.h"
#include "tensorflow_serving/core/loader.h"
#include "tensorflow_serving/core/source_adapter.h"
Expand Down Expand Up @@ -145,7 +146,9 @@ class SimpleLoader : public Loader {
absl::optional<ResourceEstimator> post_load_resource_estimator_;

// The memoized estimated resource requirement of the servable.
mutable absl::optional<ResourceAllocation> memoized_resource_estimate_;
mutable absl::optional<ResourceAllocation> memoized_resource_estimate_
TF_GUARDED_BY(memoized_resource_estimate_mu_);
mutable mutex memoized_resource_estimate_mu_;

std::unique_ptr<ResourceUtil> resource_util_;
Resource ram_resource_;
Expand Down Expand Up @@ -275,6 +278,7 @@ SimpleLoader<ServableType>::SimpleLoader(
template <typename ServableType>
Status SimpleLoader<ServableType>::EstimateResources(
ResourceAllocation* estimate) const {
mutex_lock l(memoized_resource_estimate_mu_);
if (memoized_resource_estimate_) {
*estimate = *memoized_resource_estimate_;
return Status::OK();
Expand Down Expand Up @@ -320,7 +324,10 @@ Status SimpleLoader<ServableType>::EstimateResourcesPostLoad() {
ResourceAllocation post_load_resource_estimate;
TF_RETURN_IF_ERROR(
(*post_load_resource_estimator_)(&post_load_resource_estimate));
memoized_resource_estimate_ = post_load_resource_estimate;
{
mutex_lock l(memoized_resource_estimate_mu_);
memoized_resource_estimate_ = post_load_resource_estimate;
}

// Release any transient memory used only during load to the OS.
const uint64_t during_load_ram_estimate = resource_util_->GetQuantity(
Expand Down

0 comments on commit 83959e3

Please sign in to comment.