@@ -36,6 +36,9 @@ const HANDOFF_DELAY_MS = 2_000;
3636 */
3737const PREPARED_TIMEOUT_MS = 30 * 60_000 ;
3838const TERMINATE_GRACE_MS = 5_000 ;
39+ // Keep SQLite startup and journaling from turning a full filesystem into a restart loop.
40+ const MIN_SERVICE_START_FREE_BYTES = 64 * 1024 ** 2 ;
41+ const DISK_SPACE_RETRY_MS = 30_000 ;
3942// Match contracts' SERVER_EXIT_CODE_STATE_DIR_OWNED without depending on Effect.
4043const STATE_DIR_OWNED_EXIT_CODE = 78 ;
4144
@@ -219,6 +222,11 @@ const filesystemBackupSpaceProbe: BackupSpaceProbe = {
219222 canClone : backupCanClone ,
220223} ;
221224
225+ export const serviceStartDiskSpaceBlocker = ( freeBytes : number ) : string | undefined =>
226+ freeBytes < MIN_SERVICE_START_FREE_BYTES
227+ ? `T3 Code needs at least ${ MIN_SERVICE_START_FREE_BYTES } bytes free before starting the server; ${ freeBytes } bytes available.`
228+ : undefined ;
229+
222230/** Check before stopping the server so full disks refuse updates without an outage. */
223231export async function databaseBackupBlocker (
224232 baseDir : string ,
@@ -524,6 +532,36 @@ export class Launcher {
524532 this . #timer = undefined ;
525533 }
526534
535+ async #serviceStartDiskSpaceBlocker( ) : Promise < string | undefined > {
536+ try {
537+ return serviceStartDiskSpaceBlocker (
538+ await filesystemBackupSpaceProbe . readFreeBytes ( this . #baseDir) ,
539+ ) ;
540+ } catch {
541+ return undefined ;
542+ }
543+ }
544+
545+ #waitForDiskSpace(
546+ reason : string ,
547+ version : string ,
548+ role : ChildRole ,
549+ update ?: ServiceUpdateRecord ,
550+ ) : void {
551+ if ( this . #timer !== undefined || this . #stopping) return ;
552+ process . stderr . write ( `[service-launcher] ${ reason } Waiting for free space before retrying.\n` ) ;
553+ this . #timer = setInterval (
554+ ( ) =>
555+ this . #enqueue( async ( ) => {
556+ if ( this . #child !== null || this . #stopping) return ;
557+ if ( ( await this . #serviceStartDiskSpaceBlocker( ) ) !== undefined ) return ;
558+ this . #clearTimer( ) ;
559+ await this . #startChild( version , role , update ) ;
560+ } ) ,
561+ DISK_SPACE_RETRY_MS ,
562+ ) ;
563+ }
564+
527565 async #recover( ) : Promise < void > {
528566 // Clear old stop markers to preserve future tunnels; clear restart-pending only for the awaited version to avoid CLI races.
529567 await NodeFSP . rm ( stopMarkerPath ( this . #baseDir) , { force : true } ) . catch ( ( ) => undefined ) ;
@@ -567,6 +605,11 @@ export class Launcher {
567605
568606 async #startChild( version : string , role : ChildRole , update ?: ServiceUpdateRecord ) : Promise < void > {
569607 if ( this . #stopping) return ;
608+ const diskSpaceBlocker = await this . #serviceStartDiskSpaceBlocker( ) ;
609+ if ( diskSpaceBlocker !== undefined ) {
610+ this . #waitForDiskSpace( diskSpaceBlocker , version , role , update ) ;
611+ return ;
612+ }
570613 if ( ! ( await runtimeExists ( this . #baseDir, version ) ) ) {
571614 throw new Error ( `Selected t3@${ version } runtime is missing or incomplete.` ) ;
572615 }
@@ -749,6 +792,12 @@ export class Launcher {
749792 await this . #idleWhileStateDirOwned( ) ;
750793 return ;
751794 }
795+ const diskSpaceBlocker = await this . #serviceStartDiskSpaceBlocker( ) ;
796+ if ( diskSpaceBlocker !== undefined ) {
797+ this . #clearTimer( ) ;
798+ this . #waitForDiskSpace( diskSpaceBlocker , child . version , child . role , this . #state. update ) ;
799+ return ;
800+ }
752801 if ( child . role === "trial" ) {
753802 this . #clearTimer( ) ;
754803 const pending = this . #state. update ;
0 commit comments