From 59c04bfb254b8b6b328b0ea753c5881eba46e785 Mon Sep 17 00:00:00 2001 From: Oussama Teffahi Date: Tue, 10 Dec 2024 15:40:43 +0100 Subject: [PATCH 1/4] Fix deadlock --- zenoh/src/api/session.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/zenoh/src/api/session.rs b/zenoh/src/api/session.rs index 15b7fb4d7..21144a4c7 100644 --- a/zenoh/src/api/session.rs +++ b/zenoh/src/api/session.rs @@ -1574,6 +1574,8 @@ impl SessionInner { }, }), }); + } else { + drop(state); } } else { drop(state); From 86fcca028cc12c098404ab3019f84c9805b7e544 Mon Sep 17 00:00:00 2001 From: Oussama Teffahi Date: Tue, 10 Dec 2024 15:53:37 +0100 Subject: [PATCH 2/4] Only update matching listener status for last remote subscriber --- zenoh/src/api/session.rs | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/zenoh/src/api/session.rs b/zenoh/src/api/session.rs index 21144a4c7..a1df20f31 100644 --- a/zenoh/src/api/session.rs +++ b/zenoh/src/api/session.rs @@ -1574,21 +1574,31 @@ impl SessionInner { }, }), }); + #[cfg(feature = "unstable")] + { + let state = zread!(self.state); + self.update_matching_status( + &state, + &sub_state.key_expr, + MatchingStatusType::Subscribers, + false, + ) + } } else { drop(state); } } else { drop(state); - } - #[cfg(feature = "unstable")] - { - let state = zread!(self.state); - self.update_matching_status( - &state, - &sub_state.key_expr, - MatchingStatusType::Subscribers, - false, - ) + #[cfg(feature = "unstable")] + { + let state = zread!(self.state); + self.update_matching_status( + &state, + &sub_state.key_expr, + MatchingStatusType::Subscribers, + false, + ) + } } } SubscriberKind::LivelinessSubscriber => { From 80a740f7c8968f9a5eb45c358c2c6b15a5e8087f Mon Sep 17 00:00:00 2001 From: Oussama Teffahi Date: Tue, 10 Dec 2024 16:10:47 +0100 Subject: [PATCH 3/4] Remove unnecessary drop after changes --- zenoh/src/api/session.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/zenoh/src/api/session.rs b/zenoh/src/api/session.rs index a1df20f31..013536e54 100644 --- a/zenoh/src/api/session.rs +++ b/zenoh/src/api/session.rs @@ -1584,8 +1584,6 @@ impl SessionInner { false, ) } - } else { - drop(state); } } else { drop(state); From f90245a3f9136a57d6e4e24f2e222944dd4af20c Mon Sep 17 00:00:00 2001 From: Oussama Teffahi Date: Tue, 10 Dec 2024 16:36:59 +0100 Subject: [PATCH 4/4] Add unit test for undeclare deadlock --- zenoh/tests/session.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/zenoh/tests/session.rs b/zenoh/tests/session.rs index 3e84249b8..5a1837844 100644 --- a/zenoh/tests/session.rs +++ b/zenoh/tests/session.rs @@ -430,3 +430,15 @@ async fn zenoh_session_close_in_background() { }; ztimeout!(close_all); } + +#[cfg(feature = "unstable")] +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn test_undeclare_subscribers_same_keyexpr() { + let key_expr = "test/undeclare/subscribers"; + let session = zenoh::open(zenoh::Config::default()).await.unwrap(); + let sub1 = session.declare_subscriber(key_expr).await.unwrap(); + let sub2 = session.declare_subscriber(key_expr).await.unwrap(); + tokio::time::sleep(SLEEP).await; + ztimeout!(sub1.undeclare()).unwrap(); + ztimeout!(sub2.undeclare()).unwrap(); +}