@@ -17,6 +17,7 @@ import {
1717 readServiceEnvironment ,
1818 type BackupSpaceProbe ,
1919 readServiceState ,
20+ stateDirOwned ,
2021 writeServiceState ,
2122} from "./serviceLauncher.ts" ;
2223import {
@@ -29,12 +30,16 @@ import {
2930} from "./cloud/serviceProtocol.ts" ;
3031
3132const spawnedChildren = vi . hoisted ( ( ) : Array < NodeChildProcess . ChildProcess > => [ ] ) ;
33+ const spawnHook = vi . hoisted ( ( ) => ( {
34+ current : undefined as ( ( child : NodeChildProcess . ChildProcess ) => void ) | undefined ,
35+ } ) ) ;
3236
3337vi . mock ( "node:child_process" , async ( importOriginal ) => {
3438 const actual = await importOriginal < typeof NodeChildProcess > ( ) ;
3539 const spawn = ( ...args : Parameters < typeof actual . spawn > ) => {
3640 const child = actual . spawn ( ...args ) ;
3741 spawnedChildren . push ( child ) ;
42+ spawnHook . current ?.( child ) ;
3843 return child ;
3944 } ;
4045 return { ...actual , spawn : spawn as typeof actual . spawn } ;
@@ -270,6 +275,7 @@ it.layer(NodeServices.layer)("service state persistence", (it) => {
270275 `const db = new (require("node:sqlite").DatabaseSync)(${ JSON . stringify ( ownerPath ) } );` ,
271276 `try { db.exec("PRAGMA busy_timeout = 0; BEGIN EXCLUSIVE;"); }` ,
272277 `catch { process.exit(${ SERVER_EXIT_CODE_STATE_DIR_OWNED } ); }` ,
278+ 'process.send("owner");' ,
273279 "setInterval(() => {}, 1_000);" ,
274280 ] . join ( "\n" ) ,
275281 ) ;
@@ -282,17 +288,25 @@ it.layer(NodeServices.layer)("service state persistence", (it) => {
282288 const otherOwner = new NodeSqlite . DatabaseSync ( ownerPath ) ;
283289 otherOwner . exec ( "BEGIN EXCLUSIVE;" ) ;
284290 const refused = Promise . withResolvers < void > ( ) ;
285- const reclaimed = Promise . withResolvers < void > ( ) ;
291+ const owned = Promise . withResolvers < void > ( ) ;
286292 yield * Effect . acquireRelease (
287293 Effect . sync ( ( ) =>
288294 vi . spyOn ( process . stderr , "write" ) . mockImplementation ( ( chunk ) => {
289295 if ( String ( chunk ) . includes ( "owns this T3 home" ) ) refused . resolve ( ) ;
290- if ( String ( chunk ) . includes ( "T3 home is free again" ) ) reclaimed . resolve ( ) ;
291296 return true ;
292297 } ) ,
293298 ) ,
294299 ( spy ) => Effect . sync ( ( ) => spy . mockRestore ( ) ) ,
295300 ) ;
301+ yield * Effect . acquireRelease (
302+ Effect . sync ( ( ) => {
303+ spawnHook . current = ( child ) => child . once ( "message" , ( ) => owned . resolve ( ) ) ;
304+ } ) ,
305+ ( ) =>
306+ Effect . sync ( ( ) => {
307+ spawnHook . current = undefined ;
308+ } ) ,
309+ ) ;
296310 const spawnsBefore = spawnedChildren . length ;
297311 const launcher = new Launcher (
298312 root ,
@@ -304,13 +318,38 @@ it.layer(NodeServices.layer)("service state persistence", (it) => {
304318 const running = launcher . run ( ) ;
305319 yield * Effect . promise ( ( ) => refused . promise ) ;
306320 otherOwner . close ( ) ;
307- yield * Effect . promise ( ( ) => reclaimed . promise ) ;
321+ yield * Effect . promise ( ( ) => owned . promise ) ;
308322 yield * Effect . promise ( ( ) => launcher . stop ( "SIGTERM" ) ) ;
309323 yield * Effect . promise ( ( ) => running ) ;
310324 assert . strictEqual ( spawnedChildren . length - spawnsBefore , 2 ) ;
311325 } ) ,
312326 ) ;
313327
328+ it . effect ( "treats the T3 home as owned while the lock or a pre-lock server holds it" , ( ) =>
329+ Effect . gen ( function * ( ) {
330+ const fs = yield * FileSystem . FileSystem ;
331+ const path = yield * Path . Path ;
332+ const root = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-service-owned-" } ) ;
333+ const stateDir = path . join ( root , "userdata" ) ;
334+ const recordPath = path . join ( stateDir , "server-runtime.json" ) ;
335+ yield * fs . makeDirectory ( stateDir , { recursive : true } ) ;
336+ assert . isFalse ( stateDirOwned ( root ) ) ;
337+
338+ const owner = new NodeSqlite . DatabaseSync ( path . join ( stateDir , "server-owner.sqlite" ) ) ;
339+ owner . exec ( "BEGIN EXCLUSIVE;" ) ;
340+ assert . isTrue ( stateDirOwned ( root ) ) ;
341+ owner . close ( ) ;
342+
343+ const record = { version : 1 , pid : process . pid , port : 1 , origin : "" , startedAt : "" } ;
344+ yield * fs . writeFileString ( recordPath , JSON . stringify ( record ) ) ;
345+ assert . isTrue ( stateDirOwned ( root ) ) ;
346+ yield * fs . writeFileString ( recordPath , JSON . stringify ( { ...record , ownerId : "lock-aware" } ) ) ;
347+ assert . isFalse ( stateDirOwned ( root ) ) ;
348+ yield * fs . writeFileString ( recordPath , JSON . stringify ( { ...record , pid : 2 ** 22 + 1 } ) ) ;
349+ assert . isFalse ( stateDirOwned ( root ) ) ;
350+ } ) ,
351+ ) ;
352+
314353 it . effect ( "commits only after the trial reports prepared" , ( ) =>
315354 Effect . gen ( function * ( ) {
316355 const fs = yield * FileSystem . FileSystem ;
0 commit comments