@@ -849,6 +849,18 @@ pub struct OperatorNamespaceAllowlist {
849849}
850850
851851impl OperatorNamespaceAllowlist {
852+ fn read_guard ( & self ) -> std:: sync:: RwLockReadGuard < ' _ , BTreeSet < String > > {
853+ self . inner
854+ . read ( )
855+ . unwrap_or_else ( std:: sync:: PoisonError :: into_inner)
856+ }
857+
858+ fn write_guard ( & self ) -> std:: sync:: RwLockWriteGuard < ' _ , BTreeSet < String > > {
859+ self . inner
860+ . write ( )
861+ . unwrap_or_else ( std:: sync:: PoisonError :: into_inner)
862+ }
863+
852864 #[ must_use]
853865 pub fn new ( ) -> Self {
854866 Self {
@@ -865,44 +877,35 @@ impl OperatorNamespaceAllowlist {
865877
866878 /// Replace the entire allowlist (used by background watchers on refresh).
867879 pub fn replace ( & self , new_set : BTreeSet < String > ) {
868- let mut guard = self . inner . write ( ) . expect ( "allowlist lock poisoned" ) ;
880+ let mut guard = self . write_guard ( ) ;
869881 * guard = new_set;
870882 }
871883
872884 /// Merge additional namespaces into the allowlist.
873885 pub fn merge ( & self , additional : & BTreeSet < String > ) {
874- let mut guard = self . inner . write ( ) . expect ( "allowlist lock poisoned" ) ;
886+ let mut guard = self . write_guard ( ) ;
875887 guard. extend ( additional. iter ( ) . cloned ( ) ) ;
876888 }
877889
878890 /// Read the current allowlist snapshot.
879891 pub fn read ( & self ) -> std:: sync:: RwLockReadGuard < ' _ , BTreeSet < String > > {
880- self . inner . read ( ) . expect ( "allowlist lock poisoned" )
892+ self . read_guard ( )
881893 }
882894
883895 /// Check whether a namespace is in the allowlist.
884896 #[ must_use]
885897 pub fn contains ( & self , namespace : & str ) -> bool {
886- self . inner
887- . read ( )
888- . expect ( "allowlist lock poisoned" )
889- . contains ( namespace)
898+ self . read_guard ( ) . contains ( namespace)
890899 }
891900
892901 /// Insert a namespace into the allowlist. Returns `true` if it was new.
893902 pub fn insert ( & self , name : String ) -> bool {
894- self . inner
895- . write ( )
896- . expect ( "allowlist lock poisoned" )
897- . insert ( name)
903+ self . write_guard ( ) . insert ( name)
898904 }
899905
900906 /// Remove a namespace from the allowlist. Returns `true` if it was present.
901907 pub fn remove ( & self , name : & str ) -> bool {
902- self . inner
903- . write ( )
904- . expect ( "allowlist lock poisoned" )
905- . remove ( name)
908+ self . write_guard ( ) . remove ( name)
906909 }
907910
908911 /// Return a clone of the inner `Arc` for sharing with background tasks.
@@ -1674,6 +1677,18 @@ mod tests {
16741677 ) ;
16751678 }
16761679
1680+ #[ test]
1681+ fn namespace_for_workspace_operator_requires_allowlist ( ) {
1682+ let cfg = KubernetesComputeConfig {
1683+ workspace_mode : WorkspaceMode :: Operator ,
1684+ operator_namespace_label : Some ( "openshell.ai/workspace=true" . to_string ( ) ) ,
1685+ ..KubernetesComputeConfig :: default ( )
1686+ } ;
1687+
1688+ let err = cfg. namespace_for_workspace ( "prod" , None ) . unwrap_err ( ) ;
1689+ assert_eq ! ( err, "operator mode requires a namespace allowlist" ) ;
1690+ }
1691+
16771692 #[ test]
16781693 fn kube_resource_name_shared_prefixes_workspace ( ) {
16791694 let cfg = KubernetesComputeConfig :: default ( ) ;
@@ -1830,6 +1845,19 @@ mod tests {
18301845 cfg. validate_workspace_mode ( ) . unwrap ( ) ;
18311846 }
18321847
1848+ #[ test]
1849+ fn validate_workspace_mode_operator_rejects_label_and_file ( ) {
1850+ let cfg = KubernetesComputeConfig {
1851+ workspace_mode : WorkspaceMode :: Operator ,
1852+ operator_namespace_label : Some ( "openshell.ai/workspace=true" . to_string ( ) ) ,
1853+ operator_namespace_file : Some ( "/etc/openshell/namespaces.json" . to_string ( ) ) ,
1854+ ..KubernetesComputeConfig :: default ( )
1855+ } ;
1856+
1857+ let err = cfg. validate_workspace_mode ( ) . unwrap_err ( ) ;
1858+ assert ! ( err. contains( "not both" ) , "{err}" ) ;
1859+ }
1860+
18331861 #[ test]
18341862 fn dns_1123_label_validation ( ) {
18351863 assert ! ( is_dns_1123_label( "openshell" ) ) ;
@@ -1885,4 +1913,20 @@ mod tests {
18851913 al. replace ( BTreeSet :: new ( ) ) ;
18861914 assert ! ( !al. contains( "ns1" ) ) ;
18871915 }
1916+
1917+ #[ test]
1918+ fn operator_allowlist_recovers_from_poisoned_lock ( ) {
1919+ let al = OperatorNamespaceAllowlist :: from_set ( BTreeSet :: from ( [ "ns1" . to_string ( ) ] ) ) ;
1920+ let shared = al. shared ( ) ;
1921+ let _ = std:: thread:: spawn ( move || {
1922+ let _guard = shared. write ( ) . unwrap ( ) ;
1923+ panic ! ( "poison allowlist lock" ) ;
1924+ } )
1925+ . join ( ) ;
1926+
1927+ assert ! ( al. contains( "ns1" ) ) ;
1928+ assert ! ( al. insert( "ns2" . to_string( ) ) ) ;
1929+ assert ! ( al. read( ) . contains( "ns2" ) ) ;
1930+ assert ! ( al. remove( "ns1" ) ) ;
1931+ }
18881932}
0 commit comments