77} from "../../scripts/lib/package-lifecycle-marker.mjs" ;
88
99const PACKAGE_LIFECYCLE_LOCK_RELATIVE_PATH = ".openclaw-lifecycle-lock" ;
10- const PACKAGE_LIFECYCLE_TIMEOUT_MS = 20 * 60_000 ;
10+ const DEFAULT_PACKAGE_LIFECYCLE_SCRIPT_TIMEOUT_MS = 20 * 60_000 ;
1111const PACKAGE_LIFECYCLE_LOCK_POLL_MS = 100 ;
12+ const PACKAGE_LIFECYCLE_LOCK_RECOVERY_GRACE_MS = 20 * 60_000 ;
1213
1314export type PackageLifecycleScript = Readonly < {
1415 name : "preinstall" | "postinstall" ;
@@ -25,10 +26,9 @@ const PACKAGE_LIFECYCLE_SCRIPTS: readonly PackageLifecycleScript[] = [
2526 relativePath : path . join ( "scripts" , "postinstall-bundled-plugins.mjs" ) ,
2627 } ,
2728] ;
28- // Each script can consume its full timeout. Keep one extra window so scheduler and
29- // filesystem overhead cannot make a live owner look stale between scripts.
30- const PACKAGE_LIFECYCLE_LOCK_STALE_MS =
31- PACKAGE_LIFECYCLE_TIMEOUT_MS * ( PACKAGE_LIFECYCLE_SCRIPTS . length + 1 ) ;
29+ function resolveLifecycleBudgetMs ( scriptTimeoutMs : number ) : number {
30+ return scriptTimeoutMs * PACKAGE_LIFECYCLE_SCRIPTS . length ;
31+ }
3232
3333function hasErrorCode ( error : unknown , code : string ) : boolean {
3434 return error instanceof Error && "code" in error && error . code === code ;
@@ -68,23 +68,41 @@ async function ensurePendingMarker(markerPath: string): Promise<void> {
6868 }
6969}
7070
71- async function acquireLifecycleLock ( paths : ReturnType < typeof resolveLifecyclePaths > ) {
72- const deadline = Date . now ( ) + PACKAGE_LIFECYCLE_TIMEOUT_MS ;
71+ async function acquireLifecycleLock (
72+ paths : ReturnType < typeof resolveLifecyclePaths > ,
73+ scriptTimeoutMs : number ,
74+ ) {
75+ let waitDeadline =
76+ Date . now ( ) +
77+ resolveLifecycleBudgetMs ( scriptTimeoutMs ) +
78+ PACKAGE_LIFECYCLE_LOCK_RECOVERY_GRACE_MS ;
7379 while ( await isPackageLifecyclePending ( paths ) ) {
7480 try {
7581 await fs . mkdir ( paths . lock ) ;
82+ const ownerDeadline = new Date ( Date . now ( ) + resolveLifecycleBudgetMs ( scriptTimeoutMs ) ) ;
83+ try {
84+ // The directory mtime carries the owner's full script budget across processes.
85+ // Recovery grace also protects the short mkdir-to-utimes ownership window.
86+ await fs . utimes ( paths . lock , ownerDeadline , ownerDeadline ) ;
87+ } catch ( error ) {
88+ await fs . rmdir ( paths . lock ) . catch ( ( ) => undefined ) ;
89+ throw error ;
90+ }
7691 return async ( ) => await fs . rmdir ( paths . lock ) . catch ( ( ) => undefined ) ;
7792 } catch ( error ) {
7893 if ( ! hasErrorCode ( error , "EEXIST" ) ) {
7994 throw error ;
8095 }
8196 const stat = await fs . stat ( paths . lock ) . catch ( ( ) => null ) ;
82- // Lifecycle subprocesses share this deadline. An older lock cannot still own live work.
83- if ( stat && Date . now ( ) - stat . mtimeMs >= PACKAGE_LIFECYCLE_LOCK_STALE_MS ) {
84- await fs . rmdir ( paths . lock ) . catch ( ( ) => undefined ) ;
85- continue ;
97+ if ( stat ) {
98+ const staleAt = stat . mtimeMs + PACKAGE_LIFECYCLE_LOCK_RECOVERY_GRACE_MS ;
99+ waitDeadline = Math . max ( waitDeadline , staleAt ) ;
100+ if ( Date . now ( ) >= staleAt ) {
101+ await fs . rmdir ( paths . lock ) . catch ( ( ) => undefined ) ;
102+ continue ;
103+ }
86104 }
87- if ( Date . now ( ) >= deadline ) {
105+ if ( Date . now ( ) >= waitDeadline ) {
88106 throw new Error ( "timed out waiting for another OpenClaw package lifecycle" , {
89107 cause : error ,
90108 } ) ;
@@ -97,13 +115,17 @@ async function acquireLifecycleLock(paths: ReturnType<typeof resolveLifecyclePat
97115 return null ;
98116}
99117
100- function runPackageLifecycleScript ( packageRoot : string , script : PackageLifecycleScript ) : void {
118+ function runPackageLifecycleScript (
119+ packageRoot : string ,
120+ script : PackageLifecycleScript ,
121+ timeoutMs : number ,
122+ ) : void {
101123 const scriptPath = path . join ( packageRoot , script . relativePath ) ;
102124 const result = spawnSync ( process . execPath , [ scriptPath ] , {
103125 cwd : packageRoot ,
104126 env : process . env ,
105127 stdio : "inherit" ,
106- timeout : PACKAGE_LIFECYCLE_TIMEOUT_MS ,
128+ timeout : timeoutMs ,
107129 } ) ;
108130 if ( result . error ) {
109131 throw result . error ;
@@ -118,14 +140,16 @@ function runPackageLifecycleScript(packageRoot: string, script: PackageLifecycle
118140export async function completePendingPackageLifecycle ( params : {
119141 packageRoot : string ;
120142 runScript ?: ( script : PackageLifecycleScript ) => void | Promise < void > ;
143+ timeoutMs ?: number ;
121144} ) : Promise < boolean > {
122145 const packageRoot = path . resolve ( params . packageRoot ) ;
146+ const scriptTimeoutMs = params . timeoutMs ?? DEFAULT_PACKAGE_LIFECYCLE_SCRIPT_TIMEOUT_MS ;
123147 const paths = resolveLifecyclePaths ( packageRoot ) ;
124148 if ( ! ( await isPackageLifecyclePending ( paths ) ) ) {
125149 return false ;
126150 }
127151
128- const releaseLock = await acquireLifecycleLock ( paths ) ;
152+ const releaseLock = await acquireLifecycleLock ( paths , scriptTimeoutMs ) ;
129153 if ( ! releaseLock ) {
130154 return false ;
131155 }
@@ -137,7 +161,8 @@ export async function completePendingPackageLifecycle(params: {
137161 // Postinstall alone clears the canonical marker after all lifecycle work succeeds.
138162 await ensurePendingMarker ( paths . pending ) ;
139163 const runScript =
140- params . runScript ?? ( ( script ) => runPackageLifecycleScript ( packageRoot , script ) ) ;
164+ params . runScript ??
165+ ( ( script ) => runPackageLifecycleScript ( packageRoot , script , scriptTimeoutMs ) ) ;
141166 for ( const script of PACKAGE_LIFECYCLE_SCRIPTS ) {
142167 await runScript ( script ) ;
143168 }
0 commit comments