From e3f6351646d40a533be1cdce83d23134b8d16f23 Mon Sep 17 00:00:00 2001
From: rpm-build <rpm-build>
Date: Tue, 15 Aug 2023 19:13:14 +0800
Subject: [PATCH] Fix glib assertions due to asynchronous metadata action
 completing after shutdown begins

---
 daemons/controld/controld_cib.c         | 11 +++++++++--
 daemons/controld/controld_execd.c       |  4 +++-
 daemons/controld/controld_execd_state.c | 15 +++++++--------
 daemons/controld/controld_lrm.h         |  5 -----
 4 files changed, 19 insertions(+), 16 deletions(-)

diff --git a/daemons/controld/controld_cib.c b/daemons/controld/controld_cib.c
index 94b99dd..43cf857 100644
--- a/daemons/controld/controld_cib.c
+++ b/daemons/controld/controld_cib.c
@@ -841,10 +841,17 @@ cib_rsc_callback(xmlNode * msg, int call_id, int rc, xmlNode * output, void *use
         case pcmk_ok:
         case -pcmk_err_diff_failed:
         case -pcmk_err_diff_resync:
-            crm_trace("Resource update %d complete: rc=%d", call_id, rc);
+	    crm_trace("Resource history update completed (call=%d rc=%d)",
+                      call_id, rc);
             break;
         default:
-            crm_warn("Resource update %d failed: (rc=%d) %s", call_id, rc, pcmk_strerror(rc));
+	    if (call_id > 0) {
+                crm_warn("Resource history update %d failed: %s "
+                         CRM_XS " rc=%d", call_id, pcmk_strerror(rc), rc);
+            } else {
+                crm_warn("Resource history update failed: %s " CRM_XS " rc=%d",
+                         pcmk_strerror(rc), rc);
+            }
     }
 
     if (call_id == pending_rsc_update) {
diff --git a/daemons/controld/controld_execd.c b/daemons/controld/controld_execd.c
index 0de399c..f37446b 100644
--- a/daemons/controld/controld_execd.c
+++ b/daemons/controld/controld_execd.c
@@ -1394,7 +1394,9 @@ metadata_complete(int pid, const pcmk__action_result_t *result, void *user_data)
         md = controld_cache_metadata(lrm_state->metadata_cache, data->rsc,
                                      result->action_stdout);
     }
-    do_lrm_rsc_op(lrm_state, data->rsc, data->input_xml, md);
+    if (!pcmk_is_set(controld_globals.fsa_input_register, R_HA_DISCONNECTED)) {
+        do_lrm_rsc_op(lrm_state, data->rsc, data->input_xml, md);
+    }
     free_metadata_cb_data(data);
 }
 
diff --git a/daemons/controld/controld_execd_state.c b/daemons/controld/controld_execd_state.c
index 8c68bfc..2411297 100644
--- a/daemons/controld/controld_execd_state.c
+++ b/daemons/controld/controld_execd_state.c
@@ -132,12 +132,6 @@ lrm_state_create(const char *node_name)
     return state;
 }
 
-void
-lrm_state_destroy(const char *node_name)
-{
-    g_hash_table_remove(lrm_state_table, node_name);
-}
-
 static gboolean
 remote_proxy_remove_by_node(gpointer key, gpointer value, gpointer user_data)
 {
@@ -307,7 +301,7 @@ lrm_state_destroy_all(void)
 lrm_state_t *
 lrm_state_find(const char *node_name)
 {
-    if (!node_name) {
+    if ((node_name == NULL) || (lrm_state_table == NULL)) {
         return NULL;
     }
     return g_hash_table_lookup(lrm_state_table, node_name);
@@ -318,6 +312,8 @@ lrm_state_find_or_create(const char *node_name)
 {
     lrm_state_t *lrm_state;
 
+    CRM_CHECK(lrm_state_table != NULL, return NULL);
+
     lrm_state = g_hash_table_lookup(lrm_state_table, node_name);
     if (!lrm_state) {
         lrm_state = lrm_state_create(node_name);
@@ -329,6 +325,9 @@ lrm_state_find_or_create(const char *node_name)
 GList *
 lrm_state_get_list(void)
 {
+    if (lrm_state_table == NULL) {
+        return NULL;
+    }
     return g_hash_table_get_values(lrm_state_table);
 }
 
@@ -799,7 +798,7 @@ lrm_state_unregister_rsc(lrm_state_t * lrm_state,
     }
 
     if (is_remote_lrmd_ra(NULL, NULL, rsc_id)) {
-        lrm_state_destroy(rsc_id);
+	g_hash_table_remove(lrm_state_table, rsc_id);
         return pcmk_ok;
     }
 
diff --git a/daemons/controld/controld_lrm.h b/daemons/controld/controld_lrm.h
index 25f3db3..c3113e4 100644
--- a/daemons/controld/controld_lrm.h
+++ b/daemons/controld/controld_lrm.h
@@ -108,11 +108,6 @@ gboolean lrm_state_init_local(void);
  */
 void lrm_state_destroy_all(void);
 
-/*!
- * \brief Destroy executor connection by node name
- */
-void lrm_state_destroy(const char *node_name);
-
 /*!
  * \brief Find lrm_state data by node name
  */
-- 
2.41.0

