cefsharp/CefSharp · warning · InvalidOperationException

Unexpected failure of calling CEF->GetZoomLevelAsync

Error message

Unexpected failure of calling CEF->GetZoomLevelAsync

What it means

Thrown inside the ZoomIn menu handler's task continuation when GetZoomLevelAsync did not complete successfully (faulted or canceled). The continuation checks task status and throws InvalidOperationException wrapping the original AggregateException so the failure is surfaced rather than silently swallowed. With TaskContinuationOptions.ExecuteSynchronously this throws on the captured context.

Source

Thrown at CefSharp.WinForms.Example/BrowserForm.cs:358

        }

        private void ZoomInToolStripMenuItemClick(object sender, EventArgs e)
        {
            var control = GetCurrentTabControl();
            if (control != null)
            {
                var task = control.Browser.GetZoomLevelAsync();

                task.ContinueWith(previous =>
                {
                    if (previous.Status == TaskStatus.RanToCompletion)
                    {
                        var currentLevel = previous.Result;
                        control.Browser.SetZoomLevel(currentLevel + ZoomIncrement);
                    }
                    else
                    {
                        throw new InvalidOperationException("Unexpected failure of calling CEF->GetZoomLevelAsync", previous.Exception);
                    }
                }, TaskContinuationOptions.ExecuteSynchronously);
            }
        }

        private void ZoomOutToolStripMenuItemClick(object sender, EventArgs e)
        {
            var control = GetCurrentTabControl();
            if (control != null)
            {
                var task = control.Browser.GetZoomLevelAsync();
                task.ContinueWith(previous =>
                {
                    if (previous.Status == TaskStatus.RanToCompletion)
                    {
                        var currentLevel = previous.Result;
                        control.Browser.SetZoomLevel(currentLevel - ZoomIncrement);
                    }

View on GitHub (pinned to 16bc6e0711)

Solutions

  1. Gate the zoom handler on IsBrowserInitialized and not IsDisposed.
  2. Observe the task via await instead of ContinueWith+throw to handle faults cleanly.
  3. Log previous.Exception instead of rethrowing to avoid unobserved-exception crashes.

Example fix

// before
task.ContinueWith(p => {
  if (p.Status != TaskStatus.RanToCompletion)
    throw new InvalidOperationException("...", p.Exception);
}, TaskContinuationOptions.ExecuteSynchronously);

// after
try { var level = await control.Browser.GetZoomLevelAsync();
       control.Browser.SetZoomLevel(level + ZoomIncrement); }
catch (Exception ex) { /* log */ }
Defensive patterns

Strategy: try-catch

Validate before calling

if (control?.Browser == null || !control.Browser.IsBrowserInitialized || control.Browser.IsDisposed) return;

Type guard

public static bool CanQueryZoom(IWebBrowser b) => b != null && b.IsBrowserInitialized && !b.IsDisposed;

Try / catch

try { var level = await control.Browser.GetZoomLevelAsync();
      control.Browser.SetZoomLevel(level + ZoomIncrement); }
catch (InvalidOperationException) { /* zoom query failed, skip */ }

Prevention

When it happens

Trigger: GetZoomLevelAsync faults — typically because the browser/frame was disposed or not yet initialized, or the CEF IPC call to retrieve zoom failed. Can also fire if the underlying CEF host is invalid.

Common situations: Clicking Zoom In while the browser is mid-navigation, during shutdown, or before initialization completes. The exception is thrown in a fire-and-forget continuation so it may surface as an unobserved task exception.

Related errors


AI-assisted analysis of cefsharp/CefSharp@16bc6e0711 (2026-08-13). Data as JSON: /api/errors/b5cd529c86a30eb5. Report an issue: GitHub.