fix(server): preserve newer runtime state on shutdown #9045
main fromrefs/pull/9045/head·opened View on GitHubShowing 3 changed files with 83 additions and 10 deletions.
🟠 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 beforefs.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.