Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(server): preserve newer runtime state on shutdown #9045

Closedreubengrussell(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/9045/headopened View on GitHub

Showing 3 changed files with 83 additions and 10 deletions.

x
0 of 3 viewed, 3 left
10 additions and 7 deletionsapps/server/src/server.tsModified
View file
@@ -495,7 +495,7 @@
const server = yield* HttpServer.HttpServer;
const address = server.address;
if (typeof address === "string" || !("port" in address)) {
- return;
+ return null;
​
const state = yield* makePersistedServerRuntimeState({
@@ -510,13 +510,16 @@
Effect.logWarning("Failed to persist server runtime state", { cause }),
+ return state;
}),
- () =>
- clearPersistedServerRuntimeState(config.serverRuntimeStatePath).pipe(
- Effect.catchCause((cause) =>
- Effect.logWarning("Failed to clear server runtime state", { cause }),
- ),
- ),
+ (state) =>
+ state === null
+ ? Effect.void
+ : clearPersistedServerRuntimeState(config.serverRuntimeStatePath, state).pipe(
Thread on line 518
macroscopeapp[bot](GitHub profile, opens in a new tab)commented

🟠 High src/server.ts:518

The old server can delete a newer server's runtime-state file: clearPersistedServerRuntimeState verifies ownership with readPersistedServerRuntimeState, then performs fs.remove separately, allowing an overlapping startup to replace the file between those operations. This removes the active endpoint record and breaks discovery; the check-and-remove must be atomic (or otherwise revalidate ownership immediately before deletion).

Also found in 1 other location(s)

apps/server/src/serverRuntimeState.ts:96

The owner check is a time-of-check/time-of-use race: after readPersistedServerRuntimeState(path) confirms that the old instance owns the file, a newer server can atomically replace the state before fs.remove(path) runs. The old shutdown then unlinks the newer server's record, recreating the endpoint-discovery failure this guard is intended to prevent.

🤖 Copy this AI Prompt to have your agent fix this:
In file @apps/server/src/server.ts around line 518:

The old server can delete a newer server's runtime-state file: `clearPersistedServerRuntimeState` verifies ownership with `readPersistedServerRuntimeState`, then performs `fs.remove` separately, allowing an overlapping startup to replace the file between those operations. This removes the active endpoint record and breaks discovery; the check-and-remove must be atomic (or otherwise revalidate ownership immediately before deletion).

Also found in 1 other location(s):
- apps/server/src/serverRuntimeState.ts:96 -- The owner check is a time-of-check/time-of-use race: after `readPersistedServerRuntimeState(path)` confirms that the old instance owns the file, a newer server can atomically replace the state before `fs.remove(path)` runs. The old shutdown then unlinks the newer server's record, recreating the endpoint-discovery failure this guard is intended to prevent.
+ Effect.catchCause((cause) =>
+ Effect.logWarning("Failed to clear server runtime state", { cause }),
+ ),