{"record":{"id":"b7d63949f9ae1dd8","repo":"projectdiscovery/katana","slug":"could-not-initialize-stealth","errorCode":null,"errorMessage":"could not initialize stealth","messagePattern":"could not initialize stealth","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/engine/headless/browser/browser.go","lineNumber":463,"sourceCode":"\tctx := page.GetContext()\n\tcancelCtx, cancel := context.WithCancel(ctx)\n\tpage = page.Context(cancelCtx)\n\n\tbrowserPage := &BrowserPage{\n\t\tPage:        page,\n\t\tBrowser:     browser,\n\t\tlauncher:    l,\n\t\tcancel:      cancel,\n\t\tuserDataDir: tempDir,\n\t}\n\tif err := browserPage.handlePageDialogBoxes(); err != nil {\n\t\treturn nil, err\n\t}\n\n\t// Add stealth evasion JS\n\t_, err = page.EvalOnNewDocument(stealth.JS)\n\tif err != nil {\n\t\treturn nil, errors.Wrap(err, \"could not initialize stealth\")\n\t}\n\terr = js.InitJavascriptEnv(page)\n\tif err != nil {\n\t\treturn nil, errors.Wrap(err, \"could not initialize javascript env\")\n\t}\n\n\t// Success - cancel any deferred cleanup\n\tsuccessfulPageCreation = true\n\treturn browserPage, nil\n}\n\n// GetPageFromPool returns a page from the pool\nfunc (l *Launcher) GetPageFromPool() (*BrowserPage, error) {\n\tbrowserPage, err := l.browserPool.Get(l.createBrowserPageFunc)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\t// TODO: should we check if the browser is alive because sometimes it","sourceCodeStart":445,"sourceCodeEnd":481,"githubUrl":"https://github.com/projectdiscovery/katana/blob/e3e742739c3746f085943ce918fb4e2b8daf6fe6/pkg/engine/headless/browser/browser.go#L445-L481","documentation":"createBrowserPageFunc wraps page.EvalOnNewDocument(stealth.JS) failure with this message. It means the stealth evasion JavaScript (anti-bot-detection patches) could not be registered to run on every new document of the page via the CDP Page.addScriptToEvaluateOnNewDocument call. This almost always indicates the page/browser connection is already broken, since the script itself is static and cannot be syntactically invalid at runtime.","triggerScenarios":"Calling EvalOnNewDocument after the browser/page context has died (browser crash, WebSocket disconnect, or the page context was cancelled between creation and this call), or the CDP session being invalidated before registration completes.","commonSituations":"Chrome crashing or being killed during page setup under heavy load; the deferred cleanup logic in createBrowserPageFunc racing with page setup when another error path closed the browser; container environments where Chrome dies quickly after launch; network/WS interruptions to a remote Chrome (ChromeWSUrl) instance.","solutions":["Treat this as a dead-page symptom: check the wrapped error for 'target closed' or 'context canceled' and recreate the browser/page instead of retrying EvalOnNewDocument.","Verify the remote Chrome instance (if using ChromeWSUrl) is reachable and stable; reconnect and retry page creation.","Reduce concurrent page creation to avoid resource exhaustion that kills Chrome mid-setup.","Ensure Chrome and katana/rod protocol versions are compatible."],"exampleFix":"// before: retrying setup on the same broken page\nif _, err := page.EvalOnNewDocument(stealth.JS); err != nil {\n    // retry on same page -> keeps failing\n}\n\n// after: recreate the page when stealth init fails\nif _, err := page.EvalOnNewDocument(stealth.JS); err != nil {\n    page.Close()\n    return recreatePage() // new browser page with fresh CDP session\n}","handlingStrategy":"try-catch","validationCode":"if page.GetContext().Err() != nil {\n    // context already dead; skip stealth init and recreate the page\n    return recreatePage()\n}","typeGuard":null,"tryCatchPattern":"if _, err := page.EvalOnNewDocument(stealth.JS); err != nil {\n    var cause error = errors.Unwrap(err)\n    log.Printf(\"stealth init failed: %v\", cause)\n    page.Close()\n    return recreatePage() // a dead session cannot be reused\n}","preventionTips":["Don't hold pages across long idle periods; create and dispose them per task.","Check page context validity before running setup steps.","Avoid memory pressure that kills Chrome mid-setup (limit concurrency).","For remote Chrome, add connection health checks before page creation."],"tags":["headless-browser","stealth","cdp","rod"],"backgroundTag":"cdp-script-injection-failed","analyzedSha":"e3e742739c3746f085943ce918fb4e2b8daf6fe6","analyzedAt":"2026-09-03T14:55:13.248Z","contentChangedAt":"2026-09-03T14:55:13.248Z","schemaVersion":2},"datasetVersion":"2026-09-10T17:17:09.494Z"}