update_engine: UM: UpdateManager removes EvaluationContext objects.
This fixes a situation where the destruction of an UpdateManager object
may leave a bunch of dangling main loop events due to delayed
evaluation, whose presence prevents the destruction of their
corresponding EvaluationContext objects. We solve this by registering
each EvaluationContext that's created for an async evaluation with the
UpdateManager, and storing a (weak) reverse callback in each
EvaluationContext for unregistering itself upon destruction. The
UpdateManager itself cares to unregister (i.e. remove any pending
events) for any outstanding EvaluationContexts that are still present
during its destruction. This also ensures that these objects are
properly destructed right after the destruction of the UpdateManager.
This CL also fixes a bug whereas removal of pending events might have
left a "live" callback handle inside the EvaluationContext, thus
creating a reference-count cycle and preventing the object from being
deallocated.
BUG=None
TEST=Unit tests.
Change-Id: I5b7f4b740241ed3a5f1831ae41fead0fc1481071
Reviewed-on: https://chromium-review.googlesource.com/211720
Tested-by: Gilad Arnold <garnold@chromium.org>
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
Commit-Queue: Gilad Arnold <garnold@chromium.org>
diff --git a/update_manager/evaluation_context.cc b/update_manager/evaluation_context.cc
index 04bc3ee..5bee226 100644
--- a/update_manager/evaluation_context.cc
+++ b/update_manager/evaluation_context.cc
@@ -14,6 +14,7 @@
#include "update_engine/utils.h"
+using base::Callback;
using base::Closure;
using base::Time;
using base::TimeDelta;
@@ -47,12 +48,15 @@
namespace chromeos_update_manager {
-EvaluationContext::EvaluationContext(ClockInterface* clock,
- TimeDelta evaluation_timeout,
- TimeDelta expiration_timeout)
+EvaluationContext::EvaluationContext(
+ ClockInterface* clock,
+ TimeDelta evaluation_timeout,
+ TimeDelta expiration_timeout,
+ scoped_ptr<Callback<void(EvaluationContext*)>> unregister_cb)
: clock_(clock),
evaluation_timeout_(evaluation_timeout),
expiration_timeout_(expiration_timeout),
+ unregister_cb_(unregister_cb.Pass()),
weak_ptr_factory_(this) {
ResetEvaluation();
ResetExpiration();
@@ -60,15 +64,19 @@
EvaluationContext::~EvaluationContext() {
RemoveObserversAndTimeout();
+ if (unregister_cb_.get())
+ unregister_cb_->Run(this);
}
-void EvaluationContext::RemoveObserversAndTimeout() {
+scoped_ptr<Closure> EvaluationContext::RemoveObserversAndTimeout() {
for (auto& it : value_cache_) {
if (it.first->GetMode() == kVariableModeAsync)
it.first->RemoveObserver(this);
}
CancelMainLoopEvent(timeout_event_);
timeout_event_ = kEventIdNull;
+
+ return scoped_ptr<Closure>(callback_.release());
}
TimeDelta EvaluationContext::RemainingTime(Time monotonic_deadline) const {
@@ -97,10 +105,8 @@
}
void EvaluationContext::OnValueChangedOrTimeout() {
- RemoveObserversAndTimeout();
-
// Copy the callback handle locally, allowing it to be reassigned.
- scoped_ptr<Closure> callback(callback_.release());
+ scoped_ptr<Closure> callback = RemoveObserversAndTimeout();
if (callback.get())
callback->Run();