From 102cbf0d0c88159bac8908bb7aaa22a1f590a2f6 Mon Sep 17 00:00:00 2001
From: Robert Schmidt <robert.schmidt@openairinterface.org>
Date: Fri, 3 Nov 2023 13:54:36 +0100
Subject: [PATCH] Prevent possible deadlock between scheduler and F1 UE ID

---
 openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_primitives.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_primitives.c b/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_primitives.c
index 7c4322f654..501eb963c0 100644
--- a/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_primitives.c
+++ b/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_primitives.c
@@ -2822,6 +2822,9 @@ void nr_mac_release_ue(gNB_MAC_INST *mac, int rnti)
   // MAC context, decode the PDU, find the C-RNTI MAC CE, and then throw the
   // newly created context away. See also in _nr_rx_sdu() and commit 93f59a3c6e56f
   if (du_exists_f1_ue_data(rnti)) {
+    // unlock the scheduler temporarily to prevent possible deadlocks with
+    // du_remove_f1_ue_data() (and also while sending the message to RRC)
+    NR_SCHED_UNLOCK(&mac->sched_lock);
     f1_ue_data_t ue_data = du_get_f1_ue_data(rnti);
     f1ap_ue_context_release_complete_t complete = {
       .gNB_CU_ue_id = ue_data.secondary_ue,
@@ -2830,6 +2833,7 @@ void nr_mac_release_ue(gNB_MAC_INST *mac, int rnti)
     mac->mac_rrc.ue_context_release_complete(&complete);
 
     du_remove_f1_ue_data(rnti);
+    NR_SCHED_LOCK(&mac->sched_lock);
   }
 }
 
-- 
2.26.2