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(install): hold update lock across checkout refresh

Abdurrahman Fakhrul70d03dbfd8de
diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md
index e8e95f4a..4e8896d6 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**
+- `install` hold update lock across checkout refresh
- `install` restore service after pre-mutation abort
- `runtime` close update authority and stop races
- `runtime` preserve installer and env lifecycles
diff --git a/docs/INSTALL.md b/docs/INSTALL.md
index 820fbd83..7595e79a 100644
--- a/docs/INSTALL.md
+++ b/docs/INSTALL.md
@@ -452,7 +452,7 @@ mso update # preferred: update safely even if :4005 is down
mso update log
```
-The updater verifies the incoming checkout/build before replacing the service. A normal `mso update` treats fetched `origin/main` as the release authority: after any fast-forward, local `main` must equal that remote commit exactly. A clean branch that is locally ahead or diverged is refused before dependency/build mutation instead of deploying unpushed code. `mso update status` reports that state explicitly. `mso update --rebuild` is different by design: it rebuilds the already-selected clean checkout without changing Git history. With an active
+The updater verifies the incoming checkout/build before replacing the service. Re-running the installer on an existing checkout acquires the same checkout-scoped transaction lock **before** Git fetch/checkout mutation and hands that open lock FD to the post-checkout runtime lifecycle, so installer, `mso update`, and `mso deploy` cannot mutate one checkout concurrently. A normal `mso update` treats fetched `origin/main` as the release authority: after any fast-forward, local `main` must equal that remote commit exactly. A clean branch that is locally ahead or diverged is refused before dependency/build mutation instead of deploying unpushed code. `mso update status` reports that state explicitly. `mso update --rebuild` is different by design: it rebuilds the already-selected clean checkout without changing Git history. With an active
`mso.service` it first canonicalizes the unit's `WorkingDirectory` and requires it to equal the checkout
that invoked `mso update`; a secondary clone is never allowed to restart an unrelated live service.
The updater then runs outside that service cgroup so it survives the restart. With no active
diff --git a/scripts/install-core.sh b/scripts/install-core.sh
index 9ede3a43..77e68fea 100644
--- a/scripts/install-core.sh
+++ b/scripts/install-core.sh
@@ -299,6 +299,62 @@ ensure_cli_tools() {
ensure_git; ensure_node; ensure_bun; ensure_buildtools; ensure_cli_tools
+# Existing checkouts already ship the private-state primitive. Hold the SAME
+# checkout-scoped transaction.lock used by `mso update`/`mso deploy` before Git
+# fetch/checkout mutates source. The FD stays open and is adopted by the freshly
+# checked-out lifecycle helper, so there is no unlock/relock window.
+INSTALL_EARLY_UPDATE_LOCK_HELD=0
+INSTALL_EARLY_UPDATE_LOCK_FD=''
+INSTALL_EARLY_UPDATE_LOCK_FILE=''
+INSTALL_EARLY_UPDATE_CANONICAL_ROOT=''
+
+install_early_update_lock_release() {
+ [ "$INSTALL_EARLY_UPDATE_LOCK_HELD" = 1 ] || return 0
+ if [ -n "${INSTALL_EARLY_UPDATE_LOCK_FD:-}" ]; then
+ flock -u "$INSTALL_EARLY_UPDATE_LOCK_FD" 2>/dev/null || true
+ exec {INSTALL_EARLY_UPDATE_LOCK_FD}>&- || true
+ INSTALL_EARLY_UPDATE_LOCK_FD=''
+ fi
+ INSTALL_EARLY_UPDATE_LOCK_HELD=0
+}
+
+install_early_update_lock_acquire() {
+ local private_helper canonical base key state lock timeout
+ [ -d "$DIR/.git" ] || return 0
+ [ -z "$(git -C "$DIR" status --porcelain)" ] \
+ || die "checkout has uncommitted changes at $DIR; refusing an unlocked in-place upgrade"
+ private_helper="$DIR/scripts/lib/private-state.sh"
+ [ -f "$private_helper" ] && [ ! -L "$private_helper" ] \
+ || die "existing checkout is missing the secure private-state helper; restore the tracked file or install into another --dir"
+ [ "$(stat -c '%u' -- "$private_helper" 2>/dev/null || true)" = "$(id -u)" ] \
+ || die "private-state helper is not owned by the installing user: $private_helper"
+ # shellcheck source=scripts/lib/private-state.sh
+ . "$private_helper"
+ canonical="$(realpath -e -- "$DIR" 2>/dev/null || true)"
+ [ -n "$canonical" ] || die "cannot canonicalize existing checkout before update lock"
+ base="$(mso_private_state_dir "${MSO_UPDATE_STATE_DIR:-$HOME/.mso/private/update-state}")" \
+ || die "unsafe installer update-state directory"
+ key="$(printf '%s' "$canonical" | sha256sum | awk '{print $1}')"
+ [[ "$key" =~ ^[0-9a-f]{64}$ ]] || die "cannot derive installer update-lock scope"
+ state="$(mso_private_state_dir "$base/$key")" || die "unsafe checkout-scoped installer update state"
+ lock="$state/transaction.lock"
+ mso_private_state_ensure_file "$lock" >/dev/null || die "unsafe installer update transaction lock"
+ exec {INSTALL_EARLY_UPDATE_LOCK_FD}<>"$lock" || die "cannot open installer update transaction lock"
+ timeout="${MSO_UPDATE_LOCK_TIMEOUT_SECONDS:-900}"
+ [[ "$timeout" =~ ^[0-9]+([.][0-9]+)?$ ]] || die "invalid installer update-lock timeout"
+ if ! flock -x -w "$timeout" "$INSTALL_EARLY_UPDATE_LOCK_FD"; then
+ exec {INSTALL_EARLY_UPDATE_LOCK_FD}>&- || true
+ INSTALL_EARLY_UPDATE_LOCK_FD=''
+ die "another MSO installer/update/deploy transaction is still running for $canonical"
+ fi
+ INSTALL_EARLY_UPDATE_LOCK_HELD=1
+ INSTALL_EARLY_UPDATE_LOCK_FILE="$lock"
+ INSTALL_EARLY_UPDATE_CANONICAL_ROOT="$canonical"
+ trap install_early_update_lock_release EXIT
+}
+
+install_early_update_lock_acquire
+
# portable 32-byte hex RNG (node is guaranteed present by now)
rand_hex() {
if command -v openssl >/dev/null 2>&1; then openssl rand -hex "$1"
diff --git a/scripts/install-runtime-lifecycle.test.ts b/scripts/install-runtime-lifecycle.test.ts
index 1bb47510..2340d437 100644
--- a/scripts/install-runtime-lifecycle.test.ts
+++ b/scripts/install-runtime-lifecycle.test.ts
@@ -1,4 +1,5 @@
import { execFileSync, spawnSync } from "node:child_process";
+import crypto from "node:crypto";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
@@ -11,7 +12,7 @@ const roots: string[] = [];
function copy(src: string, dst: string) { fs.mkdirSync(path.dirname(dst), { recursive: true }); fs.copyFileSync(src, dst); fs.chmodSync(dst, 0o755); }
function envIdentity(file: string) { const st = fs.statSync(file); return { path: fs.realpathSync(file), dev: String(st.dev), ino: String(st.ino) }; }
-function fixture(systemd = false, doService = 1) {
+function fixture(systemd = false, doService = 1, earlyLock = false) {
const base = fs.mkdtempSync(path.join(os.tmpdir(), "mso-install-runtime-")); roots.push(base);
const repo = path.join(base, "repo"), home = path.join(base, "home"), bin = path.join(base, "bin"), capture = path.join(base, "capture");
fs.mkdirSync(home); fs.mkdirSync(bin); fs.mkdirSync(path.join(repo, "scripts/lib"), { recursive: true });
@@ -38,14 +39,26 @@ case "$*" in
'show -p WorkingDirectory --value mso.service') printf '%s\n' ${JSON.stringify(canonical)} ;;
esac
`, { mode: 0o755 });
+ const updateBase = path.join(base, "update-state");
+ const updateKey = crypto.createHash("sha256").update(canonical).digest("hex");
+ const updateScope = path.join(updateBase, updateKey);
+ fs.mkdirSync(updateScope, { recursive: true, mode: 0o700 });
+ fs.chmodSync(updateBase, 0o700); fs.chmodSync(updateScope, 0o700);
+ const earlyLockFile = path.join(updateScope, "transaction.lock");
+ fs.writeFileSync(earlyLockFile, "", { mode: 0o600 });
const runner = path.join(base, "runner.sh");
fs.writeFileSync(runner, `#!/bin/bash
set -euo pipefail
DIR=${JSON.stringify(repo)}; PORT=4005; DO_SERVICE=${doService}; SERVICE=mso.service
-export HOME=${JSON.stringify(home)} PATH=${JSON.stringify(`${bin}:${process.env.PATH}`)} MSO_UPDATE_STATE_DIR=${JSON.stringify(path.join(base, "update-state"))} MSO_RUNTIME_EXCLUSION_DIR=${JSON.stringify(path.join(base, "runtime-exclusion"))}
+export HOME=${JSON.stringify(home)} PATH=${JSON.stringify(`${bin}:${process.env.PATH}`)} MSO_UPDATE_STATE_DIR=${JSON.stringify(path.join(base, "update-state"))} MSO_RUNTIME_EXCLUSION_DIR=${JSON.stringify(path.join(base, "runtime-exclusion"))} MSO_UPDATE_LOCK_TIMEOUT_SECONDS=0.2
die(){ echo "installer: $*" >&2; exit 1; }
sudo_do(){ "$@"; }
systemd_ready(){ ${systemd ? "return 0" : "return 1"}; }
+${earlyLock ? `exec {INSTALL_EARLY_UPDATE_LOCK_FD}<>${JSON.stringify(earlyLockFile)}
+flock -x "$INSTALL_EARLY_UPDATE_LOCK_FD"
+INSTALL_EARLY_UPDATE_LOCK_HELD=1
+INSTALL_EARLY_UPDATE_LOCK_FILE=${JSON.stringify(earlyLockFile)}
+INSTALL_EARLY_UPDATE_CANONICAL_ROOT=${JSON.stringify(canonical)}` : ""}
. "$DIR/scripts/lib/install-runtime-lifecycle.sh"
trap install_runtime_lifecycle_cleanup EXIT
install_runtime_lifecycle_begin
@@ -59,6 +72,14 @@ trap - EXIT
afterEach(() => { for (const root of roots.splice(0)) fs.rmSync(root, { recursive: true, force: true }); });
describe("installer runtime lifecycle", () => {
+ it("adopts a transaction lock acquired before checkout instead of reacquiring it", () => {
+ const f = fixture(false, 1, true);
+ const out = spawnSync(f.runner, [], { encoding: "utf8", timeout: 5_000 });
+ expect(out.status).toBe(0);
+ expect(out.error).toBeUndefined();
+ expect(fs.readFileSync(f.capture, "utf8")).toContain("MUTATE");
+ });
+
it("quiesces and restores a no-systemd fallback around installer mutation with its custom env", () => {
const f = fixture(false, 1); execFileSync(f.runner, [], { encoding: "utf8" });
const calls = fs.readFileSync(f.capture, "utf8");
diff --git a/scripts/install-update-lock-contract.test.ts b/scripts/install-update-lock-contract.test.ts
new file mode 100644
index 00000000..5cd7301f
--- /dev/null
+++ b/scripts/install-update-lock-contract.test.ts
@@ -0,0 +1,23 @@
+import fs from "node:fs";
+import path from "node:path";
+import { describe, expect, it } from "vitest";
+
+const ROOT = process.cwd();
+
+describe("installer update transaction ordering", () => {
+ it("acquires the checkout transaction lock before fetch/checkout mutation", () => {
+ const core = fs.readFileSync(path.join(ROOT, "scripts/install-core.sh"), "utf8");
+ const acquire = core.indexOf("\ninstall_early_update_lock_acquire\n");
+ const fetch = core.indexOf('git -C "$DIR" fetch --quiet origin "$REF"');
+ const checkout = core.indexOf('git -C "$DIR" checkout --quiet FETCH_HEAD');
+ expect(acquire).toBeGreaterThan(0);
+ expect(fetch).toBeGreaterThan(acquire);
+ expect(checkout).toBeGreaterThan(fetch);
+ });
+
+ it("hands the pre-checkout lock FD to the post-checkout lifecycle", () => {
+ const helper = fs.readFileSync(path.join(ROOT, "scripts/lib/install-runtime-lifecycle.sh"), "utf8");
+ expect(helper).toContain('UPDATE_LOCK_FD="$INSTALL_EARLY_UPDATE_LOCK_FD"');
+ expect(helper).toContain('[ "$UPDATE_LOCK_HELD" = 1 ] || update_lock_acquire');
+ });
+});
diff --git a/scripts/install.sh b/scripts/install.sh
index da3f9e67..50fd6d59 100644
--- a/scripts/install.sh
+++ b/scripts/install.sh
@@ -12,7 +12,7 @@ set -Eeuo pipefail
umask 077
CORE_URL="${MSO_INSTALL_CORE_URL:-https://raw.githubusercontent.com/rahmanef63/mso/main/scripts/install-core.sh}"
-CORE_SHA256="8ad53d5d052795c79148e5b551543ef6aeededd16c08032a24fb612bbdbfa991"
+CORE_SHA256="2eab509ebc3d461495a9e0b54b12ebd5b90e59b64d44ff76888da34d4628b248"
CORE_EOF='# MSO_INSTALLER_CORE_EOF'
TMP_INSTALLER=''
diff --git a/scripts/lib/install-runtime-lifecycle.sh b/scripts/lib/install-runtime-lifecycle.sh
index e5e04724..6003991c 100644
--- a/scripts/lib/install-runtime-lifecycle.sh
+++ b/scripts/lib/install-runtime-lifecycle.sh
@@ -22,6 +22,18 @@ install_runtime_lifecycle_init() {
. "$ROOT/scripts/lib/runtime-exclusion.sh"
# shellcheck source=scripts/lib/update-gateway-runtimes.sh
. "$ROOT/scripts/lib/update-gateway-runtimes.sh"
+
+ if [ "${INSTALL_EARLY_UPDATE_LOCK_HELD:-0}" = 1 ]; then
+ init_update_state
+ [ "$UPDATE_CANONICAL_ROOT" = "${INSTALL_EARLY_UPDATE_CANONICAL_ROOT:-}" ] \
+ || die "early installer update lock belongs to another checkout"
+ [ "$UPDATE_LOCK_DIR" = "${INSTALL_EARLY_UPDATE_LOCK_FILE:-}" ] \
+ || die "early installer update lock path does not match current lifecycle state"
+ UPDATE_LOCK_FD="$INSTALL_EARLY_UPDATE_LOCK_FD"
+ UPDATE_LOCK_HELD=1
+ INSTALL_EARLY_UPDATE_LOCK_FD=''
+ INSTALL_EARLY_UPDATE_LOCK_HELD=0
+ fi
}
install_runtime_lifecycle_cleanup() {
@@ -69,7 +81,7 @@ install_runtime_active_service_preflight() {
install_runtime_lifecycle_begin() {
install_runtime_lifecycle_init
- update_lock_acquire
+ [ "$UPDATE_LOCK_HELD" = 1 ] || update_lock_acquire
INSTALL_RUNTIME_LIFECYCLE=1
runtime_exclusion_acquire_exclusive \
|| die "could not acquire checkout runtime exclusion before installer build"