From 8bc480994ce75866ec12f8894fc6b16faf530870 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 2 Feb 2026 01:17:24 +0800 Subject: [PATCH] remove disconnect idiom from HAL API the implementation is flawed - because multiple APIs hit a single server, the reference count can reach zeoro for a given interface, and trigger a disconnect, while another API is still using it. the long-term fix for this, if we wanted auto-disconnect, would be to wrap the connection query into a helper that uses an Arc to ensure the count is maintained across all possible interfaces. However, for the HAL, it's likely that pretty much every server is going to want this connection, and want it to stay around so - we'll just remove the auto-disconnect idiom for now and figure something out if we end up hitting a connection resource limit on a service that truly only needs the HAL connection early on in its lifecycle and never needs it again. --- libs/bao1x-api/src/iox.rs | 19 ------------------- libs/bao1x-api/src/keyboard.rs | 15 --------------- 2 files changed, 34 deletions(-) diff --git a/libs/bao1x-api/src/iox.rs b/libs/bao1x-api/src/iox.rs index bce11974e..62c0217f0 100644 --- a/libs/bao1x-api/src/iox.rs +++ b/libs/bao1x-api/src/iox.rs @@ -1,8 +1,3 @@ -#[cfg(feature = "std")] -use core::sync::atomic::{AtomicU32, Ordering}; -#[cfg(feature = "std")] -static REFCOUNT: AtomicU32 = AtomicU32::new(0); - #[cfg(feature = "std")] use num_traits::ToPrimitive; @@ -133,7 +128,6 @@ pub struct IoxHal { #[cfg(feature = "std")] impl IoxHal { pub fn new() -> Self { - REFCOUNT.fetch_add(1, Ordering::Relaxed); let xns = xous_names::XousNames::new().unwrap(); let conn = xns.request_connection(SERVER_NAME_BAO1X_HAL).expect("Couldn't connect to bao1x HAL server"); @@ -329,19 +323,6 @@ impl IoGpio for IoxHal { } } -#[cfg(feature = "std")] -impl Drop for IoxHal { - fn drop(&mut self) { - // de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the - // connection. - if REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 { - unsafe { - xous::disconnect(self.conn).ok(); - } - } - } -} - #[cfg(feature = "std")] impl IoIrq for IoxHal { fn set_irq_pin(&self, port: IoxPort, pin: u8, active: IoxValue, server: &str, opcode: usize) { diff --git a/libs/bao1x-api/src/keyboard.rs b/libs/bao1x-api/src/keyboard.rs index ea870dc60..b83b69495 100644 --- a/libs/bao1x-api/src/keyboard.rs +++ b/libs/bao1x-api/src/keyboard.rs @@ -121,7 +121,6 @@ pub struct Keyboard { } impl Keyboard { pub fn new(xns: &xous_names::XousNames) -> Result { - REFCOUNT.fetch_add(1, Ordering::Relaxed); let conn = xns.request_connection_blocking(crate::SERVER_NAME_KBD).expect("Can't connect to KBD"); Ok(Keyboard { conn }) } @@ -205,17 +204,3 @@ impl Keyboard { /// Reveal the connection ID for use with unsafe FFI calls pub fn conn(&self) -> xous::CID { self.conn } } - -use core::sync::atomic::{AtomicU32, Ordering}; -static REFCOUNT: AtomicU32 = AtomicU32::new(0); -impl Drop for Keyboard { - fn drop(&mut self) { - // now de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using - // the connection. - if REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 { - unsafe { - xous::disconnect(self.conn).unwrap(); - } - } - } -}