cefsharp/CefSharp · critical · Exception

Cef.Shutdown has already been called, it's no longer possibl

Error message

Cef.Shutdown has already been called, it's no longer possible to execute on the CEF UI Thread. Check CefThread.HasShutdown to guard against this execption

What it means

Thrown by CefThread.ExecuteOnUiThread<TResult> when Cef.Shutdown has already run (HasShutdown is true). After shutdown the CEF UI thread and its TaskFactory are torn down (UiThreadTaskFactory is set to null and HasShutdown flipped), so no further work can be posted to it. The exception is raised synchronously inside the method under a lock so callers fail fast instead of deadlocking on a dead thread.

Source

Thrown at CefSharp/Internals/CefThread.cs:88

        /// <summary>
        /// returns true if Cef.Shutdown been called, otherwise false.
        /// </summary>
        public static bool HasShutdown { get; private set; }

        /// <summary>
        /// Execute the provided function on the CEF UI Thread
        /// </summary>
        /// <typeparam name="TResult">result</typeparam>
        /// <param name="function">function</param>
        /// <returns>Task{Result}</returns>
        public static Task<TResult> ExecuteOnUiThread<TResult>(Func<TResult> function)
        {
            lock (LockObj)
            {
                if (HasShutdown)
                {
                    throw new Exception("Cef.Shutdown has already been called, it's no longer possible to execute on the CEF UI Thread. Check CefThread.HasShutdown to guard against this execption");
                }

                var taskFactory = UiThreadTaskFactory;

                if (taskFactory == null)
                {
                    //We don't have a task factory yet, so we'll queue for execution.
                    return QueueForExcutionWhenUiThreadCreated(function);
                }

                return taskFactory.StartNew(function);
            }
        }

        /// <summary>
        /// Execute the provided action on the CEF UI Thread
        /// </summary>
        /// <param name="action">action</param>

View on GitHub (pinned to 16bc6e0711)

Solutions

  1. Cancel/await all outstanding async operations before calling Cef.Shutdown().
  2. Guard calls with if (!CefThread.HasShutdown) before invoking UI-thread-dependent APIs.
  3. Fix shutdown ordering: dispose all ChromiumWebBrowser instances and complete pending work first, then call Cef.Shutdown() exactly once at the very end.
  4. Ensure Cef.Shutdown is not called from a finalizer or an unexpected code path that races live work.

Example fix

// before
Cef.Shutdown();
var result = await browser.EvaluateScriptAsync("someScript()");

// after
var result = await browser.EvaluateScriptAsync("someScript()");
browser.Dispose();
Cef.Shutdown();
// or guard at call sites:
if (!CefThread.HasShutdown) { /* post work */ }
Defensive patterns

Strategy: validation

Validate before calling

if (!CefThread.HasShutdown)
{
    return CefThread.ExecuteOnUiThread(() => DoWork());
}
return Task.FromResult(default(TResult));

Type guard

static bool CanUseUiThread => !CefThread.HasShutdown;

Try / catch

try
{
    return await CefThread.ExecuteOnUiThread(() => DoWork());
}
catch (Exception ex) when (CefThread.HasShutdown)
{
    // Context already torn down; nothing to post. Log and return default.
    return default;
}

Prevention

When it happens

Trigger: Awaiting or invoking any CefSharp API that internally calls CefThread.ExecuteOnUiThread (e.g. certain async extension methods, EvaluateScriptAsync continuations, cookie/JS repository operations on the UI thread) after Cef.Shutdown has been called during app shutdown or browser disposal.

Common situations: Application shutdown ordering: Cef.Shutdown() runs while background tasks, timers, or disposed-browser callbacks still try to touch the UI thread; a second browser instance created after shutdown; deferred Task continuations that outlive the CEF lifetime; calling browser operations during Application.Exit or Form.Closed before Cef.Shutdown but racing the shutdown.

Related errors


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