From 419a2ed3ed3ec46b7863afab248a35b7be7f1a57 Mon Sep 17 00:00:00 2001
From: Ali Mohammed <ali.mohammed@hpe.com>
Date: Fri, 27 Jan 2023 19:08:10 +0100
Subject: [PATCH] retry if we fail to add a pending entry

---
 maestro/pool_client.c           | 11 ++++++++++-
 maestro/pool_manager_registry.c |  1 +
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/maestro/pool_client.c b/maestro/pool_client.c
index a82b78ee..288a0aaa 100644
--- a/maestro/pool_client.c
+++ b/maestro/pool_client.c
@@ -871,8 +871,17 @@ mstro_pc__app_befriend(mstro_pool_operation op)
     DEBUG("Unknown app %zu, let's make friends\n", appid);
     /* mark that this app entry is being read now for others */
 		status = mstro_pc_app_register_pending(appid);
+    /*check status */
+    if(status == MSTRO_FAIL)
+    {
+      /* we could not put a pending entry, 
+       * probably someone else was faster than us
+       * We should try this operation step again
+       * returning wouldblock so that op engine would retry later*/
+      return MSTRO_WOULDBLOCK;
+    }
+
     /**Submit read */
-    
     /* we need to be careful to not pass in any stack-local refs */
     Mstro__AppInfo *dst_epd=NULL;
     status=mstro_appinfo_deserialize(op->pc_transport.target_serialized_endpoint, &dst_epd);
diff --git a/maestro/pool_manager_registry.c b/maestro/pool_manager_registry.c
index b0d3d3b5..96eaecd5 100644
--- a/maestro/pool_manager_registry.c
+++ b/maestro/pool_manager_registry.c
@@ -257,6 +257,7 @@ mstro_pc_app_register_pending(mstro_app_id id)
         /**check there is no other entry before*/
         HASH_FIND(hh, g_mstro_pm_app_registry, &id, sizeof(mstro_app_id), elt);
         if((elt!=NULL) && (elt->pending)) {
+          //FIXME should be a warning as we can handle this 
           ERR("There is alreading a pending entry for app %"PRIappid", should not overwrite \n", id);
           return MSTRO_FAIL;
         }
-- 
GitLab