From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id B0D461FF0C0 for ; Wed, 09 Sep 2026 03:04:10 +0200 (CEST) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 38172214DC; Wed, 09 Sep 2026 03:04:10 +0200 (CEST) From: Thomas Lamprecht 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 Message-ID: <20260909010400.684482-2-t.lamprecht@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260909010400.684482-1-t.lamprecht@proxmox.com> References: <20260909010400.684482-1-t.lamprecht@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1788915837235 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.852 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: MGD3XNMNPV27PTRDMZSF7ISXEGWVZIS6 X-Message-ID-Hash: MGD3XNMNPV27PTRDMZSF7ISXEGWVZIS6 X-MailFrom: t.lamprecht@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Yew framework devel list at Proxmox List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 --- 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 SharedStateInner { self.listeners.insert(cb) } - fn remove_listener(&mut self, key: usize) { - self.listeners.remove(key); + fn remove_listener(&mut self, key: usize) -> Callback> { + self.listeners.remove(key) } } @@ -60,7 +60,10 @@ pub struct SharedStateObserver { impl Drop for SharedStateObserver { 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 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