Skip to content

Commit 0ae4ef2

Browse files
author
PR Batch Tester
committed
fix(ssh): require identity before publishing managed state
1 parent 1a19b2e commit 0ae4ef2

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

‎packages/ssh/src/tunnel.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,49 @@ server.listen(Number(flag("--port")), "127.0.0.1", () => {
647647
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
648648
);
649649

650+
it.live.each(["fails", "returns an empty identity"] as const)(
651+
"does not publish managed state when the process identity probe %s",
652+
(failure) =>
653+
Effect.gen(function* () {
654+
const { fs, run, stateDir, writeVariant, lockPath } = yield* prepareLaunch;
655+
let launchedPid: number | undefined;
656+
yield* Effect.addFinalizer(() =>
657+
Effect.sync(() => {
658+
if (launchedPid !== undefined && isAlive(launchedPid))
659+
process.kill(launchedPid, "SIGKILL");
660+
}),
661+
);
662+
const script = yield* writeVariant("missing-identity", [
663+
[
664+
' REMOTE_PID="$!"',
665+
` REMOTE_PID="$!"
666+
printf 'launched:%s\\n' "$REMOTE_PID"
667+
process_start() { return ${failure === "fails" ? 1 : 0}; }`,
668+
],
669+
]);
670+
const result = yield* run(
671+
"alias",
672+
{
673+
onLine: (line) =>
674+
Effect.sync(() => {
675+
if (line.startsWith("launched:"))
676+
launchedPid = Number(line.slice("launched:".length));
677+
}),
678+
release: Effect.void,
679+
},
680+
script,
681+
);
682+
assert.equal(result.exitCode, 1, result.stderr);
683+
assert.include(result.stderr, "Failed to record the remote T3 server's process identity");
684+
if (launchedPid === undefined) assert.fail("the launch did not report its spawned PID");
685+
assert.isFalse(isAlive(launchedPid));
686+
for (const file of ["pid", "port", "managed", "process-start"]) {
687+
assert.isFalse(yield* fs.exists(`${stateDir("alias")}/${file}`), file);
688+
}
689+
assert.isFalse(yield* fs.exists(lockPath));
690+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
691+
);
692+
650693
it.live("does not take over a server whose recorded process identity differs", () =>
651694
Effect.gen(function* () {
652695
const { fs, stateDir, readManagedPid, launch } = yield* prepareLaunch;

‎packages/ssh/src/tunnel.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,16 @@ if [ -z "$REMOTE_PORT" ]; then
876876
fi
877877
nohup env T3CODE_NO_BROWSER=1 "$RUNNER_FILE" serve --host 127.0.0.1 --port "$REMOTE_PORT" --base-dir "$DEFAULT_SERVER_HOME" >>"$LOG_FILE" 2>&1 < /dev/null &
878878
REMOTE_PID="$!"
879-
process_start "$REMOTE_PID" >"$PROCESS_START_FILE" || true
879+
rm -f "$PID_FILE" "$MANAGED_FILE"
880+
if ! REMOTE_PROCESS_START="$(process_start "$REMOTE_PID")" || [ -z "$REMOTE_PROCESS_START" ]; then
881+
printf "Failed to record the remote T3 server's process identity.\\n" >&2
882+
rm -f "$PID_FILE" "$PORT_FILE" "$MANAGED_FILE" "$PROCESS_START_FILE"
883+
kill "$REMOTE_PID" 2>/dev/null || true
884+
wait_for_pid_exit "$REMOTE_PID"
885+
wait "$REMOTE_PID" 2>/dev/null || true
886+
exit 1
887+
fi
888+
printf '%s\\n' "$REMOTE_PROCESS_START" >"$PROCESS_START_FILE"
880889
printf '%s\\n' "$REMOTE_PID" >"$PID_FILE"
881890
printf '%s\\n' "$REMOTE_PORT" >"$PORT_FILE"
882891
printf 'managed\\n' >"$MANAGED_FILE"

0 commit comments

Comments
 (0)