Skip to content

Commit 261131d

Browse files
authored
fix(update): refuse dashboard restart under a systemd unit OpenCodex does not own (lidge-jun#7996)
* fix(update): refuse dashboard restart under a systemd unit OpenCodex does not own Refuse before package mutation when the worker cannot leave the service cgroup or the proxy is not run by OpenCodex's own user unit, and settle a dead worker's job on status reads. Part of lidge-jun#7967. * fix(update): refuse unsupervised dashboard updates with or without restart The update stops the proxy before replacing the package, so restart:false was not a safe fallback. Detect a systemd service from its cgroup too, accept a delegated child cgroup of the own unit, write the settled status from the compared record, and add hard spawn barriers to the refusal test. * fix(update): report a dead worker's job as failed on status without writing it A status poll that wrote the settled record could overwrite a job another process started between its read and its rename. The poll now returns a failed view; the next run's existing stale recovery settles the persisted record. * fix(update): judge the innermost systemd unit and refuse only an update that would run A desktop terminal runs in a scope below user@<uid>.service, which the component check took for a service, and the CI runner's own service preempted the source-checkout refusal. Read the innermost unit of the systemd hierarchy, and consult the supervisor only when an update is available. * fix(update): let the observed cgroup win over an inherited INVOCATION_ID A shell moved into its own systemd-run scope keeps INVOCATION_ID; only a missing cgroup observation falls back to the variable. * docs(structure): state that the observed cgroup unit decides before INVOCATION_ID --------- Co-authored-by: JUN <[email protected]>
1 parent b53b5d2 commit 261131d

9 files changed

Lines changed: 230 additions & 11 deletions

File tree

‎docs-site/src/content/docs/reference/management-api.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ See [Codex prompt layers](/guides/codex-prompt/) for the layer model and the key
278278
| `GET /api/diagnostics/project-config` | Read cached project configuration warnings | — |
279279
| `POST /api/sync` | Sync the current model catalog into Codex | 500 failed sync |
280280
| `GET /api/update/check` | Asynchronously check the `latest` or `preview` package channel and refresh the package cache on success | 400 invalid tag |
281-
| `POST /api/update/run` | Asynchronously check a fresh package version, then start an update job, optionally followed by restart | 400 invalid body; job-specific conflict/error status |
281+
| `POST /api/update/run` | Asynchronously check a fresh package version, then start an update job, optionally followed by restart | 400 invalid body; 409 `update_restart_unsupervised` when the proxy runs inside a systemd unit other than OpenCodex's own user service, or the worker cannot leave the service cgroup (with or without restart, since the update stops the proxy first); job-specific conflict/error status |
282282
| `GET /api/update/status` | Poll an update job by id | 404 unknown job |
283283
| `GET, PUT /api/sidecar-settings` | Read or update web-search and vision sidecar model/backend settings | 400 invalid shape, backend, or limit |
284284
| `GET, PUT /api/shadow-call-settings` | Read or update shadow-call interception settings, including `sourceModels`; GET also returns the runtime-owned `sourceModelOptions` inventory | 400 invalid shape or value, including an empty, duplicate, whitespace-bearing, or provider-qualified source prefix, or more than 32 prefixes / 128 characters per prefix; source-list failures carry `code: shadow_call_source_invalid`, target conflicts `code: shadow_call_target_invalid` |

‎src/server/management/config-routes.ts‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -972,11 +972,22 @@ async function handleConfigRoutesImpl(ctx: ManagementContext): Promise<Response
972972
// Resolve the systemd-scope launcher before spawning: the first request
973973
// would otherwise run up to four sequential five-second probes inside
974974
// spawnSync on the shared event loop.
975-
const { resolveSystemdRunAsync } = await import("../../update/worker-launch");
975+
const { resolveSystemdRunAsync, dashboardRestartRefusal } = await import("../../update/worker-launch");
976976
const systemdRun = process.platform === "linux" && process.env.INVOCATION_ID
977977
? await resolveSystemdRunAsync()
978978
: undefined;
979-
return jsonResponse({ ok: true, job: startUpdateJob(channel, body.restart !== false, {
979+
const restart = body.restart !== false;
980+
// Refuse before any package mutation, with or without restart: the update stops the proxy
981+
// first, so a worker that dies with the service cgroup, or a restart aimed at a unit that
982+
// does not own this proxy, leaves the dashboard down (#7967).
983+
// Only an update that would actually run is refused; an unavailable update or a source
984+
// checkout keeps its own refusal from startUpdateJob.
985+
if (checked.canUpdate) {
986+
const { TASK } = await import("../../service/state");
987+
const refusal = (deps.dashboardRestartRefusal ?? dashboardRestartRefusal)({ systemdRun, unitName: TASK });
988+
if (refusal) return jsonResponse({ error: refusal, code: "update_restart_unsupervised" }, 409);
989+
}
990+
return jsonResponse({ ok: true, job: startUpdateJob(channel, restart, {
980991
checkForUpdateFn: () => checked,
981992
spawnWorkerFn: (jobId, runChannel, runRestart) =>
982993
spawnGuiUpdateWorker(jobId, runChannel, runRestart, { resolveSystemdRun: () => systemdRun }),
@@ -990,8 +1001,8 @@ async function handleConfigRoutesImpl(ctx: ManagementContext): Promise<Response
9901001
}
9911002

9921003
if (url.pathname === "/api/update/status" && req.method === "GET") {
993-
const { readUpdateJob } = await import("../../update/job");
994-
const job = readUpdateJob(url.searchParams.get("jobId"));
1004+
const { readUpdateJobForStatus } = await import("../../update/job");
1005+
const job = readUpdateJobForStatus(url.searchParams.get("jobId"));
9951006
if (!job) return jsonResponse({ error: "update job not found" }, 404);
9961007
return jsonResponse({ ok: true, job });
9971008
}

‎src/server/management/context.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ export interface ManagementApiDeps {
5959
/** Read-only process-local aggregate metrics; absent keeps the scrape route unavailable. */
6060
requestMetrics?: RequestMetricsSnapshotter;
6161
checkPackageUpdate?: (channel: Channel) => Promise<UpdateCheckResult>;
62+
/** Test seam for the dashboard restart supervisor check (src/update/worker-launch.ts). */
63+
dashboardRestartRefusal?: typeof import("../../update/worker-launch").dashboardRestartRefusal;
6264
remoteWorkspaceHub?: RemoteWorkspaceHubApi;
6365
remoteWorkspaceSessions?: RemoteWorkspaceSessionsApi;
6466
/** The listener retains and awaits teardown only after this optional subsystem activates. */

‎src/update/job.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,26 @@ export function staleActiveUpdateJobReason(
527527
return null;
528528
}
529529

530+
/**
531+
* Status view of an active record whose worker is gone (#7967). A worker killed with its service
532+
* cgroup never writes a terminal state, so the persisted record stays `running`. The view reports
533+
* it as `failed` with the reason so the dashboard stops waiting, without writing: the persisted
534+
* record is settled only by the next run's existing stale recovery, which holds the start path,
535+
* so a status poll can never overwrite a job another process started meanwhile.
536+
*/
537+
export function readUpdateJobForStatus(
538+
jobId?: string | null,
539+
now = Date.now(),
540+
isAlive: (pid: number) => boolean = isProcessAlive,
541+
): UpdateJobState | null {
542+
const job = readUpdateJob(jobId);
543+
if (!job) return null;
544+
const reason = staleActiveUpdateJobReason(job, now, isAlive);
545+
if (!reason) return job;
546+
const line = `Update worker stopped before finishing: ${reason}.`;
547+
return { ...job, status: "failed", error: line, exitCode: null, log: [...job.log, line] };
548+
}
549+
530550
/**
531551
* Spawn the GUI update worker without inheriting the proxy's LISTEN socket.
532552
*

‎src/update/worker-launch.ts‎

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { probeSpawn, probeSpawnSync } from "./mutation-guard.mjs";
2-
import { accessSync, constants, realpathSync, statSync } from "node:fs";
2+
import { accessSync, constants, readFileSync, realpathSync, statSync } from "node:fs";
33
import { dirname, isAbsolute } from "node:path";
44

55
/**
@@ -171,6 +171,76 @@ export function resetSystemdRunProbeForTests(): void {
171171
systemdRunProbePending = undefined;
172172
}
173173

174+
export interface DashboardRestartContext {
175+
platform?: NodeJS.Platform;
176+
env?: NodeJS.ProcessEnv;
177+
/** The resolved scope launcher, or undefined when none is usable. */
178+
systemdRun: string | undefined;
179+
/** The OpenCodex-managed user unit (`TASK` in src/service/state.ts) without `.service`. */
180+
unitName: string;
181+
readCgroup?: () => string;
182+
}
183+
184+
function cgroupPaths(cgroup: string): string[] {
185+
const rows = cgroup.split("\n").map(line => line.split(":")).filter(parts => parts.length >= 3);
186+
// cgroup v2 ("0::") or the v1 systemd hierarchy name the unit; controller lines are noise.
187+
const unified = rows.filter(parts => parts[1] === "" || parts[1] === "name=systemd");
188+
return (unified.length > 0 ? unified : rows).map(parts => parts.slice(2).join(":").trim()).filter(Boolean);
189+
}
190+
191+
/** The innermost systemd unit named by a cgroup path: its last `.service` or `.scope` component. */
192+
function innermostUnit(path: string): { unit: string; index: number; parts: string[] } | null {
193+
const parts = path.split("/");
194+
for (let index = parts.length - 1; index >= 0; index--) {
195+
const part = parts[index]!;
196+
if (part.endsWith(".service") || part.endsWith(".scope")) return { unit: part, index, parts };
197+
}
198+
return null;
199+
}
200+
201+
/**
202+
* Why a dashboard update must not run against this proxy, or null when it may (#7967).
203+
*
204+
* A dashboard update stops the proxy before it replaces the package, with or without a requested
205+
* restart, and the worker survives that stop only from its own transient scope. Only OpenCodex's
206+
* own user unit is restarted by the updater. A proxy inside any systemd service -- detected from
207+
* `INVOCATION_ID` or from a `.service` cgroup, so a unit that unsets the variable is still seen
208+
* -- is therefore refused unless the scope launcher works and the cgroup places it in
209+
* `<unitName>.service` under a per-user manager (a delegated child cgroup below it is fine). A
210+
* custom system unit with `User=` would lose the worker with its cgroup, and the restart would aim
211+
* at an unrelated, possibly inactive user unit. Outside systemd nothing changes.
212+
*/
213+
export function dashboardRestartRefusal(context: DashboardRestartContext): string | null {
214+
const platform = context.platform ?? process.platform;
215+
const env = context.env ?? process.env;
216+
if (platform !== "linux") return null;
217+
let cgroup: string | null;
218+
try { cgroup = (context.readCgroup ?? (() => readFileSync("/proc/self/cgroup", "utf8")))(); }
219+
catch { cgroup = null; }
220+
const paths = cgroup === null ? [] : cgroupPaths(cgroup);
221+
// The innermost unit decides: a terminal or app scope below the per-user manager
222+
// (user@<uid>.service/app.slice/vte-spawn-1.scope) is not a service; a system or user service is.
223+
const units: ReturnType<typeof innermostUnit>[] = paths.map(innermostUnit);
224+
const observed = units.filter((found): found is NonNullable<typeof found> => found !== null);
225+
// The observed cgroup wins over an inherited INVOCATION_ID (a shell moved into its own scope
226+
// keeps the variable); without an observation the variable alone fails closed.
227+
const inService = observed.length > 0
228+
? observed.some(found => found.unit.endsWith(".service") && !found.unit.startsWith("user@"))
229+
: Boolean(env.INVOCATION_ID);
230+
if (!inService) return null;
231+
const manual = "Stop the service with its own manager, update the package from a shell (for example `ocx update`), then start the service again.";
232+
if (!context.systemdRun || !env.INVOCATION_ID) {
233+
return `Dashboard update is unavailable: this proxy runs inside a systemd service, and the update worker cannot leave the service's cgroup (no usable \`systemd-run --user --scope\`), so stopping the proxy for the update would stop the update too. ${manual}`;
234+
}
235+
const ownUnit = `${context.unitName}.service`;
236+
const managedByOwnUnit = units.some((found): boolean => found !== null && found.unit === ownUnit
237+
&& found.parts.slice(0, found.index).some((part: string) => part.startsWith("user@") && part.endsWith(".service")));
238+
if (!managedByOwnUnit) {
239+
return `Dashboard update is unavailable: this proxy runs under a systemd unit that is not OpenCodex's own \`${context.unitName}.service\` user unit, so the updater can neither keep its worker alive nor restart that unit safely. ${manual}`;
240+
}
241+
return null;
242+
}
243+
174244
export function guiUpdateWorkerCommand(
175245
execPath: string,
176246
args: readonly string[],

‎structure/gui-and-management-api.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ per-request first-party callback reads that live object; a failed write leaves i
274274
| Config/settings | Reasoning retention follows the [panel and settings contract](dashboard-and-usage.md). Read safe config/settings views; mutate supported settings only. Full `PUT /api/config` is disabled so masked secrets are not round-tripped. `PUT /api/settings` accepts `codexAutoStart`, `streamMode`, integer `appOwnedMemoryBudgetMb` (64..4096), strict boolean `codexAccountPickerEnabled`, strict boolean `fastRows`, and a validated per-account `codexQuotaAutoRefresh` toggle (each optional, at least one required). `fastRows` defaults on when absent: false is persisted, true deletes the key, and successful writes echo the effective boolean. An effective change converges the Codex catalog and refreshes enabled or already-owned client integrations after persistence. Picker enable initializes an empty UI-managed selector map, persists before one bounded catalog convergence, and reports only `catalogRefreshPending`; allocation/save failure restores every touched live field and skips convergence. Budget changes synchronously enforce the process-wide evictable retained-state cap; this is separate from RSS/native memory. `streamMode` persists the #314 stream-shape selection in config.json (Windows services need persisted input; macOS eager relay is explicit-only). |
275275
| Startup safety | `GET /api/startup-health` reports whether injected Codex routing is restart-safe, with secret-free service/shim diagnostics. `POST /api/startup-action` provides allowlisted one-click installation for the background service or launcher shim. On Windows a healthy script shim is CLI-only; Codex Desktop requires the background service for full protection. Optional Codex compatibility uses the [confirmed local runtime](clients/codex-desktop.md#optional-compatibility-runtime), [machine-local dashboard consent](clients/codex-desktop.md#dashboard-controls), and [revision-bound next-start preference with shared awaited teardown](clients/codex-desktop.md#proxy-startup-preference); saving that preference performs no runtime action. |
276276
| Windows tray | `GET/POST /api/windows-tray` controls an owned, per-user HKCU login tray. The tray delegates fixed actions to the CLI and is never a proxy supervisor or restart-protection signal. |
277-
| Updates | `GET /api/update/check`, `POST /api/update/run`, and `GET /api/update/status` own dashboard self-update state. A launched worker PID is persisted in `update-job.json`; dead PIDs recover immediately, while legacy active records without a PID recover only after ten minutes. Live PIDs remain exclusive regardless of record age. `GET /api/update/check` and `POST /api/update/run` await one per-channel asynchronous registry lookup and write successful results through to the package cache; run passes that result to the job starter. `GET /api/update/badge` only reads the cache and reports unknown after 40 hours, on missing cache, or on channel mismatch. The badge links to the update surface rather than gating other actions. `GET /api/update/badge?surface=desktop&session=<id>` projects only that process-local Tauri session; missing or expired state is unknown and never falls back to the package cache. `POST /api/update/desktop-snapshot` is a 1 KiB bounded display-state mutation with no install permission. It accepts the existing admin-token principal or a dedicated single-use, ten-second snapshot capability bound to nonce, method, path, PID, port and the SHA-256 digest of the exact body bytes; that capability cannot authorize another route. |
277+
| Updates | `GET /api/update/check`, `POST /api/update/run`, and `GET /api/update/status` own dashboard self-update state. A launched worker PID is persisted in `update-job.json`; dead PIDs recover immediately when a new run starts, and `GET /api/update/status` already reports such a record as `failed` without writing it (`readUpdateJobForStatus`), while legacy active records without a PID recover only after ten minutes. Live PIDs remain exclusive regardless of record age. `GET /api/update/check` and `POST /api/update/run` await one per-channel asynchronous registry lookup and write successful results through to the package cache; run passes that result to the job starter. `GET /api/update/badge` only reads the cache and reports unknown after 40 hours, on missing cache, or on channel mismatch. The badge links to the update surface rather than gating other actions. `GET /api/update/badge?surface=desktop&session=<id>` projects only that process-local Tauri session; missing or expired state is unknown and never falls back to the package cache. `POST /api/update/desktop-snapshot` is a 1 KiB bounded display-state mutation with no install permission. It accepts the existing admin-token principal or a dedicated single-use, ten-second snapshot capability bound to nonce, method, path, PID, port and the SHA-256 digest of the exact body bytes; that capability cannot authorize another route. |
278278
| Providers | Create/update/delete ordinary provider configs and enrich registry metadata. A `POST /api/providers` overwrite of an existing name keeps the ten operator compatibility settings (`PROVIDER_COMPAT_CARRY_FIELDS` in `src/server/management/provider-overwrite-carry.ts`) and the stored key pool only while the destination (adapter, normalized base URL, auth mode when named) is unchanged. It keeps omitted `hideRawReasoning` on every overwrite of the same name, including destination moves, because it is a display policy; a submitted boolean wins. An omitted `inferContextWindow` likewise keeps the latest live same-name state after asynchronous validation, including `false` or a concurrent clear; a submitted boolean wins. It never merges the rest of the old row. `PATCH` is a field mask and keeps every field it does not name; `hideRawReasoning`, `preserveResponsesReasoningContent`, `promoteAdditionalTools` and `inferContextWindow` accept a boolean or `null` to clear them, rejecting other types. `GET /api/providers` exposes configured `inferContextWindow` as an optional boolean without substituting a default; its [custom-only, default-off inference policy](catalog-vendor-context.md) is unchanged. Custom Responses provider forms expose `preserveResponsesReasoningContent` as a default-off plaintext replay switch; unrelated settings saves preserve omission and curated/OpenAI destinations keep their existing defaults. The reserved `openai` card exposes Pool(default)/Direct account mode; `openai-apikey` remains the separate API route. |
279279
| Models | Fetch routed model lists, disabled model visibility, and catalog-facing ids. New non-OAuth registration holds exposure until authoritative discovery; 20 or more distinct switch rows start OFF without disabling the provider. Pending rows cannot accept visibility changes. Ordinary model reads also apply the [new-arrival catalog contract](catalog.md#shared-catalog) before returning models. With a matching persisted configuration, discovery records the baseline, NEW badges, and automatic disables together; HTTP listing does not wait for a separate Codex sync. Inventory already drifted from disk uses a detached policy projection: arrivals remain disabled in management rows without changing live config or disk. |
280280
| OAuth | Login/status/logout for OAuth-backed providers, plus multiauth account management: `GET /api/oauth/accounts`, `PUT /api/oauth/accounts/active`, `PUT /api/oauth/accounts/alias`, `PUT /api/oauth/accounts/pause`, and `DELETE /api/oauth/accounts` list masked accounts per provider, switch the active one, edit its display-only alias, pause/resume a generic OAuth account, and remove one. Paused generic OAuth accounts are excluded from request selection, 429 failover, and proactive token refresh; pausing an active account selects the next usable account when available, and resuming an account restores an active selection if the current one remains paused. With no unpaused account, requests return 403 rather than a login error. Kiro account-list rows include the current automatic-selection projection and closed exclusion reason; an active singleton may still send. The login flow itself is `GET /api/oauth/providers`, `POST /api/oauth/login`, `POST /api/oauth/login/code`, `POST /api/oauth/login/cancel`, `POST /api/oauth/logout`, and `GET /api/oauth/status`; pool controls are `GET/PUT/PATCH /api/oauth/accounts/pool` and `POST /api/oauth/accounts/clear-cooldown`. Login accepts `addAccount: true` to force a fresh browser identity. Meta Muse login start and manual-code continuation require the server-resolved `gui-session` principal before credential acquisition or code submission (including reauth); see the [provider contract](providers-and-adapters.md). Device flows return a structured `deviceCode`; the GUI highlights and copies it before the user opens the verification page. Google Antigravity account rows in `gui/src/components/provider-workspace/ProviderAuthPanel.tsx` display the account-summary `plan` as a badge and include it in the account button’s accessible name. The value is the canonical upstream tier ID (for example `free-tier` or `gemini-pro`), preserved literally across locales rather than inferred as a product name. Absent plans render no badge; pause and reauthentication controls keep their existing behavior. |

0 commit comments

Comments
 (0)