From 75ae7028eb0b3318edc62829292697ea403b10df Mon Sep 17 00:00:00 2001 From: kumburovicbranko682-boop Date: Sat, 27 Jun 2026 19:43:40 +0800 Subject: [PATCH] refactor(browser): gettabbyid returns undefined when tab not found despite non-nullable return type `Array.prototype.find()` returns `undefined` when no element matches, but the function declares its return type as `Promise` (non-nullable). The `as TargetInfo` cast silences the compiler but doesn't prevent `undefined` at runtime. Any caller accessing properties on the result (e.g., `tab.url`, `tab.webSocketDebuggerUrl`) will get a TypeError: "Cannot read properties of undefined". This is especially likely during race conditions where a tab closes between `getTabs` and the caller using the result. Affected files: utils.ts Signed-off-by: kumburovicbranko682-boop <295886834+kumburovicbranko682-boop@users.noreply.github.com> --- .../provider/built-in/dedicated/chrome/cdp-client/utils.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/browser/provider/built-in/dedicated/chrome/cdp-client/utils.ts b/src/browser/provider/built-in/dedicated/chrome/cdp-client/utils.ts index 956030ba7bf..50203c69817 100644 --- a/src/browser/provider/built-in/dedicated/chrome/cdp-client/utils.ts +++ b/src/browser/provider/built-in/dedicated/chrome/cdp-client/utils.ts @@ -16,8 +16,12 @@ export async function getTabs (port: number): Promise { export async function getTabById (port: number, id: string): Promise { const tabs = await getTabs(port); + const tab = tabs.find(tab => tab.id === id); - return tabs.find(tab => tab.id === id) as TargetInfo; + if (!tab) + throw new Error(`Tab with id "${id}" not found`); + + return tab; } export async function getFirstTab (port: number): Promise {