can1357/oh-my-pi · error · ToolError

Browser is not connected

Error message

Browser is not connected

What it means

#findAttachedTarget requires an active browser connection (`this.#browser`); if the worker's browser instance is disconnected or was never connected, it throws this ToolError instead of attempting a lookup. It is a precondition failure for any per-target operation.

Source

Thrown at packages/coding-agent/src/tools/browser/tab-worker.ts:1134

					timeout: payload.timeoutMs,
				});
			}
			this.#targetId = await targetIdForPage(this.#page);
			this.#transport.send({ type: "ready", info: await this.#currentReadyInfo() });
		} catch (error) {
			// A failed headless init leaves the worker's page orphaned in the shared
			// browser (the supervisor retries with a fresh worker), so close it before
			// reporting. Attach mode adopts an existing target — never close it.
			const page = this.#page;
			if (payload.mode === "headless" && page && !page.isClosed()) {
				await page.close().catch(() => undefined);
			}
			this.#transport.send({ type: "init-failed", error: errorPayload(error) });
		}
	}

	async #findAttachedTarget(targetId: string): Promise<Target> {
		if (!this.#browser) throw new ToolError("Browser is not connected");
		for (const target of this.#browser.targets()) {
			if ((await targetIdForTarget(target).catch(() => "")) !== targetId) continue;
			return target;
		}
		throw new ToolError(`Target ${targetId} is no longer available on the attached browser`);
	}

	/**
	 * Tell the omp browser relay this worker drives the adopted page, so the
	 * relay adds it to the per-window "omp" tab group. Best-effort: plain CDP
	 * backends (real Chrome, cmux) reject the relay-private method.
	 */
	async #claimRelayTarget(page: Page): Promise<void> {
		let session: CDPSession | undefined;
		try {
			session = await page.createCDPSession();
			// Puppeteer's protocol map cannot express the relay-private method; the
			// send signature is otherwise identical.

View on GitHub (pinned to 9690622007)

Solutions

  1. Check/reestablish the browser connection before dispatching tab operations (reconnect then retry)
  2. Verify the browser process is still running (check for crash or manual close)
  3. Ensure connect() is awaited before issuing any tab commands
  4. If reconnect is not possible, restart the browser session and re-acquire target IDs

Example fix

// before
await worker.runInTab(targetId, code); // throws if browser dropped
// after
if (!worker.isConnected) await worker.connect();
await worker.runInTab(targetId, code);
Defensive patterns

Strategy: validation

Validate before calling

if (!worker.isConnected) {
  await worker.connect(); // or surface a clear 'browser offline' state
}

Type guard

function browserReady(w: { isConnected: boolean }): boolean {
  return w.isConnected;
}

Try / catch

try {
  await worker.runInTab(targetId, code);
} catch (err) {
  if (err instanceof ToolError && err.message === 'Browser is not connected') {
    await reconnectBrowser();
    return worker.runInTab(targetId, code);
  }
  throw err;
}

Prevention

When it happens

Trigger: Any operation that resolves a targetId while the CDP connection is down: browser process exited/crashed, disconnect event fired but worker not reconnected, or an operation dispatched to the worker before `connect` completed.

Common situations: Browser closed by the user or OOM-killed mid-session; network drop to a remote browser; race where the tab operation arrives during browser shutdown/reconnect; worker reused after a previous teardown.

Related errors


AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31). Data as JSON: /api/errors/5bc213617b552232. Report an issue: GitHub.