@@ -400,6 +400,22 @@ pub(crate) mod test_gate {
400400 pub ( crate ) fn take ( ) -> Option < Arc < PreviewGate > > {
401401 NEXT_GATE . with ( |g| g. borrow_mut ( ) . take ( ) )
402402 }
403+
404+ thread_local ! {
405+ /// The next build started on this thread fails to start its worker (a thread the system
406+ /// won't give), taken by `build`.
407+ static FAIL_SPAWN : std:: cell:: Cell <bool > = const { std:: cell:: Cell :: new( false ) } ;
408+ }
409+
410+ /// Have the next build started on this thread fail to start its worker.
411+ pub ( crate ) fn fail_next_spawn ( ) {
412+ FAIL_SPAWN . with ( |f| f. set ( true ) ) ;
413+ }
414+
415+ /// Whether this build's worker is to fail to start (once).
416+ pub ( crate ) fn take_fail_spawn ( ) -> bool {
417+ FAIL_SPAWN . with ( std:: cell:: Cell :: take)
418+ }
403419}
404420
405421/// One photo's work in a build: its id and its render jobs ([`jobs_for`]).
@@ -525,9 +541,9 @@ fn build(s: &mut Session, p: &Value) -> Result<Value> {
525541 s. visible_cloned ( )
526542 } ;
527543 let jobs: Vec < PhotoJobs > = ids. iter ( ) . map ( |id| ( id. 0 , jobs_for ( s, * id, edge) ) ) . filter ( |j| !j. 1 . is_empty ( ) ) . collect ( ) ;
528- if let Some ( cancel ) = gives_way {
529- cancel . store ( true , Ordering :: Relaxed ) ;
530- }
544+ // the background build stays the session's until this one has started (a worker that can't
545+ // start leaves it running and in place: CodeRabbit on #3544)
546+ let previous = s . preview_build . clone ( ) ;
531547 let order = jobs. iter ( ) . map ( |j| j. 0 ) . collect ( ) ;
532548 #[ cfg_attr( not( test) , allow( unused_mut) ) ]
533549 let mut build_state = PreviewBuild { total : jobs. len ( ) , background, order, ..Default :: default ( ) } ;
@@ -545,20 +561,38 @@ fn build(s: &mut Session, p: &Value) -> Result<Value> {
545561 let lanes = if background { crate :: prep:: background_lanes ( ) } else { lanes ( jobs. len ( ) , edge. is_none ( ) ) } ;
546562 let wait = p. get ( "wait" ) . and_then ( Value :: as_bool) . unwrap_or ( false ) || cfg ! ( target_arch = "wasm32" ) ;
547563 if wait {
564+ if let Some ( cancel) = gives_way {
565+ cancel. store ( true , Ordering :: Relaxed ) ;
566+ }
548567 run_all ( jobs, & state, task. as_ref ( ) , lanes, force) ;
549568 return Ok ( state. json ( ) ) ;
550569 }
551570 #[ cfg( not( target_arch = "wasm32" ) ) ]
552571 {
553572 // the row stays while the worker runs (it is dropped with the closure, however that ends)
554573 let st = state. clone ( ) ;
574+ #[ cfg( test) ]
575+ let started = if test_gate:: take_fail_spawn ( ) {
576+ drop ( task) ;
577+ Err ( std:: io:: Error :: other ( "no thread (test)" ) )
578+ } else {
579+ std:: thread:: Builder :: new ( ) . name ( "lc-build-previews" . into ( ) ) . spawn ( move || run_all ( jobs, & st, task. as_ref ( ) , lanes, force) )
580+ } ;
581+ #[ cfg( not( test) ) ]
555582 let started = std:: thread:: Builder :: new ( ) . name ( "lc-build-previews" . into ( ) ) . spawn ( move || run_all ( jobs, & st, task. as_ref ( ) , lanes, force) ) ;
556583 if let Err ( e) = started {
557- // never started: not left "running", which would hold every later build back
584+ // never started: not left "running", which would hold every later build back, and the
585+ // build it was to replace carries on
558586 state. finished . store ( true , Ordering :: Relaxed ) ;
587+ s. preview_build = previous;
559588 return Err ( bad ( C , format ! ( "could not start: {e}" ) ) ) ;
560589 }
590+ if let Some ( cancel) = gives_way {
591+ cancel. store ( true , Ordering :: Relaxed ) ;
592+ }
561593 }
594+ #[ cfg( target_arch = "wasm32" ) ]
595+ let _ = previous;
562596 Ok ( state. json ( ) )
563597}
564598
0 commit comments