@@ -391,15 +391,32 @@ async fn run_zero_config_worker(
391391 update_status ( "loading" , "Loading model weights..." , 0.0 ) ;
392392 log_mobile ( & format ! ( "[cake-mobile] creating Context::from_args (cpu={})..." , force_cpu) ) ;
393393
394+ // Install a panic hook so that any panic during model loading is logged to
395+ // the mobile log (visible in the app's diagnostic output).
394396 let prev_hook = std:: panic:: take_hook ( ) ;
395397 std:: panic:: set_hook ( Box :: new ( move |info| {
396398 let msg = format ! ( "[cake-mobile] PANIC: {}" , info) ;
397399 log_mobile ( & msg) ;
398400 } ) ) ;
399401
400- let ctx_result = std:: panic:: catch_unwind ( std:: panic:: AssertUnwindSafe ( || {
401- Context :: from_args ( args)
402- } ) ) ;
402+ // Context::from_args is CPU- and I/O-intensive (loads model weight files).
403+ // Run it on a dedicated blocking thread to avoid starving the Tokio async
404+ // runtime while the listener is still open and waiting for the master's
405+ // inference reconnect.
406+ let ctx_result = match tokio:: task:: spawn_blocking ( move || {
407+ std:: panic:: catch_unwind ( std:: panic:: AssertUnwindSafe ( || Context :: from_args ( args) ) )
408+ } )
409+ . await
410+ {
411+ Ok ( r) => r,
412+ Err ( join_err) => {
413+ std:: panic:: set_hook ( prev_hook) ;
414+ let msg = format ! ( "context loading task failed: {}" , join_err) ;
415+ log_mobile ( & format ! ( "[cake-mobile] ERROR: {}" , msg) ) ;
416+ update_status ( "error" , & msg, 0.0 ) ;
417+ return msg;
418+ }
419+ } ;
403420
404421 std:: panic:: set_hook ( prev_hook) ;
405422
@@ -456,15 +473,23 @@ async fn run_direct_worker(name: &str, model: &str, address: &str) -> String {
456473 update_status ( "loading" , "Downloading model..." , 0.0 ) ;
457474 log_mobile ( "[cake-mobile] creating context..." ) ;
458475
459- let mut ctx = match Context :: from_args ( args) {
460- Ok ( ctx) => {
476+ // Context::from_args downloads / loads large model weight files. Run it
477+ // on a dedicated blocking thread so the Tokio async runtime stays live.
478+ let ctx_result = tokio:: task:: spawn_blocking ( move || Context :: from_args ( args) ) . await ;
479+ let mut ctx = match ctx_result {
480+ Ok ( Ok ( ctx) ) => {
461481 log_mobile ( & format ! ( "[cake-mobile] context created, device={:?}" , ctx. device) ) ;
462482 ctx
463483 }
464- Err ( e) => {
484+ Ok ( Err ( e) ) => {
465485 update_status ( "error" , & format ! ( "Failed: {}" , e) , 0.0 ) ;
466486 return format ! ( "context creation failed: {}" , e) ;
467487 }
488+ Err ( join_err) => {
489+ let msg = format ! ( "context loading task failed: {}" , join_err) ;
490+ update_status ( "error" , & msg, 0.0 ) ;
491+ return msg;
492+ }
468493 } ;
469494
470495 update_status ( "serving" , "Ready — serving inference" , 1.0 ) ;
0 commit comments