Skip to content

Commit 0d22f86

Browse files
STR-2927 preserve newer T3 runtime ownership
1 parent f071907 commit 0d22f86

3 files changed

Lines changed: 83 additions & 10 deletions

File tree

‎apps/server/src/server.ts‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ export const makeServerLayer = Layer.unwrap(
495495
const server = yield* HttpServer.HttpServer;
496496
const address = server.address;
497497
if (typeof address === "string" || !("port" in address)) {
498-
return;
498+
return null;
499499
}
500500

501501
const state = yield* makePersistedServerRuntimeState({
@@ -510,13 +510,16 @@ export const makeServerLayer = Layer.unwrap(
510510
Effect.logWarning("Failed to persist server runtime state", { cause }),
511511
),
512512
);
513+
return state;
513514
}),
514-
() =>
515-
clearPersistedServerRuntimeState(config.serverRuntimeStatePath).pipe(
516-
Effect.catchCause((cause) =>
517-
Effect.logWarning("Failed to clear server runtime state", { cause }),
518-
),
519-
),
515+
(state) =>
516+
state === null
517+
? Effect.void
518+
: clearPersistedServerRuntimeState(config.serverRuntimeStatePath, state).pipe(
519+
Effect.catchCause((cause) =>
520+
Effect.logWarning("Failed to clear server runtime state", { cause }),
521+
),
522+
),
520523
),
521524
);
522525
const tailscaleServeLayer = config.tailscaleServeEnabled

‎apps/server/src/serverRuntimeState.test.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,64 @@ describe("serverRuntimeState", () => {
7878
}).pipe(Effect.provide(NodeServices.layer)),
7979
);
8080

81+
it.effect("clears runtime state owned by the stopping server", () =>
82+
Effect.gen(function* () {
83+
const fileSystem = yield* FileSystem.FileSystem;
84+
const path = yield* Path.Path;
85+
const root = yield* fileSystem.makeTempDirectoryScoped({
86+
prefix: "t3-server-runtime-state-test-",
87+
});
88+
const statePath = path.join(root, "server.json");
89+
const state: ServerRuntimeState.PersistedServerRuntimeState = {
90+
version: 1,
91+
pid: 123,
92+
port: 4_971,
93+
origin: "http://127.0.0.1:4971",
94+
startedAt: "2026-06-20T00:00:00.000Z",
95+
};
96+
yield* ServerRuntimeState.persistServerRuntimeState({ path: statePath, state });
97+
98+
yield* ServerRuntimeState.clearPersistedServerRuntimeState(statePath, state);
99+
100+
const restored = yield* ServerRuntimeState.readPersistedServerRuntimeState(statePath);
101+
assert.isTrue(Option.isNone(restored));
102+
}).pipe(Effect.provide(NodeServices.layer)),
103+
);
104+
105+
it.effect("does not clear runtime state replaced by a newer server", () =>
106+
Effect.gen(function* () {
107+
const fileSystem = yield* FileSystem.FileSystem;
108+
const path = yield* Path.Path;
109+
const root = yield* fileSystem.makeTempDirectoryScoped({
110+
prefix: "t3-server-runtime-state-test-",
111+
});
112+
const statePath = path.join(root, "server.json");
113+
const oldState: ServerRuntimeState.PersistedServerRuntimeState = {
114+
version: 1,
115+
pid: 123,
116+
port: 4_971,
117+
origin: "http://127.0.0.1:4971",
118+
startedAt: "2026-06-20T00:00:00.000Z",
119+
};
120+
const currentState: ServerRuntimeState.PersistedServerRuntimeState = {
121+
...oldState,
122+
pid: 456,
123+
port: 4_972,
124+
origin: "http://127.0.0.1:4972",
125+
startedAt: "2026-06-20T00:01:00.000Z",
126+
};
127+
yield* ServerRuntimeState.persistServerRuntimeState({
128+
path: statePath,
129+
state: currentState,
130+
});
131+
132+
yield* ServerRuntimeState.clearPersistedServerRuntimeState(statePath, oldState);
133+
134+
const restored = yield* ServerRuntimeState.readPersistedServerRuntimeState(statePath);
135+
assert.deepEqual(Option.getOrThrow(restored), currentState);
136+
}).pipe(Effect.provide(NodeServices.layer)),
137+
);
138+
81139
it.effect("preserves malformed state decode failures", () => {
82140
const logs: CapturedLog[] = [];
83141
const logger = Logger.make(({ fiber, message }) => {

‎apps/server/src/serverRuntimeState.ts‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,19 @@ export const persistServerRuntimeState = (input: {
7979
),
8080
);
8181

82-
export const clearPersistedServerRuntimeState = (path: string) =>
83-
Effect.gen(function* () {
82+
export const clearPersistedServerRuntimeState = Effect.fn("clearPersistedServerRuntimeState")(
83+
function* (path: string, expectedOwner?: Pick<PersistedServerRuntimeState, "pid" | "startedAt">) {
84+
if (expectedOwner) {
85+
const current = yield* readPersistedServerRuntimeState(path);
86+
if (
87+
Option.isNone(current) ||
88+
current.value.pid !== expectedOwner.pid ||
89+
current.value.startedAt !== expectedOwner.startedAt
90+
) {
91+
return;
92+
}
93+
}
94+
8495
const fs = yield* FileSystem.FileSystem;
8596
yield* fs.remove(path, { force: true }).pipe(
8697
Effect.mapError(
@@ -102,7 +113,8 @@ export const clearPersistedServerRuntimeState = (path: string) =>
102113
),
103114
}),
104115
);
105-
});
116+
},
117+
);
106118

107119
export const readPersistedServerRuntimeState = (path: string) =>
108120
Effect.gen(function* () {

0 commit comments

Comments
 (0)