Showing the changes to scripts (3 of 8 files). Show all
Unified · Split
| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | #!/usr/bin/env node |
| 2 | 2 | |
| 3 | 3 | import * as NodeOS from "node:os"; |
| 4 | +import * as NodeCrypto from "node:crypto"; | |
| 4 | 5 | |
| 5 | 6 | import * as NodeRuntime from "@effect/platform-node/NodeRuntime"; |
| 6 | 7 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| @@ -19,7 +20,12 @@ import * as Schema from "effect/Schema"; · 12 unmodified lines | ||
| 19 | 20 | import { Argument, Command, Flag } from "effect/unstable/cli"; |
| 20 | 21 | import { ChildProcess } from "effect/unstable/process"; |
| 21 | 22 | |
| 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"; | |
| 23 | 29 | import { loadRepoEnv } from "./lib/public-config.ts"; |
| 24 | 30 | |
| 25 | 31 | Object.assign(process.env, loadRepoEnv()); |
| @@ -723,6 +729,7 @@ export function runDevRunnerWithInput(input: DevRunnerCliInput) { · 697 unmodified lines | ||
| 723 | 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 | 731 | } else { |
| 732 | + const path = yield* Path.Path; | |
| 726 | 733 | // acquireRelease, not share-then-addFinalizer: the mapping outlives this |
| 727 | 734 | // process (and reboots), so the cleanup has to be registered atomically |
| 728 | 735 | // 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 | 744 | // A tailnet that isn't up shouldn't stop the dev server from starting — |
| 738 | 745 | // 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( | |
| 745 | 754 | Effect.flatMap((result) => |
| 746 | − result.cleared | |
| 755 | + result.status !== "failed" | |
| 747 | 756 | ? Effect.void |
| 748 | 757 | : 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}`, | |
| 752 | 759 | ), |
| 753 | 760 | ), |
| 754 | 761 | ), |
| 755 | − ).pipe( | |
| 762 | + ); | |
| 763 | + }).pipe( | |
| 756 | 764 | Effect.tapError((error: DevShareError) => |
| 757 | 765 | Effect.logWarning( |
| 758 | 766 | `[dev-runner] could not share on the tailnet: ${error.message}${ |
| @@ -1,11 +1,15 @@ | ||
| 1 | +import * as NodeServices from "@effect/platform-node/NodeServices"; | |
| 1 | 2 | import { assert, describe, it } from "@effect/vitest"; |
| 2 | 3 | import * as Effect from "effect/Effect"; |
| 4 | +import * as FileSystem from "effect/FileSystem"; | |
| 3 | 5 | import * as Layer from "effect/Layer"; |
| 4 | 6 | import * as Sink from "effect/Sink"; |
| 5 | 7 | import * as Stream from "effect/Stream"; |
| 6 | 8 | import { ChildProcessSpawner } from "effect/unstable/process"; |
| 7 | 9 | |
| 8 | 10 | import { |
| 11 | + claimDevShareLease, | |
| 12 | + cleanupOwnedDevShare, | |
| 9 | 13 | type DevShareError, |
| 10 | 14 | DevServeFailedError, |
| 11 | 15 | shareDevServer, |
| @@ -27,11 +31,17 @@ const encode = (value: string) => Stream.make(new TextEncoder().encode(value)); · 15 unmodified lines | ||
| 27 | 31 | * test set the outcome of the `off` (pre-clear) and `serve` calls separately — |
| 28 | 32 | * they are the same subcommand and are told apart by the trailing `off`. |
| 29 | 33 | */ |
| 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 | +}) => | |
| 31 | 40 | Layer.succeed( |
| 32 | 41 | ChildProcessSpawner.ChildProcessSpawner, |
| 33 | 42 | ChildProcessSpawner.make((command) => { |
| 34 | 43 | const args = "args" in command ? (command.args as ReadonlyArray<string>) : []; |
| 44 | + input.calls?.push(args); | |
| 35 | 45 | const result: CallResult = args.includes("status") |
| 36 | 46 | ? { exitCode: 0 } |
| 37 | 47 | : args.includes("off") |
| @@ -41,7 +51,9 @@ const spawnerLayer = (input: { readonly off?: CallResult; readonly serve?: CallR · 3 unmodified lines | ||
| 41 | 51 | return Effect.succeed( |
| 42 | 52 | ChildProcessSpawner.makeHandle({ |
| 43 | 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 | 57 | isRunning: Effect.succeed(false), |
| 46 | 58 | kill: () => Effect.void, |
| 47 | 59 | unref: Effect.succeed(Effect.void), |
| @@ -176,3 +188,52 @@ describe("shareDevServer", () => { · 128 unmodified lines | ||
| 176 | 188 | }), |
| 177 | 189 | ); |
| 178 | 190 | }); |
| 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 | +}); | |
| @@ -22,6 +22,9 @@ import { · 21 unmodified lines | ||
| 22 | 22 | type TailscaleStderrDiagnostic, |
| 23 | 23 | } from "@t3tools/tailscale"; |
| 24 | 24 | 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"; | |
| 25 | 28 | import * as Schema from "effect/Schema"; |
| 26 | 29 | import 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 | |
| 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 | + | |
| 113 | 129 | export const DevShareError = Schema.Union([ |
| 114 | 130 | TailscaleUnavailableError, |
| 115 | 131 | TailnetNameMissingError, |
| 116 | 132 | DevServeFailedError, |
| 133 | + DevShareLeaseClaimError, | |
| 117 | 134 | ]); |
| 118 | 135 | export type DevShareError = typeof DevShareError.Type; |
| 119 | 136 | export const isDevShareError = Schema.is(DevShareError); |
| @@ -163,6 +180,81 @@ export interface DevShareResult { · 43 unmodified lines | ||
| 163 | 180 | readonly host: string; |
| 164 | 181 | } |
| 165 | 182 | |
| 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 | + | |
| 166 | 258 | /** |
| 167 | 259 | * Publishes `webPort` on the tailnet at the same port number and returns the |
| 168 | 260 | * resulting HTTPS URL. Idempotent: re-running replaces any existing mapping. |