cefsharp/CefSharp · warning · InvalidOperationException

Unexpected failure of calling CEF->GetZoomLevelAsync

Error message

Unexpected failure of calling CEF->GetZoomLevelAsync

What it means

After the underlying CEF browser finishes initializing, CefSharp automatically calls GetZoomLevelAsync to read and sync the current zoom level into the ZoomLevel dependency property. If that CEF call faults (e.g. the browser is disposed mid-call, or CEF reports an internal error), the task continuation throws an InvalidOperationException that wraps the original fault as its InnerException.

Source

Thrown at CefSharp.Wpf/ChromiumWebBrowser.cs:1457

        {
            if (newValue && !IsDisposed)
            {
                var task = this.GetZoomLevelAsync();
                task.ContinueWith(previous =>
                {
                    if (previous.Status == TaskStatus.RanToCompletion)
                    {
                        UiThreadRunAsync(() =>
                        {
                            if (!IsDisposed)
                            {
                                SetCurrentValue(ZoomLevelProperty, previous.Result);
                            }
                        });
                    }
                    else
                    {
                        throw new InvalidOperationException("Unexpected failure of calling CEF->GetZoomLevelAsync", previous.Exception);
                    }
                }, TaskContinuationOptions.ExecuteSynchronously);
            }
        }

        #endregion IsInitialized dependency property

        #region Title dependency property

        /// <summary>
        /// The title of the web page being currently displayed.
        /// </summary>
        /// <value>The title.</value>
        /// <remarks>This property is implemented as a Dependency Property and fully supports data binding.</remarks>
        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }

View on GitHub (pinned to 16bc6e0711)

Solutions

  1. Subscribe to TaskScheduler.UnobservedTaskException at application startup to observe and log these without crashing.
  2. Ensure the browser control is not disposed during or immediately after initialization — delay window close or tab removal until IsBrowserInitializedChanged has fully settled.
  3. Inspect the InnerException (previous.Exception) of the InvalidOperationException to find the real CEF-level fault and address that root cause.
  4. Upgrade CefSharp — older versions had races between init-completion and disposal that were fixed in later releases.

Example fix

// before — unobserved exception crashes the app at GC time
// (no handler, the throw in the continuation is unobserved)

// after — observe at app startup
TaskScheduler.UnobservedTaskException += (s, e) =>
{
    if (e.Exception?.InnerException != null)
        Log.Warn("Unobserved CEF task fault", e.Exception.InnerException);
    e.SetObserved();
};
Defensive patterns

Strategy: try-catch

Validate before calling

// No pre-call validation possible — this is an internal continuation.
// Instead, observe unobserved exceptions globally at startup:
TaskScheduler.UnobservedTaskException += (s, e) =>
{
    if (e.Exception is InvalidOperationException ioe
        && ioe.Message.Contains("GetZoomLevelAsync"))
    {
        Log.Warn("CEF zoom-level sync faulted", ioe.InnerException ?? ioe);
        e.SetObserved();
    }
};

Try / catch

// This exception is thrown inside a library-owned task continuation,
// so you cannot wrap it in try-catch at the call site.
// The only catch surface is TaskScheduler.UnobservedTaskException:
TaskScheduler.UnobservedTaskException += (s, e) =>
{
    e.SetObserved(); // prevent process crash
};

Prevention

When it happens

Trigger: Fires inside OnIsBrowserInitializedChanged when newValue=true and the GetZoomLevelAsync task completes in a Faulted or Canceled state rather than RanToCompletion. The throw happens in a TaskContinuationOptions.ExecuteSynchronously continuation, so it surfaces as an unobserved task exception unless the host subscribes to TaskScheduler.UnobservedTaskException.

Common situations: Rapidly creating and disposing a ChromiumWebBrowser (closing a window or tab immediately after load triggers init then teardown). CEF crashing during browser initialization. Memory or resource pressure causing the browser handle to become invalid before the async zoom query completes.

Related errors


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