@@ -9,6 +9,7 @@ import * as NodeCrypto from "node:crypto";
99import * as NodeFS from "node:fs" ;
1010import * as NodeFSP from "node:fs/promises" ;
1111import * as NodePath from "node:path" ;
12+ import * as NodeSqlite from "node:sqlite" ;
1213import * as NodeUtil from "node:util" ;
1314
1415import type {
@@ -42,6 +43,7 @@ const TERMINATE_GRACE_MS = 5_000;
4243// Matches SERVER_EXIT_CODE_STATE_DIR_OWNED in contracts. The standalone
4344// launcher stays independent of the Effect-backed contracts package.
4445const STATE_DIR_OWNED_EXIT_CODE = 78 ;
46+ const STATE_DIR_OWNER_POLL_MS = 30_000 ;
4547
4648type TerminalStatus = "committed" | "rolled-back" | "failed" ;
4749type ChildRole = "active" | "trial" ;
@@ -458,6 +460,28 @@ async function terminateChild(
458460 }
459461}
460462
463+ /**
464+ * Takes and drops the same exclusive transaction the server holds for its
465+ * lifetime (see `acquireServerOwnership`). Only SQLITE_BUSY counts as owned:
466+ * any other failure is left for the server's own startup to report.
467+ */
468+ function stateDirOwned ( baseDir : string ) : boolean {
469+ let db : NodeSqlite . DatabaseSync | undefined ;
470+ try {
471+ db = new NodeSqlite . DatabaseSync ( NodePath . join ( baseDir , "userdata" , "server-owner.sqlite" ) ) ;
472+ db . exec ( "PRAGMA busy_timeout = 0; BEGIN EXCLUSIVE; ROLLBACK;" ) ;
473+ return false ;
474+ } catch ( cause ) {
475+ return (
476+ cause instanceof Error &&
477+ ( ( "errcode" in cause && cause . errcode === 5 ) ||
478+ ( "code" in cause && cause . code === "SQLITE_BUSY" ) )
479+ ) ;
480+ } finally {
481+ db ?. close ( ) ;
482+ }
483+ }
484+
461485const stopMarkerPath = ( baseDir : string ) =>
462486 NodePath . join ( baseDir , "runtime" , SERVICE_STOP_MARKER_FILE ) ;
463487const restartPendingPath = ( baseDir : string ) =>
@@ -476,16 +500,19 @@ export class Launcher {
476500 readonly #completion = Promise . withResolvers < void > ( ) ;
477501 readonly #backupSpaceProbe: Partial < BackupSpaceProbe > | undefined ;
478502 readonly #environment: Readonly < NodeJS . ProcessEnv > ;
503+ readonly #stateDirOwnerPollMs: number ;
479504
480505 constructor (
481506 baseDir : string ,
482507 state : ServiceState ,
483508 options : {
484509 readonly backupSpaceProbe ?: Partial < BackupSpaceProbe > ;
485510 readonly environment ?: Readonly < NodeJS . ProcessEnv > ;
511+ readonly stateDirOwnerPollMs ?: number ;
486512 } = { } ,
487513 ) {
488514 this . #baseDir = baseDir ;
515+ this . #stateDirOwnerPollMs = options . stateDirOwnerPollMs ?? STATE_DIR_OWNER_POLL_MS ;
489516 this . #backupSpaceProbe = options . backupSpaceProbe ;
490517 this . #environment = { ...( options . environment ?? process . env ) } ;
491518 this . #statePath = NodePath . join ( baseDir , "runtime" , SERVICE_STATE_FILE ) ;
@@ -814,8 +841,9 @@ export class Launcher {
814841
815842 /**
816843 * Exiting would make systemd or launchd start another refused server, so stay
817- * idle until stopped or restarted. A pending update fails without restoring
818- * its backup: the other owner may have written to the database since.
844+ * idle and start the server again once the other owner releases the lock. A
845+ * pending update fails without restoring its backup: the other owner may have
846+ * written to the database since.
819847 */
820848 async #idleWhileStateDirOwned( ) : Promise < void > {
821849 this . #clearTimer( ) ;
@@ -830,13 +858,24 @@ export class Launcher {
830858 this . #state = next ;
831859 await discardDatabaseBackup ( this . #baseDir, pending . id ) . catch ( ( ) => undefined ) ;
832860 }
833- // Signal listeners alone do not keep Node alive.
834- this . #timer = setInterval ( ( ) => { } , 2_147_483_647 ) ;
835861 process . stderr . write (
836- "[service-launcher] Another T3 Code server owns this T3 home. Stop it, then restart this service.\n" ,
862+ "[service-launcher] Another T3 Code server owns this T3 home. This service starts again once it stops.\n" ,
863+ ) ;
864+ // The interval also keeps Node alive: signal listeners alone do not.
865+ this . #timer = setInterval (
866+ ( ) => this . #enqueue( ( ) => this . #reclaimStateDir( ) ) ,
867+ this . #stateDirOwnerPollMs,
837868 ) ;
838869 }
839870
871+ async #reclaimStateDir( ) : Promise < void > {
872+ if ( this . #timer === undefined || this . #child !== null || this . #stopping) return ;
873+ if ( stateDirOwned ( this . #baseDir) ) return ;
874+ this . #clearTimer( ) ;
875+ process . stderr . write ( "[service-launcher] The T3 home is free again. Starting the server.\n" ) ;
876+ await this . #startChild( this . #state. activeVersion , "active" , this . #state. update ) ;
877+ }
878+
840879 async #returnToPrevious(
841880 pending : PendingServiceUpdate ,
842881 status : "rolled-back" | "failed" ,
0 commit comments