Skip to content

Commit f3bb666

Browse files
fix(server): stopping a preview server no longer depends on the test worker's pid
The stop path compared the fake process table in PortScanner tests against the real process.pid, so a test failed with shared-group whenever the worker was assigned one of the fixture pids. The server pid is now read from HostProcess.ProcessId, which defaults to process.pid, and tests with a fake table provide a fixed one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 8de2f7c commit f3bb666

3 files changed

Lines changed: 14 additions & 3 deletions

File tree

‎apps/server/src/preview/PortScanner.test.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ const layerForeground = Layer.mock(BackgroundPolicy.BackgroundPolicy)({
5151

5252
const layerScanner = PortScanner.layer.pipe(Layer.provide(layerForeground));
5353

54+
// Fake process tables must not depend on which pid the test worker got.
55+
const SERVER_PID = 100;
56+
const layerServerPid = Layer.succeed(HostProcess.ProcessId, SERVER_PID);
57+
5458
const layerProbeFailure = (
5559
run: ProcessRunner.ProcessRunner["Service"]["run"],
5660
fetch: typeof globalThis.fetch = globalThis.fetch,
@@ -62,6 +66,7 @@ const layerProbeFailure = (
6266
fileSystem,
6367
Layer.succeed(ProcessRunner.ProcessRunner, { run }),
6468
Layer.succeed(HostProcess.Platform, "linux"),
69+
layerServerPid,
6570
FetchHttpClient.layer.pipe(Layer.provide(Layer.succeed(FetchHttpClient.Fetch, fetch))),
6671
),
6772
),
@@ -881,9 +886,9 @@ effectIt.effect.each(["linux", "darwin"] as const)(
881886
)
882887
: Effect.succeed(processOutput(lsofStdout));
883888
}
884-
// A direct child of this process, so stop refuses it without signaling anything.
889+
// A direct child of the server, so stop refuses it without signaling anything.
885890
if (input.args[0] === "-A") {
886-
return Effect.succeed(processOutput(`4242 ${process.pid} 4242\n`));
891+
return Effect.succeed(processOutput(`4242 ${SERVER_PID} 4242\n`));
887892
}
888893
if (environmentFails) {
889894
return Effect.fail(
@@ -904,6 +909,7 @@ effectIt.effect.each(["linux", "darwin"] as const)(
904909
},
905910
}),
906911
Layer.succeed(HostProcess.Platform, platform),
912+
layerServerPid,
907913
FetchHttpClient.layer.pipe(
908914
Layer.provide(
909915
Layer.succeed(FetchHttpClient.Fetch, (input) => {

‎apps/server/src/preview/PortScanner.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ export const make = Effect.gen(function* PortDiscoveryMake() {
507507
const processRunner = yield* ProcessRunner.ProcessRunner;
508508
const fileSystem = yield* FileSystem.FileSystem;
509509
const hostPlatform = yield* HostProcess.Platform;
510+
const serverPid = yield* HostProcess.ProcessId;
510511
const listenerDiscovery = (yield* HostProcess.Environment)[PORT_DISCOVERY_ENV] !== "0";
511512
const httpClient = (yield* HttpClient.HttpClient).pipe(HttpClient.withScope);
512513
const backgroundPolicy = yield* BackgroundPolicy.BackgroundPolicy;
@@ -1175,7 +1176,7 @@ export const make = Effect.gen(function* PortDiscoveryMake() {
11751176
const table = yield* readProcessTable;
11761177
// It exited after the scan, so there is nothing left to stop.
11771178
if (!table.has(input.pid)) return;
1178-
const group = stoppableProcessGroup(table, input.pid, process.pid);
1179+
const group = stoppableProcessGroup(table, input.pid, serverPid);
11791180
if (group === null) return yield* new PreviewStopLocalServerError({ reason: "shared-group" });
11801181
// False when the whole group already exited.
11811182
const signalGroup = (signal: "SIGTERM" | "SIGKILL") =>

‎packages/shared/src/HostProcess.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ export const Addresses = Context.Reference<Effect.Effect<ReadonlySet<string>>>(
121121
},
122122
);
123123

124+
export const ProcessId = Context.Reference<number>("@t3tools/shared/HostProcess/ProcessId", {
125+
defaultValue: () => process.pid,
126+
});
127+
124128
export const UserId = Context.Reference<number | undefined>("@t3tools/shared/HostProcess/UserId", {
125129
defaultValue: () => process.getuid?.(),
126130
});

0 commit comments

Comments
 (0)