Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

fix(gateway): recheck web state under lock

Rahman8a1d25a3d1bb
diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md
index eef50ad9..ad57f944 100644
--- a/docs/CHANGELOG.md
+++ b/docs/CHANGELOG.md
@@ -13,6 +13,7 @@ this is the *what*, and it is what Settings → About shows as “What's new”.
**Fixed**
+- `gateway` recheck web state under lock
- `update` reject unowned live runtimes
- `runtime` verify live update identities
- `runtime` close final update web edges
diff --git a/scripts/lib/gateway-config.sh b/scripts/lib/gateway-config.sh
index e47311c2..8d26d03f 100644
--- a/scripts/lib/gateway-config.sh
+++ b/scripts/lib/gateway-config.sh
@@ -91,35 +91,41 @@ gateway_cmd_doctor() {
return "$fails"
}
+gateway_cmd_web_resolve_locked() {
+ local mode="$1" state rc
+ case "$mode" in
+ public|auto)
+ if state="$(gateway_active_state)"; then
+ # The lifecycle decision and reconciliation share the SAME exclusive lock.
+ # Only the checkout-wide runtime lock nests inside it while Next may start.
+ gateway_with_runtime_shared gateway_reconcile_active_tunnel "$state" >/dev/null
+ state="$(gateway_active_state)" || gateway_fail "public gateway stopped during reconciliation"
+ jq -r .url <<<"$state"
+ return 0
+ else rc=$?; fi
+ [ "$rc" = 1 ] || return "$rc"
+ if [ "$mode" = public ]; then
+ gateway_fail "public gateway is not running; run: mso gateway start"
+ fi
+ gateway_with_runtime_shared gateway_cmd_local_start_locked >/dev/null
+ printf '%s\n' "$LOCAL_URL"
+ ;;
+ local)
+ gateway_with_runtime_shared gateway_cmd_local_start_locked >/dev/null
+ printf '%s\n' "$LOCAL_URL"
+ ;;
+ esac
+}
+
gateway_cmd_web() {
- local mode=auto print_only=0 url state opener
+ local mode=auto print_only=0 url opener
shift || true
while [ $# -gt 0 ]; do
case "$1" in --local) mode=local ;; --public) mode=public ;; --print) print_only=1 ;;
*) gateway_fail "usage: mso web [--local|--public] [--print]" ;; esac
shift
done
- case "$mode" in
- public)
- if state="$(gateway_active_state)"; then
- gateway_with_lock gateway_with_runtime_shared gateway_cmd_start_locked >/dev/null
- state="$(gateway_active_state)" || gateway_fail "public gateway stopped during reconciliation"
- url="$(jq -r .url <<<"$state")"
- else rc=$?; [ "$rc" = 1 ] || return "$rc"; gateway_fail "public gateway is not running; run: mso gateway start"; fi ;;
- local) gateway_health_ok || gateway_with_lock gateway_with_runtime_shared gateway_cmd_local_start_locked >/dev/null; url="$LOCAL_URL" ;;
- auto)
- if state="$(gateway_active_state)"; then
- gateway_with_lock gateway_with_runtime_shared gateway_cmd_start_locked >/dev/null
- state="$(gateway_active_state)" || gateway_fail "public gateway stopped during reconciliation"
- url="$(jq -r .url <<<"$state")"
- else rc=$?; [ "$rc" = 1 ] || return "$rc"
- # A configured public origin is metadata, not proof that its tunnel/proxy is
- # reachable. Auto mode uses public only when gateway state is actually active;
- # otherwise guarantee the supported local UI and open loopback.
- gateway_health_ok || gateway_with_lock gateway_with_runtime_shared gateway_cmd_local_start_locked >/dev/null
- url="$LOCAL_URL"
- fi ;;
- esac
+ url="$(gateway_with_lock gateway_cmd_web_resolve_locked "$mode")"
case "$url" in http:*) url="$(gateway_validate_loopback_origin "$url" 2>/dev/null || true)" ;;
https:*) url="$(gateway_validate_public_origin "$url" 2>/dev/null || true)" ;; *) url="" ;; esac
[ -n "$url" ] || gateway_fail "refusing unsafe browser URL"
diff --git a/scripts/mso-gateway-web.test.ts b/scripts/mso-gateway-web.test.ts
index 340b5ec0..27d14b6c 100644
--- a/scripts/mso-gateway-web.test.ts
+++ b/scripts/mso-gateway-web.test.ts
@@ -1,13 +1,46 @@
+import { spawn } from "node:child_process";
+import path from "node:path";
import { describe, expect, it } from "vitest";
-import { fixture, runGateway } from "./mso-gateway-test-fixture";
+import { GATEWAY, fixture, readState, runGateway } from "./mso-gateway-test-fixture";
+
+function runAsync(args: string[], env: NodeJS.ProcessEnv) {
+ return new Promise<{ code: number | null; stdout: string; stderr: string }>((resolve) => {
+ const child = spawn(GATEWAY, args, { env, stdio: ["ignore", "pipe", "pipe"] });
+ let stdout = "", stderr = "";
+ child.stdout.on("data", (v) => stdout += v); child.stderr.on("data", (v) => stderr += v);
+ child.on("close", (code) => resolve({ code, stdout, stderr }));
+ });
+}
+
+async function stopTunnelWhileWebWaits(mode: "auto" | "public") {
+ const f = fixture();
+ runGateway(["start"], f.baseEnv);
+ const state = readState(f.state), lock = path.join(f.state, "lifecycle.lock");
+ const holder = spawn("flock", ["-x", lock, "-c", "sleep 0.7"], { stdio: "ignore" });
+ await new Promise((r) => setTimeout(r, 80));
+ const web = runAsync(["web", ...(mode === "public" ? ["--public"] : []), "--print"], f.baseEnv);
+ await new Promise((r) => setTimeout(r, 120));
+ try { process.kill(state.value.tunnelIdentity.pid, "SIGTERM"); } catch { /* already gone */ }
+ await new Promise<void>((resolve) => holder.once("close", () => resolve()));
+ return web;
+}
describe("mso web routing", () => {
it("falls back to loopback when a public origin is configured but no gateway is active", () => {
const f = fixture();
- const out = runGateway(["web", "--print"], {
- ...f.baseEnv,
- OS_PUBLIC_ORIGIN: "https://mso.example.test",
- });
+ const out = runGateway(["web", "--print"], { ...f.baseEnv, OS_PUBLIC_ORIGIN: "https://mso.example.test" });
expect(out.trim()).toBe("http://127.0.0.1:4005");
});
+
+ it("rechecks state under the lifecycle lock and auto-falls back when the tunnel dies while waiting", async () => {
+ const out = await stopTunnelWhileWebWaits("auto");
+ expect(out.code).toBe(0); expect(out.stdout.trim()).toBe("http://127.0.0.1:4005");
+ expect(out.stderr).not.toContain("unbound variable");
+ });
+
+ it("rechecks state under the lifecycle lock and public mode fails cleanly when the tunnel dies", async () => {
+ const out = await stopTunnelWhileWebWaits("public");
+ expect(out.code).not.toBe(0); expect(out.stderr).toContain("public gateway is not running");
+ expect(out.stderr).not.toContain("unbound variable");
+ });
});