cefsharp/CefSharp · error · ObjectDisposedException
CookieManager
Error message
CookieManager
What it means
DeleteCookiesAsync is an extension method on ICookieManager. If the manager's IsDisposed property is true — meaning the cookie manager (and typically its owning browser or context) has already been torn down — the call throws ObjectDisposedException before attempting the delete.
Source
Thrown at CefSharp/AsyncExtensions.cs:34
/// <summary>
/// Deletes all cookies that matches all the provided parameters asynchronously.
/// If both <paramref name="url"/> and <paramref name="name"/> are empty, all cookies will be deleted.
/// </summary>
/// <param name="cookieManager">cookie manager</param>
/// <param name="url">The cookie URL. If an empty string is provided, any URL will be matched.</param>
/// <param name="name">The name of the cookie. If an empty string is provided, any URL will be matched.</param>
/// <returns>Returns -1 if a non-empty invalid URL is specified, or if cookies cannot be accessed;
/// otherwise, a task that represents the delete operation. The value of the TResult will be the number of cookies that were deleted or -1 if unknown.</returns>
public static Task<int> DeleteCookiesAsync(this ICookieManager cookieManager, string url = null, string name = null)
{
if (cookieManager == null)
{
throw new ArgumentNullException(nameof(cookieManager));
}
if (cookieManager.IsDisposed)
{
throw new ObjectDisposedException("CookieManager");
}
var callback = new TaskDeleteCookiesCallback();
if (cookieManager.DeleteCookies(url, name, callback))
{
return callback.Task;
}
//There was a problem deleting cookies
return Task.FromResult(TaskDeleteCookiesCallback.InvalidNoOfCookiesDeleted);
}
/// <summary>
/// Sets a cookie given a valid URL and explicit user-provided cookie attributes.
/// This function expects each attribute to be well-formed. It will check for disallowed
/// characters (e.g. the ';' character is disallowed within the cookie value attribute) and will return false without setting
/// </summary>
/// <param name="cookieManager">cookie manager</param>View on GitHub (pinned to 16bc6e0711)
Solutions
- Check cookieManager.IsDisposed before calling DeleteCookiesAsync.
- Cancel in-flight cookie operations when the browser is closing (use a CancellationToken or a completion gate).
- Obtain a fresh ICookieManager reference from the live browser/RequestContext rather than caching it long-term.
Example fix
// before — manager may be disposed
await cookieManager.DeleteCookiesAsync("https://example.com", null);
// after — guard before call
if (cookieManager == null || cookieManager.IsDisposed)
return;
await cookieManager.DeleteCookiesAsync("https://example.com", null); Defensive patterns
Strategy: validation
Validate before calling
if (cookieManager == null || cookieManager.IsDisposed)
{
return Task.FromResult(TaskDeleteCookiesCallback.InvalidNoOfCookiesDeleted);
}
return cookieManager.DeleteCookiesAsync(url, name); Try / catch
try
{
await cookieManager.DeleteCookiesAsync(url, name);
}
catch (ObjectDisposedException)
{
Log.Info("CookieManager disposed — skipping delete");
} Prevention
- Check cookieManager.IsDisposed before every cookie operation.
- Do not cache ICookieManager references beyond the browser's lifetime.
- Cancel in-flight cookie tasks when the browser closes.
When it happens
Trigger: Calling DeleteCookiesAsync on a cookie manager obtained from a disposed browser, a disposed RequestContext, or the global manager after Cef.Shutdown.
Common situations: Awaited async cookie operations completing after the browser window was closed. Using a cached ICookieManager reference that outlived its browser. Cleanup ordering: browser disposed while cookie tasks are still in-flight.
Related errors
- IBrowser
- Browser has been disposed
- frame
- Expected Exception
- The ChromiumWebBrowser instance creates the underlying Chrom
AI-assisted analysis of cefsharp/CefSharp@16bc6e0711 (2026-08-13).
Data as JSON: /api/errors/b3c6258767eb5d74.
Report an issue: GitHub.