@@ -22,6 +22,8 @@ pub(crate) struct OfficialWorker {
2222struct Shared {
2323 state : Mutex < State > ,
2424 changed : Condvar ,
25+ /// Told after each published status, outside the state lock.
26+ waker : Mutex < Option < Arc < dyn Fn ( ) + Send + Sync > > > ,
2527}
2628struct State {
2729 snapshot : StatusSnapshot ,
@@ -75,6 +77,15 @@ impl Shared {
7577 state. candidate_identity = record. candidate . as_ref ( ) . map ( |p| p. identity . clone ( ) ) ;
7678 state. busy = false ;
7779 self . changed . notify_all ( ) ;
80+ drop ( state) ;
81+ let waker = self
82+ . waker
83+ . lock ( )
84+ . unwrap_or_else ( |poisoned| poisoned. into_inner ( ) )
85+ . clone ( ) ;
86+ if let Some ( wake) = waker {
87+ wake ( ) ;
88+ }
7889 }
7990}
8091
@@ -156,6 +167,7 @@ impl OfficialWorker {
156167 repairs : 0 ,
157168 } ) ,
158169 changed : Condvar :: new ( ) ,
170+ waker : Mutex :: new ( None ) ,
159171 } ) ;
160172 let ( sender, receiver) = mpsc:: sync_channel ( 2 ) ;
161173 let ( cancel, mut cancelled) = watch:: channel ( false ) ;
@@ -531,6 +543,13 @@ impl OfficialWorker {
531543 state. busy = true ;
532544 Ok ( ( ) )
533545 }
546+ pub ( crate ) fn set_change_waker ( & self , wake : Arc < dyn Fn ( ) + Send + Sync > ) {
547+ * self
548+ . shared
549+ . waker
550+ . lock ( )
551+ . unwrap_or_else ( |poisoned| poisoned. into_inner ( ) ) = Some ( wake) ;
552+ }
534553 pub ( crate ) fn wait_for_change ( & self , revision : u64 , timeout : Duration ) -> WaitForStatus {
535554 let state = self . shared . lock ( ) ;
536555 let ( state, _) = self
@@ -887,6 +906,45 @@ mod tests {
887906 . unwrap ( )
888907 }
889908
909+ #[ test]
910+ fn each_published_status_wakes_the_registered_caller_outside_the_lock ( ) {
911+ let ( _dir, store, _package) = fixture ( ) ;
912+ let shared = Arc :: new ( Shared {
913+ state : Mutex :: new ( State {
914+ snapshot : StatusSnapshot {
915+ revision : 1 ,
916+ last_refresh_attempt_unix : None ,
917+ status : UpdateStatus :: Shutdown ,
918+ } ,
919+ initial : None ,
920+ candidate : None ,
921+ candidate_identity : None ,
922+ due : 0 ,
923+ verification : 0 ,
924+ freshness_deadline : 0 ,
925+ busy : false ,
926+ sealed : false ,
927+ operation : 0 ,
928+ repairs : 0 ,
929+ } ) ,
930+ changed : Condvar :: new ( ) ,
931+ waker : Mutex :: new ( None ) ,
932+ } ) ;
933+ // Nothing is registered yet: publishing must not need a waker.
934+ shared. publish ( & store. record , UpdateStatus :: Shutdown ) ;
935+ let wakes = Arc :: new ( std:: sync:: atomic:: AtomicUsize :: new ( 0 ) ) ;
936+ let ( seen, observed) = ( wakes. clone ( ) , Arc :: downgrade ( & shared) ) ;
937+ * shared. waker . lock ( ) . unwrap ( ) = Some ( Arc :: new ( move || {
938+ // The state lock is free while the caller is told.
939+ let shared = observed. upgrade ( ) . unwrap ( ) ;
940+ assert ! ( shared. state. try_lock( ) . is_ok( ) ) ;
941+ seen. fetch_add ( 1 , std:: sync:: atomic:: Ordering :: SeqCst ) ;
942+ } ) ) ;
943+ shared. publish ( & store. record , UpdateStatus :: Shutdown ) ;
944+ shared. publish ( & store. record , UpdateStatus :: Shutdown ) ;
945+ assert_eq ! ( wakes. load( std:: sync:: atomic:: Ordering :: SeqCst ) , 2 ) ;
946+ }
947+
890948 #[ test]
891949 fn a_fresh_restart_preserves_due_time_and_never_admits_a_startup_request ( ) {
892950 let dir = tempfile:: tempdir_in ( std:: env:: current_dir ( ) . unwrap ( ) ) . unwrap ( ) ;
0 commit comments