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/update_manager.cc b/update_manager/update_manager.cc
index fb3800c..13c208e 100644
--- a/update_manager/update_manager.cc
+++ b/update_manager/update_manager.cc
@@ -14,10 +14,25 @@
base::TimeDelta expiration_timeout, State* state)
: default_policy_(clock), state_(state), clock_(clock),
evaluation_timeout_(evaluation_timeout),
- expiration_timeout_(expiration_timeout) {
+ expiration_timeout_(expiration_timeout),
+ weak_ptr_factory_(this) {
// TODO(deymo): Make it possible to replace this policy with a different
// implementation with a build-time flag.
policy_.reset(new ChromeOSPolicy());
}
+UpdateManager::~UpdateManager() {
+ // Remove pending main loop events associated with any of the outstanding
+ // evaluation contexts. This will prevent dangling pending events, causing
+ // these contexts to be destructed once the repo itself is destructed.
+ for (auto& ec : ec_repo_)
+ ec->RemoveObserversAndTimeout();
+}
+
+void UpdateManager::UnregisterEvalContext(EvaluationContext* ec) {
+ if (!ec_repo_.erase(ec)) {
+ LOG(ERROR) << "Unregistering an unknown evaluation context, this is a bug.";
+ }
+}
+
} // namespace chromeos_update_manager