@@ -11,12 +11,14 @@ import { makeServerLayer } from "./server.ts";
1111import { PersistedServerRuntimeState } from "./serverRuntimeState.ts" ;
1212import {
1313 SERVER_LOCK_FILENAME ,
14+ SERVER_LOCK_DATABASE_FILENAME ,
1415 SERVER_RUNTIME_STATE_FILENAME ,
1516 acquireServerSingleton ,
17+ isServerLockBusyError ,
1618 processIsAlive ,
1719 processIsAliveWith ,
1820 recordServerLockPort ,
19- releaseServerLock ,
21+ serverLockDatabasePath ,
2022 serverLockPath ,
2123} from "./serverSingleton.ts" ;
2224
@@ -29,7 +31,7 @@ const makeStateDir = Effect.fn("test.makeStateDir")(function* () {
2931
3032/** A stale lock file, written without the module's own encoder on purpose. */
3133const staleHolder = ( pid : number ) =>
32- `{"version":1,"pid":${ pid } ,"startedAt":"2026-01-01T00:00:00.000Z"}` ;
34+ `{"version":1,"ownerId":"stale-owner"," pid":${ pid } ,"startedAt":"2026-01-01T00:00:00.000Z"}` ;
3335
3436const PersistedServerRuntimeStateFromJson = Schema . fromJsonString ( PersistedServerRuntimeState ) ;
3537const encodeRuntimeState = Schema . encodeSync ( PersistedServerRuntimeStateFromJson ) ;
@@ -52,17 +54,17 @@ layer("serverSingleton", (it) => {
5254 const baseDir = yield * fs . makeTempDirectory ( { prefix : "t3-singleton-server-layer-" } ) ;
5355 const stateDir = path . join ( baseDir , "userdata" ) ;
5456 yield * fs . makeDirectory ( stateDir , { recursive : true } ) ;
55- yield * fs . writeFileString (
56- path . join ( stateDir , SERVER_LOCK_FILENAME ) ,
57- `{"version":1,"pid":${ process . pid } ,"startedAt":"2026-01-01T00:00:00.000Z"}` ,
58- ) ;
59-
60- const failure = yield * Layer . build (
61- makeServerLayer . pipe ( Layer . provide ( ServerConfig . layerTest ( process . cwd ( ) , baseDir ) ) ) ,
62- ) . pipe ( Effect . scoped , Effect . flip ) ;
57+ yield * Effect . scoped (
58+ Effect . gen ( function * ( ) {
59+ yield * acquireServerSingleton ( stateDir ) ;
60+ const failure = yield * Layer . build (
61+ makeServerLayer . pipe ( Layer . provide ( ServerConfig . layerTest ( process . cwd ( ) , baseDir ) ) ) ,
62+ ) . pipe ( Effect . scoped , Effect . flip ) ;
6363
64- assert . strictEqual ( failure . _tag , "ServerAlreadyRunningError" ) ;
65- assert . isFalse ( yield * fs . exists ( path . join ( stateDir , "state.sqlite" ) ) ) ;
64+ assert . strictEqual ( failure . _tag , "ServerAlreadyRunningError" ) ;
65+ assert . isFalse ( yield * fs . exists ( path . join ( stateDir , "state.sqlite" ) ) ) ;
66+ } ) ,
67+ ) ;
6668 } ) ,
6769 ) ;
6870
@@ -103,7 +105,7 @@ layer("serverSingleton", (it) => {
103105 } ) ,
104106 ) ;
105107
106- it . effect ( "reclaims a lock whose owner is gone" , ( ) =>
108+ it . effect ( "overwrites stale display metadata after its owner is gone" , ( ) =>
107109 Effect . gen ( function * ( ) {
108110 const stateDir = yield * makeStateDir ( ) ;
109111 const fs = yield * FileSystem . FileSystem ;
@@ -112,18 +114,18 @@ layer("serverSingleton", (it) => {
112114 // be live. A crashed server must not lock its own directory forever.
113115 yield * fs . writeFileString ( lockPath , staleHolder ( 4194304 ) ) ;
114116
115- // One call confirms the dead holder, reclaims it, and claims the freed
116- // directory on the same pass.
117117 yield * Effect . scoped (
118118 Effect . gen ( function * ( ) {
119119 const held = yield * acquireServerSingleton ( stateDir ) ;
120120 assert . strictEqual ( held , lockPath ) ;
121+ const metadata = yield * fs . readFileString ( lockPath ) ;
122+ assert . notInclude ( metadata , "stale-owner" ) ;
121123 } ) ,
122124 ) ;
123125 } ) ,
124126 ) ;
125127
126- it . effect ( "reclaims a lock file left half-written by a crash" , ( ) =>
128+ it . effect ( "overwrites half-written display metadata after a crash" , ( ) =>
127129 Effect . gen ( function * ( ) {
128130 const stateDir = yield * makeStateDir ( ) ;
129131 const fs = yield * FileSystem . FileSystem ;
@@ -138,16 +140,20 @@ layer("serverSingleton", (it) => {
138140 } ) ,
139141 ) ;
140142
141- it . effect ( "does not release a lock another process has reclaimed " , ( ) =>
143+ it . effect ( "does not remove display metadata replaced by another owner " , ( ) =>
142144 Effect . gen ( function * ( ) {
143145 const stateDir = yield * makeStateDir ( ) ;
144146 const fs = yield * FileSystem . FileSystem ;
145147 const lockPath = yield * serverLockPath ( stateDir ) ;
146- yield * fs . writeFileString ( lockPath , staleHolder ( 4194304 ) ) ;
148+ yield * Effect . scoped (
149+ Effect . gen ( function * ( ) {
150+ yield * acquireServerSingleton ( stateDir ) ;
151+ yield * fs . writeFileString ( lockPath , staleHolder ( process . pid ) ) ;
152+ } ) ,
153+ ) ;
147154
148- yield * releaseServerLock ( lockPath ) ;
149- // Evicting a live successor would recreate the very bug this prevents.
150155 assert . isTrue ( yield * fs . exists ( lockPath ) ) ;
156+ assert . include ( yield * fs . readFileString ( lockPath ) , "stale-owner" ) ;
151157 } ) ,
152158 ) ;
153159
@@ -183,12 +189,14 @@ layer("serverSingleton", (it) => {
183189 } ) ,
184190 ) ;
185191
186- it . effect ( "uses a lock file inside the state directory" , ( ) =>
192+ it . effect ( "keeps ownership and display metadata inside the state directory" , ( ) =>
187193 Effect . gen ( function * ( ) {
188194 const stateDir = yield * makeStateDir ( ) ;
189195 const path = yield * Path . Path ;
190196 const lockPath = yield * serverLockPath ( stateDir ) ;
197+ const databasePath = yield * serverLockDatabasePath ( stateDir ) ;
191198 assert . strictEqual ( lockPath , path . join ( stateDir , SERVER_LOCK_FILENAME ) ) ;
199+ assert . strictEqual ( databasePath , path . join ( stateDir , SERVER_LOCK_DATABASE_FILENAME ) ) ;
192200 } ) ,
193201 ) ;
194202
@@ -209,6 +217,16 @@ layer("serverSingleton", (it) => {
209217 ) ;
210218 } ) ;
211219
220+ it ( "recognizes Node and Bun SQLite busy errors" , ( ) => {
221+ assert . isTrue (
222+ isServerLockBusyError (
223+ Object . assign ( new Error ( "database is locked" ) , { code : "SQLITE_BUSY" } ) ,
224+ ) ,
225+ ) ;
226+ assert . isTrue ( isServerLockBusyError ( Object . assign ( new Error ( "busy" ) , { errno : 5 } ) ) ) ;
227+ assert . isFalse ( isServerLockBusyError ( new Error ( "disk I/O error" ) ) ) ;
228+ } ) ;
229+
212230 it . effect ( "refuses next to a live pre-lock server that wrote no lock" , ( ) =>
213231 Effect . gen ( function * ( ) {
214232 const stateDir = yield * makeStateDir ( ) ;
@@ -272,74 +290,14 @@ layer("serverSingleton", (it) => {
272290 } ) ,
273291 ) ;
274292
275- it . effect ( "a starter cannot reclaim a lock a live holder still owns" , ( ) =>
276- Effect . gen ( function * ( ) {
277- const stateDir = yield * makeStateDir ( ) ;
278- const fs = yield * FileSystem . FileSystem ;
279- const lockPath = yield * serverLockPath ( stateDir ) ;
280-
281- // Starter B holds the directory live. Starter A must refuse rather than
282- // remove B's claim.
283- yield * fs . writeFileString (
284- lockPath ,
285- `{"version":1,"pid":${ process . pid } ,"startedAt":"2026-01-01T00:00:00.000Z"}` ,
286- ) ;
287- yield * Effect . scoped (
288- Effect . gen ( function * ( ) {
289- // Undecodable-by-design: B's pid is live, so A must refuse. Before
290- // the fix, A's unconditional reclaim deleted B's lock and A owned
291- // the directory alongside B.
292- const failure = yield * acquireServerSingleton ( stateDir ) . pipe ( Effect . flip ) ;
293- assert . strictEqual ( failure . _tag , "ServerAlreadyRunningError" ) ;
294- } ) ,
295- ) ;
296-
297- // And the lock is still on disk, owned by B, not reclaimed under it.
298- assert . isTrue ( yield * fs . exists ( lockPath ) ) ;
299- const holder = yield * fs . readFileString ( lockPath ) ;
300- assert . include ( holder , `"pid":${ process . pid } ` ) ;
301- } ) ,
302- ) ;
303-
304- it . effect ( "does not unlink a live holder encountered during reclaim" , ( ) =>
305- Effect . gen ( function * ( ) {
306- const stateDir = yield * makeStateDir ( ) ;
307- const fs = yield * FileSystem . FileSystem ;
308- const lockPath = yield * serverLockPath ( stateDir ) ;
309-
310- // A live successor's claim looks identical on paper except for the pid,
311- // which the liveness check must recognize before reclaim can remove it.
312- yield * fs . writeFileString (
313- lockPath ,
314- `{"version":1,"pid":${ process . pid } ,"startedAt":"2026-01-01T00:00:00.000Z"}` ,
315- ) ;
316-
317- const failure = yield * acquireServerSingleton ( stateDir ) . pipe ( Effect . flip ) ;
318-
319- // Under the old unconditional `fs.remove(lockPath)`, claim would
320- // *succeed* here by deleting this live claim first.
321- assert . strictEqual ( failure . _tag , "ServerAlreadyRunningError" ) ;
322- assert . isTrue ( yield * fs . exists ( lockPath ) ) ;
323- const holder = yield * fs . readFileString ( lockPath ) ;
324- assert . include ( holder , `"pid":${ process . pid } ` ) ;
325- } ) ,
326- ) ;
327-
328- it . effect ( "a partial metadata update is never readable as an empty owner" , ( ) =>
293+ it . effect ( "updates port metadata atomically" , ( ) =>
329294 Effect . gen ( function * ( ) {
330295 const stateDir = yield * makeStateDir ( ) ;
331296 const fs = yield * FileSystem . FileSystem ;
332297 const lockPath = yield * serverLockPath ( stateDir ) ;
333298 yield * Effect . scoped (
334299 Effect . gen ( function * ( ) {
335300 yield * acquireServerSingleton ( stateDir ) ;
336- // Before: the in-place rewrite truncated the file first, so a reader
337- // mid-update decoded an empty holder and could reclaim a live lock.
338- // The atomic write means the bytes on disk are always a full holder —
339- // and a temp-file rename is what arranges that: it stages the payload
340- // in a `.server.lock.*` sibling and swaps it in, which an in-place
341- // truncate never does. Asserting the update *completed* then lets a
342- // concurrent reader never observe an empty or partial lock.
343301 yield * recordServerLockPort ( lockPath , 3775 ) ;
344302
345303 const failure = yield * acquireServerSingleton ( stateDir ) . pipe ( Effect . flip ) ;
@@ -350,10 +308,7 @@ layer("serverSingleton", (it) => {
350308 } ) ,
351309 ) ;
352310
353- // No temp staging directory outlives the update: the payload arrives at
354- // the lock path whole or not at all, which is what makes "partial" reads
355- // above unreachable rather than merely unlucky. The scope above released
356- // the lock itself — only the temp-directory shape is under test.
311+ // No temp staging directory outlives the metadata update.
357312 const leftovers = yield * fs
358313 . readDirectory ( stateDir )
359314 . pipe (
0 commit comments