public inbox for yew-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Thomas Lamprecht <t.lamprecht@proxmox.com>
To: yew-devel@lists.proxmox.com
Subject: [PATCH 1/2] shared state: avoid borrow panics when removing listeners
Date: Wed,  9 Sep 2026 03:01:12 +0200	[thread overview]
Message-ID: <20260909010400.684482-2-t.lamprecht@proxmox.com> (raw)
In-Reply-To: <20260909010400.684482-1-t.lamprecht@proxmox.com>

Removing a listener drops its callback while the listener list is
mutably borrowed. If the callback owns another observer of the same
state, destroying it tries to remove that observer under a second
mutable borrow and panics.

Release the borrow before destroying the removed callback so observer
teardown can unregister captured observers or drop other values whose
destructors access the state.

Fixes: f117efc2c4ce ("add experimental shared state object")
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
---
 src/state/shared_state.rs | 35 ++++++++++++++++++++++++++++++++---
 1 file changed, 32 insertions(+), 3 deletions(-)

diff --git a/src/state/shared_state.rs b/src/state/shared_state.rs
index 2b5d7baf..d870db26 100644
--- a/src/state/shared_state.rs
+++ b/src/state/shared_state.rs
@@ -22,8 +22,8 @@ impl<T> SharedStateInner<T> {
         self.listeners.insert(cb)
     }
 
-    fn remove_listener(&mut self, key: usize) {
-        self.listeners.remove(key);
+    fn remove_listener(&mut self, key: usize) -> Callback<SharedState<T>> {
+        self.listeners.remove(key)
     }
 }
 
@@ -60,7 +60,10 @@ pub struct SharedStateObserver<T> {
 
 impl<T> Drop for SharedStateObserver<T> {
     fn drop(&mut self) {
-        self.inner.borrow_mut().remove_listener(self.key);
+        // Keep a named binding so the borrow ends before captured values are dropped. Using `let _`
+        // would drop the callback first, so destructors that borrow the state again could panic.
+        let callback = self.inner.borrow_mut().remove_listener(self.key);
+        drop(callback);
     }
 }
 
@@ -193,3 +196,29 @@ impl<T> Deref for SharedStateReadGuard<'_, T> {
         &self.borrowed_state
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use std::cell::Cell;
+
+    use super::*;
+
+    #[test]
+    fn removing_listener_can_drop_another_observer() {
+        let state = SharedState::new(());
+        let calls = Rc::new(Cell::new(0));
+        let first = state.add_listener({
+            let calls = Rc::clone(&calls);
+            move |_| calls.set(calls.get() + 1)
+        });
+        let second = state.add_listener(move |_| {
+            let _ = &first;
+        });
+
+        drop(state.write());
+        assert_eq!(calls.get(), 1);
+        drop(second);
+        drop(state.write());
+        assert_eq!(calls.get(), 1);
+    }
+}
-- 
2.47.3





  reply	other threads:[~2026-09-09  1:04 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-09  1:01 [PATCH 0/2] fix listener teardown and loader cancellation Thomas Lamprecht
2026-09-09  1:01 ` Thomas Lamprecht [this message]
2026-09-09  1:01 ` [PATCH 2/2] loader: fix stuck loading state and cancel loads without owners Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260909010400.684482-2-t.lamprecht@proxmox.com \
    --to=t.lamprecht@proxmox.com \
    --cc=yew-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal