Preserve standalone release pins during daemon updates (#43521)

## Why

Bootstrapping the app-server daemon should preserve an explicitly selected release, even when that version is currently `latest`. Older managed binaries should also be able to serve app-server without supporting the updater command.

## What changed

- Record `latest` selections in `auto-update-version` in both standalone installers and clear the marker for explicit releases.
- Start the daemon updater only for a marked stable release whose binary supports `pid-update-loop`. Existing installs without a marker require a new `latest` installation to enable automatic updates.
- Recheck the selected release under the install lock so an in-flight update cannot overwrite a new pin, including installer calls from older updaters. Recheck selection before restarting app-server or replacing the updater.
- Cancel Unix installer process groups and clean up their owned fallback locks when the updater stops.

## Testing

Add coverage for channel markers, explicit pins of the current latest version, older updater guards, updater command support, and Unix installer cancellation with child-process and lock cleanup.

GitOrigin-RevId: 4d275237bd77d896bf64dc1b85a5bca3608142c7
This commit is contained in:
Eric Traut
2026-09-07 17:18:02 +00:00
committed by copyberry
parent b1205c12d5
commit adee0b04fa
9 changed files with 608 additions and 33 deletions
+57
View File
@@ -906,6 +906,7 @@ $codexHome = if ([string]::IsNullOrWhiteSpace($env:CODEX_HOME)) {
$standaloneRoot = Join-Path $codexHome "packages\standalone"
$releasesDir = Join-Path $standaloneRoot "releases"
$currentDir = Join-Path $standaloneRoot "current"
$autoUpdateVersion = Join-Path $standaloneRoot "auto-update-version"
$lockPath = Join-Path $standaloneRoot "install.lock"
$defaultVisibleBinDir = Join-Path $env:LOCALAPPDATA "Programs\OpenAI\Codex\bin"
@@ -943,9 +944,55 @@ $checksumMetadata = $assetSelection.ChecksumMetadata
$installLayout = $assetSelection.InstallLayout
$tempDir = Join-Path ([System.IO.Path]::GetTempPath()) ("codex-install-" + [System.Guid]::NewGuid().ToString("N"))
New-Item -ItemType Directory -Force -Path $tempDir | Out-Null
$guardRejected = $false
try {
Invoke-WithInstallLock -LockPath $lockPath -Script {
$updaterRecord = Join-Path $codexHome "app-server-daemon\app-server-updater.pid"
$oldUpdaterParent = $false
if ($Release -eq "latest" -and $env:CODEX_INSTALL_IF_LATEST -ne "1" -and (Test-Path -LiteralPath $updaterRecord)) {
$updaterPid = $null
$updaterStartTime = $null
try {
$record = Get-Content -LiteralPath $updaterRecord -Raw | ConvertFrom-Json
$updaterPid = [long]$record.pid
$updaterStartTime = [string]$record.processStartTime
} catch {
# Empty or stale PID reservations must not block a manual install.
}
if ($null -ne $updaterPid -and $updaterPid -gt 0 -and -not [string]::IsNullOrEmpty($updaterStartTime)) {
$updaterProcess = Get-Process -Id $updaterPid -ErrorAction SilentlyContinue
if ($null -ne $updaterProcess -and $updaterStartTime -eq [string]$updaterProcess.StartTime.ToFileTimeUtc()) {
try {
$parentPid = (Get-CimInstance Win32_Process -Filter "ProcessId = $PID" -ErrorAction Stop).ParentProcessId
if ([long]$parentPid -le 0) { throw "Missing updater parent process." }
} catch {
try {
$parentPid = (Get-WmiObject Win32_Process -Filter "ProcessId = $PID" -ErrorAction Stop).ParentProcessId
if ([long]$parentPid -le 0) { throw "Missing updater parent process." }
} catch {
throw "Cannot verify whether the standalone installer was launched by an older updater."
}
}
$oldUpdaterParent = $updaterPid -eq $parentPid
}
}
}
if ($env:CODEX_INSTALL_IF_LATEST -eq "1" -or $oldUpdaterParent) {
$previousRelease = if ($oldUpdaterParent -and (Test-Path -LiteralPath $autoUpdateVersion)) {
[System.IO.File]::ReadAllText($autoUpdateVersion)
} else {
$env:CODEX_UPDATE_FROM_RELEASE
}
$currentTarget = if (Test-Path -LiteralPath $currentDir) { (Get-Item -LiteralPath $currentDir).Target } else { $null }
if ($Release -ne "latest" -or [string]::IsNullOrEmpty($previousRelease) -or [string]::IsNullOrEmpty($currentTarget) -or
-not (Test-Path -LiteralPath $autoUpdateVersion) -or
[System.IO.File]::ReadAllText($autoUpdateVersion) -cne $previousRelease -or
[System.IO.Path]::GetFullPath($currentTarget) -ne [System.IO.Path]::GetFullPath((Join-Path $releasesDir $previousRelease))) {
$script:guardRejected = $true
return
}
}
Remove-StaleInstallArtifacts -ReleasesDir $releasesDir
if (-not (Test-ReleaseIsComplete -ReleaseDir $releaseDir -ExpectedVersion $resolvedVersion -ExpectedTarget $target -Layout $installLayout)) {
@@ -1012,6 +1059,15 @@ try {
New-Item -ItemType Directory -Force -Path $standaloneRoot | Out-Null
Ensure-Junction -LinkPath $currentDir -TargetPath $releaseDir -InstallerOwnedTargetPrefix $releasesDir
if ($Release -eq "latest") {
$tempMarker = "$autoUpdateVersion.tmp.$PID"
[System.IO.File]::WriteAllText($tempMarker, $releaseName)
Move-Item -LiteralPath $tempMarker -Destination $autoUpdateVersion -Force
} else {
if (Test-Path -LiteralPath $autoUpdateVersion) {
Remove-Item -LiteralPath $autoUpdateVersion -Force -ErrorAction Stop
}
}
$visibleParent = Split-Path -Parent $visibleBinDir
$currentBinDir = if ($installLayout -eq "Package") {
@@ -1040,6 +1096,7 @@ try {
} finally {
Remove-Item -Recurse -Force $tempDir -ErrorAction SilentlyContinue
}
if ($guardRejected) { return }
Maybe-HandleConflictingInstall -Conflict $conflictingInstall
+49 -1
View File
@@ -19,6 +19,7 @@ CODEX_HOME_DIR="${CODEX_HOME:-$HOME/.codex}"
STANDALONE_ROOT="$CODEX_HOME_DIR/packages/standalone"
RELEASES_DIR="$STANDALONE_ROOT/releases"
CURRENT_LINK="$STANDALONE_ROOT/current"
AUTO_UPDATE_VERSION="$STANDALONE_ROOT/auto-update-version"
LOCK_FILE="$STANDALONE_ROOT/install.lock"
LOCK_DIR="$STANDALONE_ROOT/install.lock.d"
LOCK_STALE_AFTER_SECS=600
@@ -1148,9 +1149,50 @@ cleanup() {
rm -rf "$tmp_dir"
fi
}
trap cleanup EXIT INT TERM
trap cleanup EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
acquire_install_lock
updater_record="$CODEX_HOME_DIR/app-server-daemon/app-server-updater.pid"
old_updater_parent="false"
if [ "${CODEX_INSTALL_IF_LATEST:-}" != "1" ] && [ -f "$updater_record" ]; then
updater_pid="$(sed -n 's/.*"pid"[[:space:]]*:[[:space:]]*\([0-9][0-9]*\).*/\1/p' "$updater_record" | head -n 1)"
recorded_start="$(sed -n 's/.*"processStartTime"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$updater_record" | head -n 1)"
if [ -r "/proc/$$/stat" ]; then
parent_pid="$(sed 's/^.*) //' "/proc/$$/stat" | awk '{ print $2 }')"
else
parent_pid="$(ps -p "$$" -o ppid= 2>/dev/null)" || parent_pid=""
parent_pid="$(printf '%s' "$parent_pid" | tr -d ' ')"
fi
if [ -n "$updater_pid" ] && [ "$updater_pid" = "$parent_pid" ]; then
actual_details="$(ps -p "$updater_pid" -o stat= -o lstart= 2>/dev/null)" || actual_details=""
actual_start="$(printf '%s' "$actual_details" | sed 's/^[^[:space:]]*[[:space:]]*//; s/[[:space:]]*$//')"
if [ -n "$recorded_start" ] && [ "$recorded_start" = "$actual_start" ]; then
old_updater_parent="true"
fi
fi
if [ "$RELEASE" = "latest" ] && [ -n "$updater_pid" ] &&
{ [ -z "$parent_pid" ] || { [ "$updater_pid" = "$parent_pid" ] &&
{ [ -z "$recorded_start" ] || [ -z "$actual_start" ]; }; }; } &&
kill -0 "$updater_pid" 2>/dev/null; then
warn "Cannot verify whether an older updater launched this installer; skipping latest update."
exit 0
fi
fi
if [ "${CODEX_INSTALL_IF_LATEST:-}" = "1" ] || [ "$old_updater_parent" = "true" ]; then
guarded_release="${CODEX_UPDATE_FROM_RELEASE:-}"
if [ "$old_updater_parent" = "true" ]; then
guarded_release="$(cat "$AUTO_UPDATE_VERSION" 2>/dev/null || true)"
fi
current_release_dir="$(cd -P "$CURRENT_LINK" 2>/dev/null && pwd)" || exit 0
releases_dir="$(cd -P "$RELEASES_DIR" 2>/dev/null && pwd)" || exit 0
if [ "$RELEASE" != "latest" ] || [ -z "$guarded_release" ] ||
[ "$(cat "$AUTO_UPDATE_VERSION" 2>/dev/null || true)" != "$guarded_release" ] ||
[ "$current_release_dir" != "$releases_dir/$guarded_release" ]; then
exit 0
fi
fi
cleanup_stale_install_artifacts
if ! release_dir_is_complete "$release_dir" "$resolved_version" "$vendor_target" "$install_layout"; then
@@ -1183,6 +1225,12 @@ if ! release_dir_is_complete "$release_dir" "$resolved_version" "$vendor_target"
exit 1
fi
update_current_link "$release_dir"
if [ "$RELEASE" = "latest" ]; then
printf '%s' "$release_name" > "$AUTO_UPDATE_VERSION.tmp.$$"
mv -f "$AUTO_UPDATE_VERSION.tmp.$$" "$AUTO_UPDATE_VERSION"
else
rm -f "$AUTO_UPDATE_VERSION"
fi
update_visible_command "$release_dir"
add_to_path
verify_visible_command
+130
View File
@@ -156,6 +156,104 @@ class InstallShTest(unittest.TestCase):
],
)
def test_explicit_release_pins_even_the_current_latest_version(self) -> None:
with tempfile.TemporaryDirectory() as temp_dir:
root = Path(temp_dir)
archive, checksum, metadata = create_package_release(root)
options = dict(
metadata_json=metadata,
archive_path=archive,
checksum_path=checksum,
force_macos=True,
)
marker = root / "codex-home/packages/standalone/auto-update-version"
latest, _ = run_installer_in(root, "latest", **options)
self.assertEqual(latest.returncode, 0, latest.stderr)
release_name = f"{VERSION}-aarch64-apple-darwin"
self.assertEqual(marker.read_text(), release_name)
pinned, _ = run_installer_in(root, VERSION, **options)
self.assertEqual(pinned.returncode, 0, pinned.stderr)
self.assertFalse(marker.exists())
updater_record = (
root / "codex-home/app-server-daemon/app-server-updater.pid"
)
updater_record.parent.mkdir(parents=True)
updater_record.write_text(
json.dumps(
{"pid": os.getpid(), "processStartTime": process_start_time()}
)
)
old_updater, _ = run_installer_in(
root, "latest", old_updater_parent_pid=os.getpid(), **options
)
self.assertEqual(old_updater.returncode, 0, old_updater.stderr)
self.assertFalse(marker.exists())
skipped, _ = run_installer_in(
root, "latest", update_guard_from_release=release_name, **options
)
self.assertEqual(skipped.returncode, 0, skipped.stderr)
self.assertFalse(marker.exists())
updater_record.write_text(
json.dumps({"pid": os.getpid(), "processStartTime": "stale"})
)
latest_again, _ = run_installer_in(
root, "latest", old_updater_parent_pid=os.getpid(), **options
)
self.assertEqual(latest_again.returncode, 0, latest_again.stderr)
self.assertEqual(marker.read_text(), release_name)
managed = (
root
/ f"codex-home/packages/standalone/releases/{release_name}/bin/codex"
)
managed.unlink()
guarded, _ = run_installer_in(
root,
"latest",
update_guard_from_release=release_name,
**options,
)
self.assertEqual(guarded.returncode, 0, guarded.stderr)
self.assertTrue(managed.exists())
def test_uninspectable_legacy_updater_does_not_clear_pin(self) -> None:
with tempfile.TemporaryDirectory() as temp_dir:
root = Path(temp_dir)
archive, checksum, metadata = create_package_release(root)
options = dict(
metadata_json=metadata,
archive_path=archive,
checksum_path=checksum,
force_macos=True,
)
pinned, _ = run_installer_in(root, VERSION, **options)
self.assertEqual(pinned.returncode, 0, pinned.stderr)
updater_record = (
root / "codex-home/app-server-daemon/app-server-updater.pid"
)
updater_record.parent.mkdir(parents=True)
updater_record.write_text(
json.dumps(
{"pid": os.getpid(), "processStartTime": process_start_time()}
)
)
attempted, _ = run_installer_in(
root,
"latest",
old_updater_parent_pid=os.getpid(),
fail_ps=True,
**options,
)
self.assertEqual(attempted.returncode, 0, attempted.stderr)
self.assertFalse(
(root / "codex-home/packages/standalone/auto-update-version").exists()
)
def test_releases_unusable_metadata_falls_back_to_github(self) -> None:
unusable_metadata = {
"html": "<html>proxy error</html>",
@@ -536,6 +634,9 @@ def run_installer_in(
force_macos: bool = False,
use_mirror: bool | None = False,
releases_mode: str = "",
update_guard_from_release: str | None = None,
old_updater_parent_pid: int | None = None,
fail_ps: bool = False,
) -> tuple[subprocess.CompletedProcess[str], list[str]]:
bin_dir = root / "bin"
bin_dir.mkdir(exist_ok=True)
@@ -647,6 +748,19 @@ def run_installer_in(
encoding="utf-8",
)
fake_uname.chmod(0o755)
if old_updater_parent_pid is not None:
fake_ps = bin_dir / "ps"
fake_ps.write_text(
"#!/bin/sh\nexit 1\n"
if fail_ps
else "#!/bin/sh\n"
'case "$*" in\n'
' *lstart*) printf "S %s\\n" "$CODEX_TEST_PARENT_START" ;;\n'
' *) printf "%s\\n" "$CODEX_TEST_PARENT_PID" ;;\n'
"esac\n",
encoding="utf-8",
)
fake_ps.chmod(0o755)
home = root / "home"
home.mkdir(exist_ok=True)
@@ -681,6 +795,15 @@ def run_installer_in(
"SHELL": "/bin/sh",
}
)
if update_guard_from_release is None:
env.pop("CODEX_INSTALL_IF_LATEST", None)
env.pop("CODEX_UPDATE_FROM_RELEASE", None)
else:
env["CODEX_INSTALL_IF_LATEST"] = "1"
env["CODEX_UPDATE_FROM_RELEASE"] = update_guard_from_release
if old_updater_parent_pid is not None:
env["CODEX_TEST_PARENT_PID"] = str(old_updater_parent_pid)
env["CODEX_TEST_PARENT_START"] = process_start_time()
if use_mirror is None:
env.pop("CODEX_INSTALLER_USE_RELEASES_OPENAI_COM", None)
else:
@@ -702,6 +825,13 @@ def run_installer_in(
return result, requests
def process_start_time() -> str:
details = subprocess.check_output(
["ps", "-p", str(os.getpid()), "-o", "stat=", "-o", "lstart="], text=True
).strip()
return details.split(maxsplit=1)[1]
def create_package_release(
root: Path,
*,