{"record":{"id":"22969dfaac095f84","repo":"microsoft/playwright","slug":"could-not-connect-to-channel-invalid-devtoolsa","errorCode":null,"errorMessage":"Could not connect to ${channel}: invalid DevToolsActivePort file at ${devToolsActivePortPath}.","messagePattern":"Could not connect to (.+?): invalid DevToolsActivePort file at (.+?)\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/playwright-core/src/server/chromium/chromium.ts","lineNumber":494,"sourceCode":"\nasync function resolveChannelEndpoint(progress: Progress, channel: string): Promise<string> {\n  const userDataDir = defaultUserDataDirForChannel(channel);\n  if (!userDataDir)\n    throw new Error(`Connecting to ${channel} by channel name is not supported on ${process.platform}.`);\n\n  const devToolsActivePortPath = path.join(userDataDir, 'DevToolsActivePort');\n  progress.log(`<ws preparing> reading ${devToolsActivePortPath}`);\n\n  const contents = await progress.race(fs.promises.readFile(devToolsActivePortPath, 'utf-8').catch(() => undefined));\n  if (!contents) {\n    throw new Error(\n        `Could not connect to ${channel}: DevToolsActivePort file not found at ${devToolsActivePortPath}.\\n` +\n        remoteDebuggingHint(channel));\n  }\n\n  const port = parseInt(contents.trim(), 10);\n  if (isNaN(port))\n    throw new Error(`Could not connect to ${channel}: invalid DevToolsActivePort file at ${devToolsActivePortPath}.`);\n\n  const endpoint = `ws://localhost:${port}/devtools/browser`;\n  progress.log(`<ws preparing> resolved channel \"${channel}\" to ${endpoint}`);\n  return endpoint;\n}\n\nasync function seleniumErrorHandler(params: HTTPRequestParams, response: http.IncomingMessage) {\n  const body = await streamToString(response);\n  let message = body;\n  try {\n    const json = JSON.parse(body);\n    message = json.value.localizedMessage || json.value.message;\n  } catch (e) {\n  }\n  return new Error(`Error connecting to Selenium at ${params.url}: ${message}`);\n}\n\nfunction addProtocol(url: string) {","sourceCodeStart":476,"sourceCodeEnd":512,"githubUrl":"https://github.com/microsoft/playwright/blob/c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6/packages/playwright-core/src/server/chromium/chromium.ts#L476-L512","documentation":"Thrown by resolveChannelEndpoint after reading the DevToolsActivePort file: its content is present but parseInt(contents.trim(), 10) returns NaN. The file exists but does not contain a parseable port number, indicating a corrupted or unexpected file at the channel's user-data-dir.","triggerScenarios":"chromium.connectOverCDP('chrome') where the DevToolsActivePort file exists but contains non-numeric text (e.g. an error message written there, a partial write, or a file left by a different tool).","commonSituations":"A crashed/aborted Chrome left a malformed file. A custom channel build writes a different format. Another tool owns that path. Permissions issue causing a truncated read.","solutions":["Delete the corrupted DevToolsActivePort file in the channel's user-data-dir and relaunch Chrome with remote debugging.","Use a clean/throwaway user-data-dir for Chrome: 'chrome --remote-debugging-port=9222 --user-data-dir=/tmp/cdp', then connect by http URL.","Connect by an explicit ws:// endpoint obtained from http://localhost:9222/json/version instead of by channel name."],"exampleFix":"// before\nconst b = await chromium.connectOverCDP('chrome');\n// after\n// shell: rm -f \"<channelUserDataDir>/DevToolsActivePort\"\n//        google-chrome --remote-debugging-port=9222 --user-data-dir=/tmp/cdp\nconst b = await chromium.connectOverCDP('http://localhost:9222');","handlingStrategy":"validation","validationCode":"import fs from 'fs';\nfunction assertDevToolsPortValid(file: string) {\n  const txt = fs.readFileSync(file, 'utf-8').trim();\n  if (!Number.isInteger(parseInt(txt, 10)))\n    throw new Error(`DevToolsActivePort at ${file} is not a valid port; delete it and relaunch Chrome`);\n}","typeGuard":"function devToolsPortIsValid(txt: string): boolean {\n  return Number.isInteger(parseInt(txt.trim(), 10));\n}","tryCatchPattern":"try {\n  browser = await chromium.connectOverCDP('chrome');\n} catch (e) {\n  if (/invalid DevToolsActivePort/.test(String(e.message))) {\n    // delete the corrupted file and relaunch Chrome with a clean profile\n  }\n  throw e;\n}","preventionTips":["Use a dedicated --user-data-dir for CDP so DevToolsActivePort is not contaminated.","Delete stale DevToolsActivePort files when Chrome did not shut down cleanly."],"tags":["cdp","connect","chromium","remote-debugging","corrupted-state"],"backgroundTag":null,"analyzedSha":"c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6","analyzedAt":"2026-08-12T07:26:36.950Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}