{"record":{"id":"817210aa9ce52672","repo":"cefsharp/CefSharp","slug":"frame-is-invalid-unable-to-continue","errorCode":null,"errorMessage":"Frame is invalid, unable to continue.","messagePattern":"Frame is invalid, unable to continue\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"CefSharp.Core/WebBrowserExtensionsEx.cs","lineNumber":65,"sourceCode":"\n                tcs.TrySetResult(entry);\n            });\n\n            return tcs.Task;\n        }\n\n        /// <summary>\n        /// Downloads the specified <paramref name=\"url\"/> and calls <paramref name=\"completeHandler\"/>\n        /// when the download is complete. Makes a GET Request.\n        /// </summary>\n        /// <param name=\"frame\">valid frame</param>\n        /// <param name=\"url\">url to download</param>\n        /// <param name=\"completeHandler\">Action to be executed when the download is complete.</param>\n        public static void DownloadUrl(this IFrame frame, string url, Action<IUrlRequest, Stream>  completeHandler)\n        {\n            if (!frame.IsValid)\n            {\n                throw new Exception(\"Frame is invalid, unable to continue.\");\n            }\n\n            //Can be created on any valid CEF Thread, here we'll use the CEF UI Thread\n            Cef.UIThreadTaskFactory.StartNew(delegate\n            {\n                var request = frame.CreateRequest(false);\n\n                request.Method = \"GET\";\n                request.Url = url;\n\n                var memoryStream = new MemoryStream();\n\n                var urlRequestClient = Fluent.UrlRequestClient\n                    .Create()\n                    .OnDownloadData((req, stream) =>\n                    {\n                        stream.CopyTo(memoryStream);\n                    })","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/cefsharp/CefSharp/blob/16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec/CefSharp.Core/WebBrowserExtensionsEx.cs#L47-L83","documentation":"Thrown by the DownloadUrl extension method when frame.IsValid is false. A frame becomes invalid after navigation, frame detach, or browser disposal — CEF has already torn down the underlying CefFrame handle. The method cannot create a request on a dead frame, so it refuses immediately rather than silently failing on the CEF UI thread.","triggerScenarios":"Calling frame.DownloadUrl(url, handler) after the page navigated away, after the frame was removed from the DOM, or after the browser was disposed. Common in FrameLoadStart/FrameLoadEnd racing or in async callbacks that outlive the frame.","commonSituations":"Storing an IFrame reference in a field and using it later in an async continuation. Handling a download triggered from a popup frame that closed. Cross-frame access after a redirect changed main frame identity.","solutions":["Check frame.IsValid immediately before calling DownloadUrl and skip if false.","Reacquire the frame from browser.MainFrame at the call site rather than caching it long-term.","Move the download trigger into a lifecycle-safe handler (e.g. LoadingStateChanged with the current frame captured at call time)."],"exampleFix":"// before\nframe.DownloadUrl(url, OnComplete);\n\n// after\nif (frame.IsValid)\n{\n    frame.DownloadUrl(url, OnComplete);\n}","handlingStrategy":"validation","validationCode":"if (frame == null || !frame.IsValid) return;\nframe.DownloadUrl(url, handler);","typeGuard":"public static bool IsFrameUsable(IFrame f) => f != null && f.IsValid;","tryCatchPattern":"try { frame.DownloadUrl(url, handler); }\ncatch (Exception ex) when (ex.Message.Contains(\"Frame is invalid\")) { /* re-fetch frame or skip */ }","preventionTips":["Never cache IFrame references across navigations.","Check IsValid immediately before frame-dependent calls.","Capture the frame at call-time from browser.MainFrame."],"tags":["frame-invalid","download","lifecycle","cefscore-core"],"backgroundTag":null,"analyzedSha":"16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec","analyzedAt":"2026-08-13T19:57:05.828Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}