@@ -127,29 +127,26 @@ impl AppAgentHost {
127127
128128/// Which running copy of the app runs each agent. Every copy on this machine
129129/// (the packaged app and any dev builds) shares the agent list and hears the
130- /// same events, so each agent is run by the one copy holding its lock file. The
131- /// OS lets go when that copy exits, however it exits.
130+ /// same events. The packaged app always runs what it can and holds
131+ /// `<pubkey>.packaged` while it does; there is only ever one, since release
132+ /// builds are single-instance. Dev builds step aside while that is held, and
133+ /// otherwise run each agent from whichever holds `<pubkey>.lock`. The OS lets go
134+ /// of both when a copy exits, however it exits.
132135struct Hosting {
133136 dir : Result < PathBuf , String > ,
134- /// A dev build steps aside for a packaged app that can run the same agent.
135137 packaged : bool ,
136- held : Mutex < HashMap < String , Held > > ,
137- }
138- #[ derive( Default ) ]
139- struct Held {
140- /// `<pubkey>.lock`: this copy runs the agent.
141- run : Option < File > ,
142- /// `<pubkey>.packaged`, held by a packaged copy that can run it, so dev builds let go.
143- first : Option < File > ,
138+ /// The lock file this copy holds per agent: `.packaged` or `.lock`.
139+ held : Mutex < HashMap < String , File > > ,
144140}
145141impl Hosting {
146- fn held ( & self ) -> std:: sync:: MutexGuard < ' _ , HashMap < String , Held > > {
142+ fn held ( & self ) -> std:: sync:: MutexGuard < ' _ , HashMap < String , File > > {
147143 self . held
148144 . lock ( )
149145 . unwrap_or_else ( std:: sync:: PoisonError :: into_inner)
150146 }
151- /// Whether this copy runs the agent, taking it if no other copy does. A dev
152- /// build lets go of it, and declines, while a packaged copy wants it.
147+ /// Whether this copy runs the agent. The packaged app always does, so a
148+ /// stuck dev build can never silence it. A dev build takes the agent if no
149+ /// other copy runs it, and lets go while the packaged app does.
153150 #[ allow( clippy:: incompatible_msrv) ] // `File` locking; see `sign_out::Instance`.
154151 fn claim ( & self , pubkey : & str ) -> Result < bool , String > {
155152 if pubkey. len ( ) != 64
@@ -161,30 +158,29 @@ impl Hosting {
161158 }
162159 let dir = self . dir . as_ref ( ) . map_err ( Clone :: clone) ?;
163160 std:: fs:: create_dir_all ( dir) . map_err ( |_| "Could not open agent hosting" ) ?;
164- let first = dir. join ( format ! ( "{pubkey}.packaged" ) ) ;
161+ let packaged = dir. join ( format ! ( "{pubkey}.packaged" ) ) ;
165162 let mut held = self . held ( ) ;
166- if !self . packaged {
167- let probe = open_lock ( & first) ?;
168- if !lock ( probe. try_lock_shared ( ) ) ? {
169- held. remove ( pubkey) ;
170- return Ok ( false ) ;
163+ if self . packaged {
164+ if !held. contains_key ( pubkey) {
165+ let file = open_lock ( & packaged) ?;
166+ // A dev build checking for it holds it briefly; this tries again next time.
167+ if lock ( file. try_lock ( ) ) ? {
168+ held. insert ( pubkey. into ( ) , file) ;
169+ }
171170 }
171+ return Ok ( true ) ;
172172 }
173- let entry = held. entry ( pubkey. into ( ) ) . or_default ( ) ;
174- if self . packaged && entry. first . is_none ( ) {
175- let file = open_lock ( & first) ?;
176- // A dev build checking for it holds it briefly; this tries again next time.
177- if lock ( file. try_lock ( ) ) ? {
178- entry. first = Some ( file) ;
179- }
173+ if !lock ( open_lock ( & packaged) ?. try_lock_shared ( ) ) ? {
174+ held. remove ( pubkey) ;
175+ return Ok ( false ) ;
180176 }
181- if entry . run . is_none ( ) {
177+ if !held . contains_key ( pubkey ) {
182178 let file = open_lock ( & dir. join ( format ! ( "{pubkey}.lock" ) ) ) ?;
183179 if lock ( file. try_lock ( ) ) ? {
184- entry . run = Some ( file) ;
180+ held . insert ( pubkey . into ( ) , file) ;
185181 }
186182 }
187- Ok ( entry . run . is_some ( ) )
183+ Ok ( held . contains_key ( pubkey ) )
188184 }
189185}
190186/// Whether a try at a lock took it; closing the file lets go.
@@ -645,22 +641,26 @@ mod tests {
645641 }
646642
647643 #[ test]
648- fn a_dev_build_lets_go_of_what_a_packaged_copy_can_run ( ) {
644+ fn a_packaged_app_runs_what_it_can_and_dev_builds_let_go ( ) {
649645 let dir = tempfile:: tempdir ( ) . unwrap ( ) ;
650646 let ( agent, other) = ( "a" . repeat ( 64 ) , "b" . repeat ( 64 ) ) ;
651647 let dev = hosting ( Ok ( dir. path ( ) . join ( "hosts" ) ) , false ) ;
652648 let packaged = hosting ( Ok ( dir. path ( ) . join ( "hosts" ) ) , true ) ;
653649 assert ! ( dev. claim( & agent) . unwrap( ) ) ;
654650 assert ! ( dev. claim( & other) . unwrap( ) ) ;
655- // The dev build had it first, and lets go at its next claim.
656- assert ! ( !packaged. claim( & agent) . unwrap( ) ) ;
657- assert ! ( !dev. claim( & agent) . unwrap( ) ) ;
651+ // The packaged app runs it at once, though the dev build had it first,
658652 assert ! ( packaged. claim( & agent) . unwrap( ) ) ;
653+ // and the dev build lets go at its next claim.
659654 assert ! ( !dev. claim( & agent) . unwrap( ) ) ;
660- // One the packaged copy does not ask for stays with the dev build.
655+ assert ! ( packaged. claim( & agent) . unwrap( ) ) ;
656+ // A second dev build cannot take it either.
657+ let another = hosting ( Ok ( dir. path ( ) . join ( "hosts" ) ) , false ) ;
658+ assert ! ( !another. claim( & agent) . unwrap( ) ) ;
659+ // One the packaged app does not ask for stays with the dev build.
661660 assert ! ( dev. claim( & other) . unwrap( ) ) ;
662661 // As when it can no longer run it.
663662 packaged. held ( ) . remove ( & agent) ;
664663 assert ! ( dev. claim( & agent) . unwrap( ) ) ;
664+ assert ! ( !another. claim( & agent) . unwrap( ) ) ;
665665 }
666666}
0 commit comments