@@ -417,6 +417,29 @@ export function isMacLaunchAgentConfigured(
417417 }
418418}
419419
420+ function bootstrapMacLaunchAgent ( domain : string , path : string ) {
421+ const retryDeadline = Date . now ( ) + 2_000 ;
422+ const sleeper = new Int32Array ( new SharedArrayBuffer ( 4 ) ) ;
423+ let result = spawnSync ( launchctlBin ( ) , [ "bootstrap" , domain , path ] , {
424+ encoding : "utf8" ,
425+ timeout : pm2CommandTimeoutMs ( ) ,
426+ killSignal : "SIGKILL" ,
427+ } ) ;
428+
429+ while ( result . status !== 0 && Date . now ( ) < retryDeadline ) {
430+ const output = `${ result . stdout ?? "" } ${ result . stderr ?? "" } ` ;
431+ if ( result . status !== 5 && ! output . includes ( "Bootstrap failed: 5:" ) ) break ;
432+ Atomics . wait ( sleeper , 0 , 0 , 100 ) ;
433+ result = spawnSync ( launchctlBin ( ) , [ "bootstrap" , domain , path ] , {
434+ encoding : "utf8" ,
435+ timeout : pm2CommandTimeoutMs ( ) ,
436+ killSignal : "SIGKILL" ,
437+ } ) ;
438+ }
439+
440+ return result ;
441+ }
442+
420443export function installMacLaunchAgent ( expectedRuntime = currentGatewayRuntime ( ) ) : string {
421444 if ( typeof process . getuid !== "function" ) {
422445 throw new Error ( "[supertask] 当前运行时无法获取 macOS 用户 ID" ) ;
@@ -488,11 +511,7 @@ export function installMacLaunchAgent(expectedRuntime = currentGatewayRuntime())
488511 timeout : pm2CommandTimeoutMs ( ) ,
489512 killSignal : "SIGKILL" ,
490513 } ) ;
491- const loaded = spawnSync ( launchctlBin ( ) , [ "bootstrap" , domain , path ] , {
492- encoding : "utf8" ,
493- timeout : pm2CommandTimeoutMs ( ) ,
494- killSignal : "SIGKILL" ,
495- } ) ;
514+ const loaded = bootstrapMacLaunchAgent ( domain , path ) ;
496515 const configuredVerifyTimeout = Number ( process . env . SUPERTASK_LAUNCH_AGENT_VERIFY_TIMEOUT_MS ?? 2_000 ) ;
497516 const verifyTimeoutMs = Number . isFinite ( configuredVerifyTimeout ) && configuredVerifyTimeout > 0
498517 ? configuredVerifyTimeout
@@ -523,11 +542,7 @@ export function installMacLaunchAgent(expectedRuntime = currentGatewayRuntime())
523542
524543 let rollbackFailure = "" ;
525544 if ( wasLoaded && previousPlist != null ) {
526- const restored = spawnSync ( launchctlBin ( ) , [ "bootstrap" , domain , path ] , {
527- encoding : "utf8" ,
528- timeout : pm2CommandTimeoutMs ( ) ,
529- killSignal : "SIGKILL" ,
530- } ) ;
545+ const restored = bootstrapMacLaunchAgent ( domain , path ) ;
531546 if ( restored . status !== 0 ) {
532547 rollbackFailure = `;旧 LaunchAgent 恢复失败: ${ `${ restored . stdout ?? "" } ${ restored . stderr ?? "" } ` . trim ( ) || `退出码 ${ restored . status } ` } ` ;
533548 }
0 commit comments