fix(ssh): preserve external runtime ownership #5751
Closedmikeyperes(GitHub profile, opens in a new tab)wants to merge 4 commits into
main fromrefs/pull/5751/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
- packages/ssh/src/tunnel.ts lines 519 to 519OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -511,8 +511,9 @@511511 DEFAULT_RUNTIME_INFO="$(resolve_default_runtime_port 2>/dev/null || true)"512512 DEFAULT_RUNTIME_PID=""513513 DEFAULT_REMOTE_PORT=""514+DEFAULT_RUNTIME_IS_MANAGED=0514515 if [ -n "$DEFAULT_RUNTIME_INFO" ]; then515516 DEFAULT_RUNTIME_PID="\${DEFAULT_RUNTIME_INFO%% *}"516517 DEFAULT_REMOTE_PORT="\${DEFAULT_RUNTIME_INFO#* }"517518 fi518-if [ -n "$DEFAULT_REMOTE_PORT" ]; then519+if [ "$REMOTE_MANAGED" = "managed" ] && [ -n "$REMOTE_PID" ] && [ -n "$REMOTE_PORT" ] && [ "$DEFAULT_REMOTE_PORT" = "$REMOTE_PORT" ] && kill -0 "$REMOTE_PID" 2>/dev/null; thenapps/server/src/serverRuntimeState.ts line 161Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -155,3 +155,5 @@155155 ),156156 }),157157 );158+159+export const clearPersistedServerRuntimeStateIfOwned = (input: {
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:M
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/server/src/serverRuntimeState.ts lines 194 to 204OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -157,0 +184,21 @@184+ }185+186+ const current = yield* readPersistedServerRuntimeState(capturedPath);187+ const ownsCapturedState =188+ Option.isSome(current) &&189+ current.value.pid === input.state.pid &&190+ current.value.startedAt === input.state.startedAt;191+192+ if (!ownsCapturedState) {193+ yield* fs.link(capturedPath, input.path).pipe(194+ Effect.catchTag("PlatformError", (cause) =>195+ cause.reason._tag === "AlreadyExists"196+ ? Effect.void197+ : Effect.fail(198+ new ServerRuntimeStateError({199+ operation: "clear",200+ statePath: input.path,201+ cause,202+ }),203+ ),204+ ),apps/server/src/serverRuntimeState.ts lines 170 to 180OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -157,0 +160,21 @@160+export const clearPersistedServerRuntimeStateIfOwned = (input: {161+ readonly path: string;162+ readonly state: Pick<PersistedServerRuntimeState, "pid" | "startedAt">;163+}) =>164+ Effect.gen(function* () {165+ const fs = yield* FileSystem.FileSystem;166+ const nonce = yield* Random.nextInt;167+ const capturedPath = `${input.path}.clearing-${process.pid}-${nonce.toString(36)}`;168+ const captured = yield* fs.rename(input.path, capturedPath).pipe(169+ Effect.as(true),170+ Effect.catchTag("PlatformError", (cause) =>171+ cause.reason._tag === "NotFound"172+ ? Effect.succeed(false)173+ : Effect.fail(174+ new ServerRuntimeStateError({175+ operation: "clear",176+ statePath: input.path,177+ cause,178+ }),179+ ),180+ ),apps/server/src/serverRuntimeState.ts lines 219 to 227OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -157,0 +207,21 @@207+208+ yield* fs.remove(capturedPath, { force: true }).pipe(209+ Effect.mapError(210+ (cause) =>211+ new ServerRuntimeStateError({212+ operation: "clear",213+ statePath: input.path,214+ cause,215+ }),216+ ),217+ );218+ }).pipe(219+ Effect.catchTag("ServerRuntimeStateError", (error) =>220+ Effect.logWarning(error.message).pipe(221+ Effect.annotateLogs({222+ operation: error.operation,223+ statePath: error.statePath,224+ cause: error,225+ }),226+ ),227+ ),
- apps/server/src/serverRuntimeState.ts line 227OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -157,0 +207,21 @@207+208+ yield* fs.remove(capturedPath, { force: true }).pipe(209+ Effect.mapError(210+ (cause) =>211+ new ServerRuntimeStateError({212+ operation: "clear",213+ statePath: input.path,214+ cause,215+ }),216+ ),217+ );218+ }).pipe(219+ Effect.catchTag("ServerRuntimeStateError", (error) =>220+ Effect.logWarning(error.message).pipe(221+ Effect.annotateLogs({222+ operation: error.operation,223+ statePath: error.statePath,224+ cause: error,225+ }),226+ ),227+ ),
mikeyperes(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.


Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
2d983572-49d6-4a56-b786-8cb7f70d20deYou can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share