2aacef
From 82e78c509c9eab33c1dbf2bc445f91b67b2f118f Mon Sep 17 00:00:00 2001
2aacef
From: Yu Watanabe <watanabe.yu+github@gmail.com>
2aacef
Date: Wed, 16 Nov 2022 03:08:22 +0900
2aacef
Subject: [PATCH] core/unit: drop dependency to the unit being merged
2aacef
2aacef
Fixes a bug in 15ed3c3a188cf7fa5a60ae508fc7a3ed048d2220.
2aacef
2aacef
Fixes #24990. Also, hopefully fixes #24577.
2aacef
2aacef
(cherry picked from commit c8b3b524134539846917269ddd644ee93a35623f)
2aacef
2aacef
Related: #2160477
2aacef
---
2aacef
 src/core/unit.c | 21 +++++++++++++--------
2aacef
 1 file changed, 13 insertions(+), 8 deletions(-)
2aacef
2aacef
diff --git a/src/core/unit.c b/src/core/unit.c
2aacef
index 4cde5c4b4e..ae9f688ad2 100644
2aacef
--- a/src/core/unit.c
2aacef
+++ b/src/core/unit.c
2aacef
@@ -1043,10 +1043,10 @@ static int unit_add_dependency_hashmap(
2aacef
         return unit_per_dependency_type_hashmap_update(per_type, other, origin_mask, destination_mask);
2aacef
 }
2aacef
 
2aacef
-static void unit_merge_dependencies(
2aacef
-                Unit *u,
2aacef
-                Unit *other) {
2aacef
-
2aacef
+static void unit_merge_dependencies(Unit *u, Unit *other) {
2aacef
+        Hashmap *deps;
2aacef
+        void *dt; /* Actually of type UnitDependency, except that we don't bother casting it here,
2aacef
+                   * since the hashmaps all want it as void pointer. */
2aacef
         int r;
2aacef
 
2aacef
         assert(u);
2aacef
@@ -1055,12 +1055,19 @@ static void unit_merge_dependencies(
2aacef
         if (u == other)
2aacef
                 return;
2aacef
 
2aacef
+        /* First, remove dependency to other. */
2aacef
+        HASHMAP_FOREACH_KEY(deps, dt, u->dependencies) {
2aacef
+                if (hashmap_remove(deps, other))
2aacef
+                        unit_maybe_warn_about_dependency(u, other->id, UNIT_DEPENDENCY_FROM_PTR(dt));
2aacef
+
2aacef
+                if (hashmap_isempty(deps))
2aacef
+                        hashmap_free(hashmap_remove(u->dependencies, dt));
2aacef
+        }
2aacef
+
2aacef
         for (;;) {
2aacef
                 _cleanup_(hashmap_freep) Hashmap *other_deps = NULL;
2aacef
                 UnitDependencyInfo di_back;
2aacef
                 Unit *back;
2aacef
-                void *dt; /* Actually of type UnitDependency, except that we don't bother casting it here,
2aacef
-                           * since the hashmaps all want it as void pointer. */
2aacef
 
2aacef
                 /* Let's focus on one dependency type at a time, that 'other' has defined. */
2aacef
                 other_deps = hashmap_steal_first_key_and_value(other->dependencies, &dt);
2aacef
@@ -1102,8 +1109,6 @@ static void unit_merge_dependencies(
2aacef
                  * them per type wholesale. */
2aacef
                 r = hashmap_put(u->dependencies, dt, other_deps);
2aacef
                 if (r == -EEXIST) {
2aacef
-                        Hashmap *deps;
2aacef
-
2aacef
                         /* The target unit already has dependencies of this type, let's then merge this individually. */
2aacef
 
2aacef
                         assert_se(deps = hashmap_get(u->dependencies, dt));