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 @@
1#!/usr/bin/env node1#!/usr/bin/env node
2 2
3import * as NodeOS from "node:os";3import * as NodeOS from "node:os";
4import * as NodeCrypto from "node:crypto";
4 5
5import * as NodeRuntime from "@effect/platform-node/NodeRuntime";6import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
6import * as NodeServices from "@effect/platform-node/NodeServices";7import * as NodeServices from "@effect/platform-node/NodeServices";
@@ -19,7 +20,12 @@ import * as Schema from "effect/Schema"; · 12 unmodified lines
19import { Argument, Command, Flag } from "effect/unstable/cli";20import { Argument, Command, Flag } from "effect/unstable/cli";
20import { ChildProcess } from "effect/unstable/process";21import { ChildProcess } from "effect/unstable/process";
21 22
22import { type DevShareError, shareDevServer, unshareDevServer } from "./lib/dev-share.ts";23import {
24 claimDevShareLease,
25 cleanupOwnedDevShare,
26 type DevShareError,
27 shareDevServer,
28} from "./lib/dev-share.ts";
23import { loadRepoEnv } from "./lib/public-config.ts";29import { loadRepoEnv } from "./lib/public-config.ts";
24 30
25Object.assign(process.env, loadRepoEnv());31Object.assign(process.env, loadRepoEnv());
@@ -723,6 +729,7 @@ export function runDevRunnerWithInput(input: DevRunnerCliInput) { · 697 unmodified lines
723 "[dev-runner] --share is not supported for dev:desktop (the renderer is pinned to loopback). Use `dev`, which runs the whole browser stack.",729 "[dev-runner] --share is not supported for dev:desktop (the renderer is pinned to loopback). Use `dev`, which runs the whole browser stack.",
724 );730 );
725 } else {731 } else {
732 const path = yield* Path.Path;
726 // acquireRelease, not share-then-addFinalizer: the mapping outlives this733 // acquireRelease, not share-then-addFinalizer: the mapping outlives this
727 // process (and reboots), so the cleanup has to be registered atomically734 // process (and reboots), so the cleanup has to be registered atomically
728 // with creating it. An interrupt landing in between would otherwise735 // with creating it. An interrupt landing in between would otherwise
@@ -736,23 +743,24 @@ export function runDevRunnerWithInput(input: DevRunnerCliInput) { · 7 unmodified lines
736 //743 //
737 // A tailnet that isn't up shouldn't stop the dev server from starting —744 // A tailnet that isn't up shouldn't stop the dev server from starting —
738 // warn, and carry on serving locally.745 // warn, and carry on serving locally.
739 const shared = yield* Effect.acquireRelease(746 const shared = yield* Effect.gen(function* () {
740 shareDevServer({ webPort: sharedWebPort }),747 const lease = yield* claimDevShareLease({
741 () =>748 leasePath: path.join(baseDir, "dev-share", `${String(sharedWebPort)}.owner`),
742 // Serve config outlives this process, so a cleanup that did not749 ownerId: `${String(process.pid)}:${NodeCrypto.randomUUID()}`,
743 // take leaves a tailnet URL pointing at a port nothing serves.750 webPort: sharedWebPort,
744 unshareDevServer(sharedWebPort).pipe(751 });
752 return yield* Effect.acquireRelease(shareDevServer({ webPort: sharedWebPort }), () =>
753 cleanupOwnedDevShare(lease).pipe(
745 Effect.flatMap((result) =>754 Effect.flatMap((result) =>
746 result.cleared755 result.status !== "failed"
747 ? Effect.void756 ? Effect.void
748 : Effect.logWarning(757 : Effect.logWarning(
749 `[dev-runner] could not remove the tailnet mapping for port ${String(sharedWebPort)}${758 `[dev-runner] could not safely clean up the tailnet mapping for port ${String(sharedWebPort)}: ${result.explanation}`,
750 result.explanation ? `: ${result.explanation}` : ""
751 }. Remove it with \`tailscale serve --https=${String(sharedWebPort)} off\`.`,
752 ),759 ),
753 ),760 ),
754 ),761 ),
755 ).pipe(762 );
763 }).pipe(
756 Effect.tapError((error: DevShareError) =>764 Effect.tapError((error: DevShareError) =>
757 Effect.logWarning(765 Effect.logWarning(
758 `[dev-runner] could not share on the tailnet: ${error.message}${766 `[dev-runner] could not share on the tailnet: ${error.message}${
scripts/lib/dev-share.test.ts+63−2
@@ -1,11 +1,15 @@
1import * as NodeServices from "@effect/platform-node/NodeServices";
1import { assert, describe, it } from "@effect/vitest";2import { assert, describe, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
3import * as Layer from "effect/Layer";5import * as Layer from "effect/Layer";
4import * as Sink from "effect/Sink";6import * as Sink from "effect/Sink";
5import * as Stream from "effect/Stream";7import * as Stream from "effect/Stream";
6import { ChildProcessSpawner } from "effect/unstable/process";8import { ChildProcessSpawner } from "effect/unstable/process";
7 9
8import {10import {
11 claimDevShareLease,
12 cleanupOwnedDevShare,
9 type DevShareError,13 type DevShareError,
10 DevServeFailedError,14 DevServeFailedError,
11 shareDevServer,15 shareDevServer,
@@ -27,11 +31,17 @@ const encode = (value: string) => Stream.make(new TextEncoder().encode(value)); · 15 unmodified lines
27 * test set the outcome of the `off` (pre-clear) and `serve` calls separately —31 * test set the outcome of the `off` (pre-clear) and `serve` calls separately —
28 * they are the same subcommand and are told apart by the trailing `off`.32 * they are the same subcommand and are told apart by the trailing `off`.
29 */33 */
30const spawnerLayer = (input: { readonly off?: CallResult; readonly serve?: CallResult }) =>34const spawnerLayer = (input: {
35 readonly off?: CallResult;
36 readonly serve?: CallResult;
37 readonly onOff?: Effect.Effect<void>;
38 readonly calls?: Array<ReadonlyArray<string>>;
39}) =>
31 Layer.succeed(40 Layer.succeed(
32 ChildProcessSpawner.ChildProcessSpawner,41 ChildProcessSpawner.ChildProcessSpawner,
33 ChildProcessSpawner.make((command) => {42 ChildProcessSpawner.make((command) => {
34 const args = "args" in command ? (command.args as ReadonlyArray<string>) : [];43 const args = "args" in command ? (command.args as ReadonlyArray<string>) : [];
44 input.calls?.push(args);
35 const result: CallResult = args.includes("status")45 const result: CallResult = args.includes("status")
36 ? { exitCode: 0 }46 ? { exitCode: 0 }
37 : args.includes("off")47 : args.includes("off")
@@ -41,7 +51,9 @@ const spawnerLayer = (input: { readonly off?: CallResult; readonly serve?: CallR · 3 unmodified lines
41 return Effect.succeed(51 return Effect.succeed(
42 ChildProcessSpawner.makeHandle({52 ChildProcessSpawner.makeHandle({
43 pid: ChildProcessSpawner.ProcessId(1),53 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 ),
45 isRunning: Effect.succeed(false),57 isRunning: Effect.succeed(false),
46 kill: () => Effect.void,58 kill: () => Effect.void,
47 unref: Effect.succeed(Effect.void),59 unref: Effect.succeed(Effect.void),
@@ -176,3 +188,52 @@ describe("shareDevServer", () => { · 128 unmodified lines
176 }),188 }),
177 );189 );
178});190});
191
192it.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
22 type TailscaleStderrDiagnostic,22 type TailscaleStderrDiagnostic,
23} from "@t3tools/tailscale";23} from "@t3tools/tailscale";
24import * as Effect from "effect/Effect";24import * as Effect from "effect/Effect";
25import * as FileSystem from "effect/FileSystem";
26import * as Option from "effect/Option";
27import * as Path from "effect/Path";
25import * as Schema from "effect/Schema";28import * as Schema from "effect/Schema";
26import type { ChildProcessSpawner } from "effect/unstable/process";29import type { ChildProcessSpawner } from "effect/unstable/process";
27 30
@@ -110,10 +113,24 @@ export class DevServeFailedError extends Schema.TaggedErrorClass<DevServeFailedE · 82 unmodified lines
110 }113 }
111}114}
112 115
116export 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
113export const DevShareError = Schema.Union([129export const DevShareError = Schema.Union([
114 TailscaleUnavailableError,130 TailscaleUnavailableError,
115 TailnetNameMissingError,131 TailnetNameMissingError,
116 DevServeFailedError,132 DevServeFailedError,
133 DevShareLeaseClaimError,
117]);134]);
118export type DevShareError = typeof DevShareError.Type;135export type DevShareError = typeof DevShareError.Type;
119export const isDevShareError = Schema.is(DevShareError);136export const isDevShareError = Schema.is(DevShareError);
@@ -163,6 +180,81 @@ export interface DevShareResult { · 43 unmodified lines
163 readonly host: string;180 readonly host: string;
164}181}
165 182
183export interface DevShareLease {
184 readonly leasePath: string;
185 readonly ownerId: string;
186 readonly webPort: number;
187}
188
189export 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
201export 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 */
210export 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
166/**258/**
167 * Publishes `webPort` on the tailnet at the same port number and returns the259 * Publishes `webPort` on the tailnet at the same port number and returns the
168 * resulting HTTPS URL. Idempotent: re-running replaces any existing mapping.260 * 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