Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

fix(installer): cancel pending native sign-in starts

Afloatc6679e0c84e6
diff --git a/apps/linger-installer/AGENTS.md b/apps/linger-installer/AGENTS.md
index 2e27eda3..fe3f1f9b 100644
--- a/apps/linger-installer/AGENTS.md
+++ b/apps/linger-installer/AGENTS.md
@@ -10,6 +10,9 @@ Only the authorized no-store login endpoint and explicit credentials view may
show the user's password; redact events, diagnostics and persisted job snapshots.
Browser closure never cancels installation. Repeated launches reuse the manager,
instance mutations remain locked, and cancellation retains account/data/device.
+Native sign-in cancellation and manager shutdown invalidate pending runtime
+inspections before they can launch a login process; a newer explicit start owns
+the instance's pending inspection.
A start returns the instance's running or queued job; a job that finished its
run and waits for the person (`waiting_user`, e.g. `LOCAL_MODELS_UNAVAILABLE`)
is canceled and replaced, so Try Again checks the computer again.
diff --git a/apps/linger-installer/src/server/runtime-login-start-cancel.test.ts b/apps/linger-installer/src/server/runtime-login-start-cancel.test.ts
new file mode 100644
index 00000000..e036bd53
--- /dev/null
+++ b/apps/linger-installer/src/server/runtime-login-start-cancel.test.ts
@@ -0,0 +1,185 @@
+import { afterEach, expect, it, vi } from "vitest";
+import { mkdtemp, writeFile, rm, readFile } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import path from "node:path";
+import type { inspectRuntime } from "@linger/local-installation/managed-runtimes";
+import { RuntimeLoginManager } from "./runtime-login.js";
+
+vi.mock("@linger/local-installation/managed-codex", () => ({
+ managedRuntimeEnvironment: async () => ({}),
+}));
+
+const clean: (() => Promise<unknown>)[] = [];
+afterEach(async () => {
+ for (const fn of clean.splice(0).reverse()) await fn();
+});
+function gate() {
+ let resolve!: () => void;
+ const promise = new Promise<void>((done) => {
+ resolve = done;
+ });
+ return { promise, resolve };
+}
+async function fixture() {
+ const home = await mkdtemp(path.join(tmpdir(), "runtime-pending-"));
+ clean.push(() => rm(home, { recursive: true, force: true }));
+ const starts = path.join(home, "starts.txt");
+ const script = path.join(home, "login.cjs");
+ await writeFile(
+ script,
+ `require('node:fs').appendFileSync(${JSON.stringify(starts)}, process.argv[2] + '\\n'); console.log('https://auth.openai.com/codex/device'); setInterval(() => {}, 1000);`,
+ );
+ const state = (
+ label: string,
+ ): Awaited<ReturnType<typeof inspectRuntime>> => ({
+ runtime: "codex",
+ executable: process.execPath,
+ installed: true,
+ ready: false,
+ loginCommand: "unused",
+ descriptor: {
+ runtime: "codex",
+ executable: process.execPath,
+ argvPrefix: [],
+ installed: true,
+ authenticated: false,
+ authMode: null,
+ loginCommand: {
+ executable: process.execPath,
+ args: [script, label],
+ env: {},
+ },
+ deviceLoginCommand: {
+ executable: process.execPath,
+ args: [script, label],
+ env: {},
+ },
+ },
+ });
+ const input = {
+ instance: "default",
+ home,
+ root: home,
+ runtime: "codex" as const,
+ };
+ return { starts, state, input };
+}
+
+it("cancel invalidates an in-flight inspection without canceling a later retry", async () => {
+ const { starts, state, input } = await fixture();
+ const entered = gate();
+ const resume = gate();
+ let calls = 0;
+ const manager = new RuntimeLoginManager(async () => {
+ if (++calls === 1) {
+ entered.resolve();
+ await resume.promise;
+ return state("canceled");
+ }
+ return state("retry");
+ }, "linux");
+ clean.push(async () => manager.close());
+ const pending = manager.start(input);
+ await entered.promise;
+ expect(manager.cancel(input.instance)).toEqual({ state: "idle" });
+ resume.resolve();
+ expect(await pending).toEqual({ state: "idle" });
+ await expect(readFile(starts)).rejects.toMatchObject({ code: "ENOENT" });
+ await manager.start(input);
+ await expect.poll(() => readFile(starts, "utf8")).toBe("retry\n");
+ await expect
+ .poll(async () => (await manager.status(input)).state)
+ .toBe("waiting_user");
+ expect(await readFile(starts, "utf8")).toBe("retry\n");
+});
+
+it("close invalidates pending inspections and prevents later login processes", async () => {
+ const { starts, state, input } = await fixture();
+ const entered = gate();
+ const resume = gate();
+ let calls = 0;
+ const manager = new RuntimeLoginManager(async () => {
+ if (++calls === 2) entered.resolve();
+ await resume.promise;
+ return state("closed");
+ }, "linux");
+ clean.push(async () => manager.close());
+ const pending = [
+ manager.start(input),
+ manager.start({ ...input, instance: "other" }),
+ ];
+ await entered.promise;
+ manager.close();
+ resume.resolve();
+ expect(await Promise.all(pending)).toEqual([
+ { state: "idle" },
+ { state: "idle" },
+ ]);
+ expect(await manager.start(input)).toEqual({ state: "idle" });
+ await expect(readFile(starts)).rejects.toMatchObject({ code: "ENOENT" });
+});
+
+it("an older inspection cannot launch a login after a newer start finds credentials ready", async () => {
+ const { starts, state, input } = await fixture();
+ const entered = gate();
+ const resume = gate();
+ let calls = 0;
+ const manager = new RuntimeLoginManager(async () => {
+ if (++calls === 1) {
+ entered.resolve();
+ await resume.promise;
+ return state("stale");
+ }
+ return { ...state("ready"), ready: true };
+ }, "linux");
+ clean.push(async () => manager.close());
+ const pending = manager.start(input);
+ await entered.promise;
+ expect(await manager.start(input)).toEqual({ state: "ready" });
+ resume.resolve();
+ expect(await pending).toEqual({ state: "idle" });
+ await expect(readFile(starts)).rejects.toMatchObject({ code: "ENOENT" });
+});
+
+it("reports an active inspection failure and allows a subsequent start", async () => {
+ const { starts, state, input } = await fixture();
+ let calls = 0;
+ const error = new Error("inspection failed");
+ const manager = new RuntimeLoginManager(async () => {
+ if (++calls === 1) throw error;
+ return state("retry");
+ }, "linux");
+ clean.push(async () => manager.close());
+ await expect(manager.start(input)).rejects.toBe(error);
+ expect((await manager.start(input)).state).toBe("starting");
+ await expect.poll(() => readFile(starts, "utf8")).toBe("retry\n");
+});
+
+it("cancel aborts the pending inspector and settles its start as idle", async () => {
+ const { starts, input } = await fixture();
+ const entered = gate();
+ let signal!: AbortSignal;
+ let rejectProbe!: (error: Error) => void;
+ const manager = new RuntimeLoginManager(async (_runtime, options) => {
+ signal = options.signal!;
+ entered.resolve();
+ return new Promise<never>((_resolve, reject) => {
+ rejectProbe = reject;
+ signal.addEventListener("abort", () => reject(signal.reason), {
+ once: true,
+ });
+ });
+ }, "linux");
+ clean.push(async () => manager.close());
+ const pending = manager.start(input);
+ // Ensure a failing baseline assertion can still release the controlled inspector.
+ clean.push(async () => {
+ rejectProbe(new Error("fixture cleanup"));
+ await pending.catch(() => {});
+ });
+ await entered.promise;
+ manager.cancel(input.instance);
+ expect(signal.aborted).toBe(true);
+ expect(await pending).toEqual({ state: "idle" });
+ await expect(readFile(starts)).rejects.toMatchObject({ code: "ENOENT" });
+});
diff --git a/apps/linger-installer/src/server/runtime-login.ts b/apps/linger-installer/src/server/runtime-login.ts
index 7099bfcd..fcd786df 100644
--- a/apps/linger-installer/src/server/runtime-login.ts
+++ b/apps/linger-installer/src/server/runtime-login.ts
@@ -42,19 +42,24 @@ function allowedCodexUrl(url: URL, nativeBrowser: boolean) {
/** Native output is private and ephemeral. Never attach it to persisted jobs or logs. */
export class RuntimeLoginManager {
private sessions = new Map<string, Session>();
+ private starting = new Map<string, AbortController>();
+ private closed = false;
constructor(
private inspect: typeof inspectRuntime = inspectRuntime,
private platform: NodeJS.Platform = process.platform,
) {}
- private async probe(input: Input) {
+ private async probe(input: Input, signal?: AbortSignal) {
const env = {
...process.env,
...(await managedRuntimeEnvironment(input.home, input.root)),
};
+ signal?.throwIfAborted();
const state = await this.inspect(input.runtime, {
home: input.home,
env,
- signal: AbortSignal.timeout(15000),
+ signal: signal
+ ? AbortSignal.any([signal, AbortSignal.timeout(15000)])
+ : AbortSignal.timeout(15000),
});
return { env, state };
}
@@ -81,7 +86,23 @@ export class RuntimeLoginManager {
session.child.stdin.write(code + "\n");
return structuredClone(session.snapshot);
}
- const { env, state } = await this.probe(input);
+ if (this.closed) return { state: "idle" };
+ this.starting.get(input.instance)?.abort();
+ const controller = new AbortController();
+ this.starting.set(input.instance, controller);
+ let result;
+ try {
+ result = await this.probe(input, controller.signal);
+ } catch (error) {
+ if (this.starting.get(input.instance) === controller)
+ this.starting.delete(input.instance);
+ if (controller.signal.aborted) return { state: "idle" };
+ throw error;
+ }
+ if (this.starting.get(input.instance) !== controller)
+ return { state: "idle" };
+ this.starting.delete(input.instance);
+ const { env, state } = result;
if (state.ready) return { state: "ready" };
const existing = this.sessions.get(input.instance);
if (
@@ -191,6 +212,8 @@ export class RuntimeLoginManager {
}
/** Stops a login this manager started, such as when the person cancels it. */
cancel(instance: string): Snapshot {
+ this.starting.get(instance)?.abort();
+ this.starting.delete(instance);
this.stop(instance);
return { state: "idle" };
}
@@ -211,6 +234,9 @@ export class RuntimeLoginManager {
this.sessions.delete(instance);
}
close() {
+ this.closed = true;
+ for (const controller of this.starting.values()) controller.abort();
+ this.starting.clear();
for (const name of this.sessions.keys()) this.stop(name);
}
}