{"record":{"id":"cd3c0a255e5dd134","repo":"jackwener/OpenCLI","slug":"failed-to-create-tab-lease-in-automation-container","errorCode":null,"errorMessage":"Failed to create tab lease in automation container","messagePattern":"Failed to create tab lease in automation container","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"extension/src/background.ts","lineNumber":1103,"sourceCode":"\nasync function createOwnedTabLeaseUnlocked(leaseKey: string, initialUrl?: string): Promise<ResolvedTab> {\n  const targetUrl = (initialUrl && isSafeNavigationUrl(initialUrl)) ? initialUrl : BLANK_PAGE;\n  const role = getOwnedWindowRole(leaseKey);\n  const { windowId, initialTabId } = await ensureOwnedContainerWindow(role, targetUrl, getWindowMode(leaseKey));\n  let tab: chrome.tabs.Tab;\n\n  if (initialTabIsAvailable(initialTabId)) {\n    tab = await chrome.tabs.get(initialTabId);\n    if (!isTargetUrl(tab.url, targetUrl)) {\n      tab = await chrome.tabs.update(initialTabId, { url: targetUrl });\n      await new Promise(resolve => setTimeout(resolve, 300));\n      tab = await chrome.tabs.get(initialTabId);\n    }\n  } else {\n    tab = await chrome.tabs.create({ windowId, url: targetUrl, active: true });\n  }\n  const tabId = tab.id;\n  if (!tabId) throw new Error('Failed to create tab lease in automation container');\n  const group = await ensureOwnedContainerGroup(role, windowId, [tabId]);\n  const sessionWindowId = group?.windowId ?? tab.windowId;\n  if (tab.windowId !== sessionWindowId) tab = await chrome.tabs.get(tabId);\n\n  setLeaseSession(leaseKey, {\n    session: getSessionFromKey(leaseKey),\n    surface: getSurfaceFromKey(leaseKey),\n    kind: 'owned',\n    windowId: sessionWindowId,\n    owned: true,\n    preferredTabId: tabId,\n  });\n  resetWindowIdleTimer(leaseKey);\n  return { tabId, tab };\n}\n\n/** Get or create the dedicated automation container window.\n *  This compatibility helper returns the shared owned container. Leases","sourceCodeStart":1085,"sourceCodeEnd":1121,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/extension/src/background.ts#L1085-L1121","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Retry tab creation once — transient races (immediate close) usually succeed on the second attempt","Verify the target windowId is still open before creating the tab","Fall back to chrome.tabs.query({ url: targetUrl }) to recover the created tab's id","Check for other extension handlers (onCreated/onRemoved) closing tabs during setup"],"exampleFix":"// before\nconst tab = await chrome.tabs.create({ windowId, url: targetUrl, active: true });\n// after\nlet tab = await chrome.tabs.create({ windowId, url: targetUrl, active: true });\nif (!tab.id) {\n  await new Promise(r => setTimeout(r, 100));\n  const [t] = await chrome.tabs.query({ url: targetUrl });\n  tab = t ?? tab;\n}\nif (!tab.id) throw new Error('Failed to create tab lease in automation container');","handlingStrategy":"retry","validationCode":"const win = await chrome.windows.get(windowId).catch(() => null);\nif (!win) throw new Error('Target window closed before tab creation');","typeGuard":"const hasTabId = (tab) => tab != null && typeof tab.id === 'number';","tryCatchPattern":"try {\n  lease = await openAutomationTab(windowId, url);\n} catch (e) {\n  if (/Failed to create tab lease/.test(e.message)) {\n    await new Promise(r => setTimeout(r, 150));\n    lease = await openAutomationTab(windowId, url); // retry once\n  } else throw e;\n}","preventionTips":["Verify windowId is still alive before creating tabs in it","Avoid other handlers closing tabs during automation setup (disable onCreated/onRemoved interference)","Retry tab creation once with a short delay to absorb creation races"],"tags":["chrome-extension","tabs","race-condition","automation"],"backgroundTag":"tab-create-returned-no-id","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}