History

Fix dev sharing across hosted and restarted servers

Theo Browne committed 3 months ago

Commit c698e6a1 parent b769bb68 files · +185 −62Browse files

Showing the changes to scripts (3 of 8 files). Show all

Unified · Split

3 files
  1. scripts/dev-runner.ts +20 −12
  2. scripts/lib/dev-share.test.ts +63 −2
  3. scripts/lib/dev-share.ts +92 −0
scripts/dev-runner.ts+20−12
@@ -1,6 +1,7 @@
11 #!/usr/bin/env node
22
33 import * as NodeOS from "node:os";
4+import * as NodeCrypto from "node:crypto";
45
56 import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
67 import * as NodeServices from "@effect/platform-node/NodeServices";
@@ -19,7 +20,12 @@ import * as Schema from "effect/Schema"; · 12 unmodified lines
1920 import { Argument, Command, Flag } from "effect/unstable/cli";
2021 import { ChildProcess } from "effect/unstable/process";
2122
22−import { type DevShareError, shareDevServer, unshareDevServer } from "./lib/dev-share.ts";
23+import {
24+ claimDevShareLease,
25+ cleanupOwnedDevShare,
26+ type DevShareError,
27+ shareDevServer,
28+} from "./lib/dev-share.ts";
2329 import { loadRepoEnv } from "./lib/public-config.ts";
2430
2531 Object.assign(process.env, loadRepoEnv());
@@ -723,6 +729,7 @@ export function runDevRunnerWithInput(input: DevRunnerCliInput) { · 697 unmodified lines
723729 "[dev-runner] --share is not supported for dev:desktop (the renderer is pinned to loopback). Use `dev`, which runs the whole browser stack.",
724730 );
725731 } else {
732+ const path = yield* Path.Path;
726733 // acquireRelease, not share-then-addFinalizer: the mapping outlives this
727734 // process (and reboots), so the cleanup has to be registered atomically
728735 // with creating it. An interrupt landing in between would otherwise
@@ -736,23 +743,24 @@ export function runDevRunnerWithInput(input: DevRunnerCliInput) { · 7 unmodified lines
736743 //
737744 // A tailnet that isn't up shouldn't stop the dev server from starting —
738745 // warn, and carry on serving locally.
739− const shared = yield* Effect.acquireRelease(
740− shareDevServer({ webPort: sharedWebPort }),
741− () =>
742− // Serve config outlives this process, so a cleanup that did not
743− // take leaves a tailnet URL pointing at a port nothing serves.
744− unshareDevServer(sharedWebPort).pipe(
746+ const shared = yield* Effect.gen(function* () {
747+ const lease = yield* claimDevShareLease({
748+ leasePath: path.join(baseDir, "dev-share", `${String(sharedWebPort)}.owner`),
749+ ownerId: `${String(process.pid)}:${NodeCrypto.randomUUID()}`,
750+ webPort: sharedWebPort,
751+ });
752+ return yield* Effect.acquireRelease(shareDevServer({ webPort: sharedWebPort }), () =>
753+ cleanupOwnedDevShare(lease).pipe(
745754 Effect.flatMap((result) =>
746− result.cleared
755+ result.status !== "failed"
747756 ? Effect.void
748757 : Effect.logWarning(
749− `[dev-runner] could not remove the tailnet mapping for port ${String(sharedWebPort)}${
750− result.explanation ? `: ${result.explanation}` : ""
751− }. Remove it with \`tailscale serve --https=${String(sharedWebPort)} off\`.`,
758+ `[dev-runner] could not safely clean up the tailnet mapping for port ${String(sharedWebPort)}: ${result.explanation}`,
752759 ),
753760 ),
754761 ),
755− ).pipe(
762+ );
763+ }).pipe(
756764 Effect.tapError((error: DevShareError) =>
757765 Effect.logWarning(
758766 `[dev-runner] could not share on the tailnet: ${error.message}${
scripts/lib/dev-share.test.ts+63−2
@@ -1,11 +1,15 @@
1+import * as NodeServices from "@effect/platform-node/NodeServices";
12 import { assert, describe, it } from "@effect/vitest";
23 import * as Effect from "effect/Effect";
4+import * as FileSystem from "effect/FileSystem";
35 import * as Layer from "effect/Layer";
46 import * as Sink from "effect/Sink";
57 import * as Stream from "effect/Stream";
68 import { ChildProcessSpawner } from "effect/unstable/process";
79
810 import {
11+ claimDevShareLease,
12+ cleanupOwnedDevShare,
913 type DevShareError,
1014 DevServeFailedError,
1115 shareDevServer,
@@ -27,11 +31,17 @@ const encode = (value: string) => Stream.make(new TextEncoder().encode(value)); · 15 unmodified lines
2731 * test set the outcome of the `off` (pre-clear) and `serve` calls separately —
2832 * they are the same subcommand and are told apart by the trailing `off`.
2933 */
30−const spawnerLayer = (input: { readonly off?: CallResult; readonly serve?: CallResult }) =>
34+const spawnerLayer = (input: {
35+ readonly off?: CallResult;
36+ readonly serve?: CallResult;
37+ readonly onOff?: Effect.Effect<void>;
38+ readonly calls?: Array<ReadonlyArray<string>>;
39+}) =>
3140 Layer.succeed(
3241 ChildProcessSpawner.ChildProcessSpawner,
3342 ChildProcessSpawner.make((command) => {
3443 const args = "args" in command ? (command.args as ReadonlyArray<string>) : [];
44+ input.calls?.push(args);
3545 const result: CallResult = args.includes("status")
3646 ? { exitCode: 0 }
3747 : args.includes("off")
@@ -41,7 +51,9 @@ const spawnerLayer = (input: { readonly off?: CallResult; readonly serve?: CallR · 3 unmodified lines
4151 return Effect.succeed(
4252 ChildProcessSpawner.makeHandle({
4353 pid: ChildProcessSpawner.ProcessId(1),
44− exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(result.exitCode)),
54+ exitCode: (args.includes("off") ? (input.onOff ?? Effect.void) : Effect.void).pipe(
55+ Effect.as(ChildProcessSpawner.ExitCode(result.exitCode)),
56+ ),
4557 isRunning: Effect.succeed(false),
4658 kill: () => Effect.void,
4759 unref: Effect.succeed(Effect.void),
@@ -176,3 +188,52 @@ describe("shareDevServer", () => { · 128 unmodified lines
176188 }),
177189 );
178190 });
191+
192+it.layer(NodeServices.layer)("dev share cleanup ownership", (it) => {
193+ it.effect("restores a newer runner's mapping when ownership changes during cleanup", () =>
194+ Effect.gen(function* () {
195+ const fileSystem = yield* FileSystem.FileSystem;
196+ const directory = yield* fileSystem.makeTempDirectoryScoped({
197+ prefix: "t3-dev-share-lease-",
198+ });
199+ const leasePath = `${directory}/5788.owner`;
200+ const oldLease = { leasePath, ownerId: "old-runner", webPort: 5788 };
201+ const calls: Array<ReadonlyArray<string>> = [];
202+
203+ yield* claimDevShareLease(oldLease);
204+ const result = yield* cleanupOwnedDevShare(oldLease).pipe(
205+ Effect.provide(
206+ spawnerLayer({
207+ calls,
208+ onOff: fileSystem.writeFileString(leasePath, "new-runner").pipe(Effect.orDie),
209+ }),
210+ ),
211+ );
212+
213+ assert.equal(result.status, "restored");
214+ assert.isTrue(calls.some((args) => args.includes("off")));
215+ assert.isTrue(calls.some((args) => args.includes("http://127.0.0.1:5788")));
216+ }),
217+ );
218+
219+ it.effect("leaves the mapping alone when a newer runner already owns it", () =>
220+ Effect.gen(function* () {
221+ const fileSystem = yield* FileSystem.FileSystem;
222+ const directory = yield* fileSystem.makeTempDirectoryScoped({
223+ prefix: "t3-dev-share-lease-",
224+ });
225+ const leasePath = `${directory}/5788.owner`;
226+ const oldLease = { leasePath, ownerId: "old-runner", webPort: 5788 };
227+ const calls: Array<ReadonlyArray<string>> = [];
228+
229+ yield* claimDevShareLease(oldLease);
230+ yield* claimDevShareLease({ ...oldLease, ownerId: "new-runner" });
231+ const result = yield* cleanupOwnedDevShare(oldLease).pipe(
232+ Effect.provide(spawnerLayer({ calls })),
233+ );
234+
235+ assert.equal(result.status, "superseded");
236+ assert.isEmpty(calls);
237+ }),
238+ );
239+});
scripts/lib/dev-share.ts+92−0
@@ -22,6 +22,9 @@ import { · 21 unmodified lines
2222 type TailscaleStderrDiagnostic,
2323 } from "@t3tools/tailscale";
2424 import * as Effect from "effect/Effect";
25+import * as FileSystem from "effect/FileSystem";
26+import * as Option from "effect/Option";
27+import * as Path from "effect/Path";
2528 import * as Schema from "effect/Schema";
2629 import type { ChildProcessSpawner } from "effect/unstable/process";
2730
@@ -110,10 +113,24 @@ export class DevServeFailedError extends Schema.TaggedErrorClass<DevServeFailedE · 82 unmodified lines
110113 }
111114 }
112115
116+export class DevShareLeaseClaimError extends Schema.TaggedErrorClass<DevShareLeaseClaimError>()(
117+ "DevShareLeaseClaimError",
118+ { leasePath: Schema.String, cause: Schema.Defect() },
119+) {
120+ override get message(): string {
121+ return `could not claim cleanup ownership at ${this.leasePath}`;
122+ }
123+
124+ get hint(): string {
125+ return "Check that the dev data directory is writable.";
126+ }
127+}
128+
113129 export const DevShareError = Schema.Union([
114130 TailscaleUnavailableError,
115131 TailnetNameMissingError,
116132 DevServeFailedError,
133+ DevShareLeaseClaimError,
117134 ]);
118135 export type DevShareError = typeof DevShareError.Type;
119136 export const isDevShareError = Schema.is(DevShareError);
@@ -163,6 +180,81 @@ export interface DevShareResult { · 43 unmodified lines
163180 readonly host: string;
164181 }
165182
183+export interface DevShareLease {
184+ readonly leasePath: string;
185+ readonly ownerId: string;
186+ readonly webPort: number;
187+}
188+
189+export const claimDevShareLease = Effect.fn("devShare.claimDevShareLease")(function* (
190+ lease: DevShareLease,
191+) {
192+ const fileSystem = yield* FileSystem.FileSystem;
193+ const path = yield* Path.Path;
194+ yield* fileSystem.makeDirectory(path.dirname(lease.leasePath), { recursive: true }).pipe(
195+ Effect.andThen(fileSystem.writeFileString(lease.leasePath, lease.ownerId)),
196+ Effect.mapError((cause) => new DevShareLeaseClaimError({ leasePath: lease.leasePath, cause })),
197+ );
198+ return lease;
199+});
200+
201+export type DevShareCleanupResult =
202+ | { readonly status: "cleared" | "superseded" | "restored" }
203+ | { readonly status: "failed"; readonly explanation: string };
204+
205+/**
206+ * Clears only the mapping this runner owns. If a successor claims the lease
207+ * while `tailscale serve off` is in flight, restore the same port mapping so
208+ * the old finalizer cannot silently disconnect the new runner.
209+ */
210+export const cleanupOwnedDevShare = Effect.fn("devShare.cleanupOwnedDevShare")(function* (
211+ lease: DevShareLease,
212+): Effect.fn.Return<
213+ DevShareCleanupResult,
214+ never,
215+ FileSystem.FileSystem | ChildProcessSpawner.ChildProcessSpawner
216+> {
217+ const fileSystem = yield* FileSystem.FileSystem;
218+ const readOwner = fileSystem
219+ .readFileString(lease.leasePath)
220+ .pipe(Effect.option, Effect.map(Option.getOrUndefined));
221+ const ownerBefore = yield* readOwner;
222+ if (ownerBefore !== lease.ownerId) {
223+ return { status: "superseded" };
224+ }
225+
226+ const result = yield* unshareDevServer(lease.webPort);
227+ if (!result.cleared) {
228+ return {
229+ status: "failed",
230+ explanation:
231+ result.explanation ??
232+ `could not remove the tailnet mapping for port ${String(lease.webPort)}`,
233+ };
234+ }
235+
236+ const ownerAfter = yield* readOwner;
237+ if (ownerAfter === lease.ownerId) {
238+ return { status: "cleared" };
239+ }
240+
241+ return yield* ensureTailscaleServe({
242+ localPort: lease.webPort,
243+ servePort: lease.webPort,
244+ }).pipe(
245+ Effect.as({ status: "restored" } as const),
246+ Effect.catch((cause: TailscaleCommandError) =>
247+ Effect.succeed({
248+ status: "failed",
249+ explanation:
250+ explainCommandFailure(cause) ??
251+ `a newer runner claimed port ${String(lease.webPort)}, but its mapping could not be restored`,
252+ } as const),
253+ ),
254+ Effect.uninterruptible,
255+ );
256+});
257+
166258 /**
167259 * Publishes `webPort` on the tailnet at the same port number and returns the
168260 * resulting HTTPS URL. Idempotent: re-running replaces any existing mapping.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t