Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions apps/server/src/preview/PortScanner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ const layerForeground = Layer.mock(BackgroundPolicy.BackgroundPolicy)({

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

// Fake process tables must not depend on which pid the test worker got.
const SERVER_PID = 100;
const layerServerPid = Layer.succeed(HostProcess.ProcessId, SERVER_PID);

const layerProbeFailure = (
run: ProcessRunner.ProcessRunner["Service"]["run"],
fetch: typeof globalThis.fetch = globalThis.fetch,
Expand All @@ -62,6 +66,7 @@ const layerProbeFailure = (
fileSystem,
Layer.succeed(ProcessRunner.ProcessRunner, { run }),
Layer.succeed(HostProcess.Platform, "linux"),
layerServerPid,
FetchHttpClient.layer.pipe(Layer.provide(Layer.succeed(FetchHttpClient.Fetch, fetch))),
),
),
Expand Down Expand Up @@ -881,9 +886,9 @@ effectIt.effect.each(["linux", "darwin"] as const)(
)
: Effect.succeed(processOutput(lsofStdout));
}
// A direct child of this process, so stop refuses it without signaling anything.
// A direct child of the server, so stop refuses it without signaling anything.
if (input.args[0] === "-A") {
return Effect.succeed(processOutput(`4242 ${process.pid} 4242\n`));
return Effect.succeed(processOutput(`4242 ${SERVER_PID} 4242\n`));
}
if (environmentFails) {
return Effect.fail(
Expand All @@ -904,6 +909,7 @@ effectIt.effect.each(["linux", "darwin"] as const)(
},
}),
Layer.succeed(HostProcess.Platform, platform),
layerServerPid,
FetchHttpClient.layer.pipe(
Layer.provide(
Layer.succeed(FetchHttpClient.Fetch, (input) => {
Expand Down
3 changes: 2 additions & 1 deletion apps/server/src/preview/PortScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,7 @@ export const make = Effect.gen(function* PortDiscoveryMake() {
const processRunner = yield* ProcessRunner.ProcessRunner;
const fileSystem = yield* FileSystem.FileSystem;
const hostPlatform = yield* HostProcess.Platform;
const serverPid = yield* HostProcess.ProcessId;
const listenerDiscovery = (yield* HostProcess.Environment)[PORT_DISCOVERY_ENV] !== "0";
const httpClient = (yield* HttpClient.HttpClient).pipe(HttpClient.withScope);
const backgroundPolicy = yield* BackgroundPolicy.BackgroundPolicy;
Expand Down Expand Up @@ -1175,7 +1176,7 @@ export const make = Effect.gen(function* PortDiscoveryMake() {
const table = yield* readProcessTable;
// It exited after the scan, so there is nothing left to stop.
if (!table.has(input.pid)) return;
const group = stoppableProcessGroup(table, input.pid, process.pid);
const group = stoppableProcessGroup(table, input.pid, serverPid);
if (group === null) return yield* new PreviewStopLocalServerError({ reason: "shared-group" });
// False when the whole group already exited.
const signalGroup = (signal: "SIGTERM" | "SIGKILL") =>
Expand Down
4 changes: 4 additions & 0 deletions packages/shared/src/HostProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ export const Addresses = Context.Reference<Effect.Effect<ReadonlySet<string>>>(
},
);

export const ProcessId = Context.Reference<number>("@t3tools/shared/HostProcess/ProcessId", {
defaultValue: () => process.pid,
});

export const UserId = Context.Reference<number | undefined>("@t3tools/shared/HostProcess/UserId", {
defaultValue: () => process.getuid?.(),
});
Expand Down
Loading