jackwener/OpenCLI · error · Error

Failed to create tab lease in automation container

Error message

Failed to create tab lease in automation container

What it means

During automation-window setup, a tab is created (or reused from initialTabId) inside the automation container; chrome.tabs.create/get returns a Tab whose id can be undefined in Chrome's type system, and the code treats a missing id as fatal because every subsequent lease call needs a numeric tabId. It throws 'Failed to create tab lease in automation container' so callers fail fast instead of writing a lease keyed by undefined.

Source

Thrown at extension/src/background.ts:1103

async function createOwnedTabLeaseUnlocked(leaseKey: string, initialUrl?: string): Promise<ResolvedTab> {
  const targetUrl = (initialUrl && isSafeNavigationUrl(initialUrl)) ? initialUrl : BLANK_PAGE;
  const role = getOwnedWindowRole(leaseKey);
  const { windowId, initialTabId } = await ensureOwnedContainerWindow(role, targetUrl, getWindowMode(leaseKey));
  let tab: chrome.tabs.Tab;

  if (initialTabIsAvailable(initialTabId)) {
    tab = await chrome.tabs.get(initialTabId);
    if (!isTargetUrl(tab.url, targetUrl)) {
      tab = await chrome.tabs.update(initialTabId, { url: targetUrl });
      await new Promise(resolve => setTimeout(resolve, 300));
      tab = await chrome.tabs.get(initialTabId);
    }
  } else {
    tab = await chrome.tabs.create({ windowId, url: targetUrl, active: true });
  }
  const tabId = tab.id;
  if (!tabId) throw new Error('Failed to create tab lease in automation container');
  const group = await ensureOwnedContainerGroup(role, windowId, [tabId]);
  const sessionWindowId = group?.windowId ?? tab.windowId;
  if (tab.windowId !== sessionWindowId) tab = await chrome.tabs.get(tabId);

  setLeaseSession(leaseKey, {
    session: getSessionFromKey(leaseKey),
    surface: getSurfaceFromKey(leaseKey),
    kind: 'owned',
    windowId: sessionWindowId,
    owned: true,
    preferredTabId: tabId,
  });
  resetWindowIdleTimer(leaseKey);
  return { tabId, tab };
}

/** Get or create the dedicated automation container window.
 *  This compatibility helper returns the shared owned container. Leases

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Retry tab creation once — transient races (immediate close) usually succeed on the second attempt
  2. Verify the target windowId is still open before creating the tab
  3. Fall back to chrome.tabs.query({ url: targetUrl }) to recover the created tab's id
  4. Check for other extension handlers (onCreated/onRemoved) closing tabs during setup

Example fix

// before
const tab = await chrome.tabs.create({ windowId, url: targetUrl, active: true });
// after
let tab = await chrome.tabs.create({ windowId, url: targetUrl, active: true });
if (!tab.id) {
  await new Promise(r => setTimeout(r, 100));
  const [t] = await chrome.tabs.query({ url: targetUrl });
  tab = t ?? tab;
}
if (!tab.id) throw new Error('Failed to create tab lease in automation container');
Defensive patterns

Strategy: retry

Validate before calling

const win = await chrome.windows.get(windowId).catch(() => null);
if (!win) throw new Error('Target window closed before tab creation');

Type guard

const hasTabId = (tab) => tab != null && typeof tab.id === 'number';

Try / catch

try {
  lease = await openAutomationTab(windowId, url);
} catch (e) {
  if (/Failed to create tab lease/.test(e.message)) {
    await new Promise(r => setTimeout(r, 150));
    lease = await openAutomationTab(windowId, url); // retry once
  } else throw e;
}

Prevention

When it happens

Trigger: chrome.tabs.create({ windowId, url, active }) or chrome.tabs.get(initialTabId) resolves with tab.id undefined/null — e.g. the tab was immediately closed by another handler, the windowId is invalid, or the tab was discarded/preredendered so no id was assigned.

Common situations: Race where a user or another extension closes the new tab within milliseconds of creation; targeting a windowId that was closed concurrently; Chrome quirk where prerendered/deferred tabs report no id.

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/cd3c0a255e5dd134. Report an issue: GitHub.