fix(connect): relay client updates itself and skips incompatible cloudflared #17275
Mergedjuliusmarmingemerged 10 commits into
main fromrefs/pull/17275/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
juliusmarminge added labelsmacroscope-review
- apps/server/src/cloud/ManagedEndpointRuntime.ts line 465Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -328,3 +394,13 @@328394 yield* stopActive;329395330396 const executable = yield* relayClient.resolve;397+ // A linked host converges on the pinned managed release: install it when it398+ // is missing, and when only an older managed release or a PATH binary is399+ // available. An explicit override is the user's choice and is left alone.400+ if (401+ executable.status === "missing" ||402+ (executable.status === "available" &&403+ executable.source !== "override" &&404+ !RelayClient.isPinnedManagedRelayClient(executable))405+ ) {406+ yield* ensurePinnedRelayClient;
- packages/shared/src/relayClient.ts lines 263 to 270OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -209,0 +250,21 @@250+ // in place, so the version is read from the binary, never from its folder name.251+ const versionCache = new Map<252+ string,253+ { readonly stamp: string; readonly version: string | null }254+ >();255+ const probeVersion = Effect.fn("cloudflared.probeVersion")(function* (executablePath: string) {256+ const info = yield* fileSystem.stat(executablePath).pipe(Effect.option);257+ if (Option.isNone(info) || info.value.type !== "File") return null;258+ if (platform !== "win32" && (info.value.mode & 0o111) === 0) return null;259+ const mtime = Option.getOrUndefined(info.value.mtime)?.getTime() ?? 0;260+ const stamp = `${String(info.value.size)}:${mtime}`;261+ const cached = versionCache.get(executablePath);262+ if (cached?.stamp === stamp) return cached.version;263+ const version = yield* spawner264+ .string(ChildProcess.make(executablePath, ["version"], { stdin: "ignore", stderr: "ignore" }))265+ .pipe(266+ Effect.map(parseCloudflaredVersionOutput),267+ Effect.timeoutOption(VERSION_PROBE_TIMEOUT),268+ Effect.map(Option.getOrNull),269+ Effect.orElseSucceed(() => null),270+ );
- packages/shared/src/relayClient.ts lines 323 to 323OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -209,0 +303,21 @@303+ if (version === null || compareCloudflaredVersions(version, CLOUDFLARED_MIN_VERSION) < 0) {304+ return null;305+ }306+ if (expectedVersion !== undefined && version !== expectedVersion) {307+ yield* Effect.logWarning("Ignoring a managed relay client that reports another version", {308+ executablePath,309+ expectedVersion,310+ version,311+ });312+ return null;313+ }314+ return { status: "available", executablePath, source, version } satisfies AvailableRelayClient;315+ });316+317+ // Managed releases on disk other than the pinned one, newest first.318+ const olderManagedVersions = Effect.gen(function* () {319+ const entries = yield* fileSystem320+ .readDirectory(managedRoot)321+ .pipe(Effect.orElseSucceed(() => []));322+ return entries323+ .filter((entry) => entry !== CLOUDFLARED_VERSION && parseVersion(entry) !== null)
- apps/server/src/cloud/ManagedEndpointRuntime.ts lines 261 to 261OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -230,0 +241,21 @@241+ // connector on it. The running connector, if any, keeps serving until the new242+ // binary is installed and validated, so an update costs one brief reconnect.243+ const ensurePinnedRelayClient = Effect.gen(function* () {244+ const now = yield* Clock.currentTimeMillis;245+ const lastFailureAt = yield* Ref.get(lastInstallFailureAtRef);246+ if (lastFailureAt !== null && now - lastFailureAt < RELAY_CLIENT_INSTALL_RETRY_INTERVAL_MS) {247+ return;248+ }249+ if (yield* Ref.getAndSet(installInFlightRef, true)) return;250+ yield* Effect.logInfo("Installing the pinned relay client", {251+ version: RelayClient.CLOUDFLARED_VERSION,252+ });253+ yield* relayClient.install.pipe(254+ Effect.tap(() => Ref.set(lastInstallFailureAtRef, null)),255+ Effect.flatMap((installed) =>256+ reconcileSemaphore.withPermits(1)(257+ Effect.gen(function* () {258+ const desiredConfig = yield* Ref.get(desiredConfigRef);259+ if (!desiredConfig || desiredConfig.providerKind !== "cloudflare_tunnel") return;260+ const active = yield* Ref.get(activeRef);261+ if (active?.executable.executablePath === installed.executablePath) return;
- apps/server/src/cloud/ManagedEndpointRuntime.ts line 341OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -230,0 +321,21 @@321+ // Reconciles call this; a recent failure skips the attempt so a crash-looping322+ // connector or an offline host does not redownload on every restart.323+ const ensurePinnedRelayClient = Effect.gen(function* () {324+ const now = yield* Clock.currentTimeMillis;325+ const lastFailureAt = yield* Ref.get(lastInstallFailureAtRef);326+ if (lastFailureAt !== null && now - lastFailureAt < RELAY_CLIENT_INSTALL_RETRY_INTERVAL_MS) {327+ return;328+ }329+ yield* installPinnedRelayClient;330+ }).pipe(Effect.forkIn(runtimeScope), Effect.asVoid);331+332+ // Until the host runs the pinned release, retry the install every interval.333+ // applyConfig returns early while an older connector is healthy, and some334+ // activations never retry a missing client, so one runtime loop covers both.335+ // It paces itself, so it skips the failure gate above.336+ const retryPinnedRelayClientInstall = Effect.gen(function* () {337+ while (true) {338+ yield* Effect.sleep(Duration.millis(RELAY_CLIENT_INSTALL_RETRY_INTERVAL_MS));339+ const desiredConfig = yield* Ref.get(desiredConfigRef);340+ if (!desiredConfig || desiredConfig.providerKind !== "cloudflare_tunnel") return;341+ if (!needsPinnedRelayClient(yield* relayClient.resolve)) return;
- apps/server/src/cloud/ManagedEndpointRuntime.ts lines 344 to 344OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -230,0 +324,21 @@324+ const now = yield* Clock.currentTimeMillis;325+ const lastFailureAt = yield* Ref.get(lastInstallFailureAtRef);326+ if (lastFailureAt !== null && now - lastFailureAt < RELAY_CLIENT_INSTALL_RETRY_INTERVAL_MS) {327+ return;328+ }329+ yield* installPinnedRelayClient;330+ }).pipe(Effect.forkIn(runtimeScope), Effect.asVoid);331+332+ // Until the host runs the pinned release, retry the install every interval.333+ // applyConfig returns early while an older connector is healthy, and some334+ // activations never retry a missing client, so one runtime loop covers both.335+ // It paces itself, so it skips the failure gate above.336+ const retryPinnedRelayClientInstall = Effect.gen(function* () {337+ while (true) {338+ yield* Effect.sleep(Duration.millis(RELAY_CLIENT_INSTALL_RETRY_INTERVAL_MS));339+ const desiredConfig = yield* Ref.get(desiredConfigRef);340+ if (!desiredConfig || desiredConfig.providerKind !== "cloudflare_tunnel") return;341+ // Judge the running connector, not the disk: the pin can be installed while342+ // an older connector still runs because its post-install probe failed.343+ const active = yield* Ref.get(activeRef);344+ if (!needsPinnedRelayClient(active?.executable ?? (yield* relayClient.resolve))) return;
juliusmarminge merged this2a93885
juliusmarminge closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed