@@ -729,6 +729,18 @@ pub struct OperatorNamespaceAllowlist {
729729}
730730
731731impl OperatorNamespaceAllowlist {
732+ fn read_guard ( & self ) -> std:: sync:: RwLockReadGuard < ' _ , BTreeSet < String > > {
733+ self . inner
734+ . read ( )
735+ . unwrap_or_else ( std:: sync:: PoisonError :: into_inner)
736+ }
737+
738+ fn write_guard ( & self ) -> std:: sync:: RwLockWriteGuard < ' _ , BTreeSet < String > > {
739+ self . inner
740+ . write ( )
741+ . unwrap_or_else ( std:: sync:: PoisonError :: into_inner)
742+ }
743+
732744 #[ must_use]
733745 pub fn new ( ) -> Self {
734746 Self {
@@ -745,44 +757,35 @@ impl OperatorNamespaceAllowlist {
745757
746758 /// Replace the entire allowlist (used by background watchers on refresh).
747759 pub fn replace ( & self , new_set : BTreeSet < String > ) {
748- let mut guard = self . inner . write ( ) . expect ( "allowlist lock poisoned" ) ;
760+ let mut guard = self . write_guard ( ) ;
749761 * guard = new_set;
750762 }
751763
752764 /// Merge additional namespaces into the allowlist.
753765 pub fn merge ( & self , additional : & BTreeSet < String > ) {
754- let mut guard = self . inner . write ( ) . expect ( "allowlist lock poisoned" ) ;
766+ let mut guard = self . write_guard ( ) ;
755767 guard. extend ( additional. iter ( ) . cloned ( ) ) ;
756768 }
757769
758770 /// Read the current allowlist snapshot.
759771 pub fn read ( & self ) -> std:: sync:: RwLockReadGuard < ' _ , BTreeSet < String > > {
760- self . inner . read ( ) . expect ( "allowlist lock poisoned" )
772+ self . read_guard ( )
761773 }
762774
763775 /// Check whether a namespace is in the allowlist.
764776 #[ must_use]
765777 pub fn contains ( & self , namespace : & str ) -> bool {
766- self . inner
767- . read ( )
768- . expect ( "allowlist lock poisoned" )
769- . contains ( namespace)
778+ self . read_guard ( ) . contains ( namespace)
770779 }
771780
772781 /// Insert a namespace into the allowlist. Returns `true` if it was new.
773782 pub fn insert ( & self , name : String ) -> bool {
774- self . inner
775- . write ( )
776- . expect ( "allowlist lock poisoned" )
777- . insert ( name)
783+ self . write_guard ( ) . insert ( name)
778784 }
779785
780786 /// Remove a namespace from the allowlist. Returns `true` if it was present.
781787 pub fn remove ( & self , name : & str ) -> bool {
782- self . inner
783- . write ( )
784- . expect ( "allowlist lock poisoned" )
785- . remove ( name)
788+ self . write_guard ( ) . remove ( name)
786789 }
787790
788791 /// Return a clone of the inner `Arc` for sharing with background tasks.
@@ -1379,6 +1382,18 @@ mod tests {
13791382 ) ;
13801383 }
13811384
1385+ #[ test]
1386+ fn namespace_for_workspace_operator_requires_allowlist ( ) {
1387+ let cfg = KubernetesComputeConfig {
1388+ workspace_mode : WorkspaceMode :: Operator ,
1389+ operator_namespace_label : Some ( "openshell.ai/workspace=true" . to_string ( ) ) ,
1390+ ..KubernetesComputeConfig :: default ( )
1391+ } ;
1392+
1393+ let err = cfg. namespace_for_workspace ( "prod" , None ) . unwrap_err ( ) ;
1394+ assert_eq ! ( err, "operator mode requires a namespace allowlist" ) ;
1395+ }
1396+
13821397 #[ test]
13831398 fn kube_resource_name_shared_prefixes_workspace ( ) {
13841399 let cfg = KubernetesComputeConfig :: default ( ) ;
@@ -1535,6 +1550,19 @@ mod tests {
15351550 cfg. validate_workspace_mode ( ) . unwrap ( ) ;
15361551 }
15371552
1553+ #[ test]
1554+ fn validate_workspace_mode_operator_rejects_label_and_file ( ) {
1555+ let cfg = KubernetesComputeConfig {
1556+ workspace_mode : WorkspaceMode :: Operator ,
1557+ operator_namespace_label : Some ( "openshell.ai/workspace=true" . to_string ( ) ) ,
1558+ operator_namespace_file : Some ( "/etc/openshell/namespaces.json" . to_string ( ) ) ,
1559+ ..KubernetesComputeConfig :: default ( )
1560+ } ;
1561+
1562+ let err = cfg. validate_workspace_mode ( ) . unwrap_err ( ) ;
1563+ assert ! ( err. contains( "not both" ) , "{err}" ) ;
1564+ }
1565+
15381566 #[ test]
15391567 fn dns_1123_label_validation ( ) {
15401568 assert ! ( is_dns_1123_label( "openshell" ) ) ;
@@ -1590,4 +1618,20 @@ mod tests {
15901618 al. replace ( BTreeSet :: new ( ) ) ;
15911619 assert ! ( !al. contains( "ns1" ) ) ;
15921620 }
1621+
1622+ #[ test]
1623+ fn operator_allowlist_recovers_from_poisoned_lock ( ) {
1624+ let al = OperatorNamespaceAllowlist :: from_set ( BTreeSet :: from ( [ "ns1" . to_string ( ) ] ) ) ;
1625+ let shared = al. shared ( ) ;
1626+ let _ = std:: thread:: spawn ( move || {
1627+ let _guard = shared. write ( ) . unwrap ( ) ;
1628+ panic ! ( "poison allowlist lock" ) ;
1629+ } )
1630+ . join ( ) ;
1631+
1632+ assert ! ( al. contains( "ns1" ) ) ;
1633+ assert ! ( al. insert( "ns2" . to_string( ) ) ) ;
1634+ assert ! ( al. read( ) . contains( "ns2" ) ) ;
1635+ assert ! ( al. remove( "ns1" ) ) ;
1636+ }
15931637}
0 commit comments