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.h b/update_manager/update_manager.h
index 2cd21d4..8cfd8f4 100644
--- a/update_manager/update_manager.h
+++ b/update_manager/update_manager.h
@@ -5,6 +5,7 @@
#ifndef UPDATE_ENGINE_UPDATE_MANAGER_UPDATE_MANAGER_H_
#define UPDATE_ENGINE_UPDATE_MANAGER_UPDATE_MANAGER_H_
+#include <set>
#include <string>
#include <base/callback.h>
@@ -14,11 +15,21 @@
#include "update_engine/clock_interface.h"
#include "update_engine/update_manager/default_policy.h"
+#include "update_engine/update_manager/evaluation_context.h"
#include "update_engine/update_manager/policy.h"
#include "update_engine/update_manager/state.h"
namespace chromeos_update_manager {
+// Comparator for scoped_refptr objects.
+template<typename T>
+struct ScopedRefPtrLess {
+ bool operator()(const scoped_refptr<T>& first,
+ const scoped_refptr<T>& second) const {
+ return first.get() < second.get();
+ }
+};
+
// The main Update Manager singleton class.
class UpdateManager {
public:
@@ -28,7 +39,7 @@
base::TimeDelta evaluation_timeout,
base::TimeDelta expiration_timeout, State* state);
- virtual ~UpdateManager() {}
+ virtual ~UpdateManager();
// PolicyRequest() evaluates the given policy with the provided arguments and
// returns the result. The |policy_method| is the pointer-to-method of the
@@ -111,6 +122,9 @@
Args...) const,
Args... args);
+ // Unregisters (removes from repo) a previously created EvaluationContext.
+ void UnregisterEvalContext(EvaluationContext* ec);
+
// The policy used by the UpdateManager. Note that since it is a const Policy,
// policy implementations are not allowed to persist state on this class.
scoped_ptr<const Policy> policy_;
@@ -131,6 +145,16 @@
// Timeout for expiration of the evaluation context, used for async requests.
const base::TimeDelta expiration_timeout_;
+ // Repository of previously created EvaluationContext objects. These are being
+ // unregistered (and the reference released) when the context is being
+ // destructed; alternatively, when the UpdateManager instance is destroyed, it
+ // will remove all pending events associated with all outstanding contexts
+ // (which should, in turn, trigger their destruction).
+ std::set<scoped_refptr<EvaluationContext>,
+ ScopedRefPtrLess<EvaluationContext>> ec_repo_;
+
+ base::WeakPtrFactory<UpdateManager> weak_ptr_factory_;
+
DISALLOW_COPY_AND_ASSIGN(UpdateManager);
};