{"record":{"id":"d9e1b0f01f2d9907","repo":"cefsharp/CefSharp","slug":"unable-to-add-messageid-0-to-queuedcommandresult","errorCode":null,"errorMessage":"Unable to add MessageId {0} to queuedCommandResults ConcurrentDictionary.","messagePattern":"Unable to add MessageId (.+?) to queuedCommandResults ConcurrentDictionary\\.","errorType":"exception","errorClass":"DevToolsClientException","httpStatus":null,"severity":"error","filePath":"CefSharp.Core/DevTools/DevToolsClient.cs","lineNumber":156,"sourceCode":"                throw new ObjectDisposedException(nameof(IBrowser));\n            }\n\n            var taskCompletionSource = new TaskCompletionSource<T>(TaskCreationOptions.RunContinuationsAsynchronously);\n\n            var methodResultContext = new DevToolsMethodResponseContext(\n                type: typeof(T),\n                setResult: o => taskCompletionSource.TrySetResult((T)o),\n                setException: taskCompletionSource.TrySetException,\n                syncContext: CaptureSyncContext ? SynchronizationContext.Current : SyncContext\n            );\n\n            var browserHost = browser.GetHost();\n\n            var messageId = browserHost.GetNextDevToolsMessageId();\n\n            if (!queuedCommandResults.TryAdd(messageId, methodResultContext))\n            {\n                throw new DevToolsClientException(string.Format(\"Unable to add MessageId {0} to queuedCommandResults ConcurrentDictionary.\", messageId));\n            }\n\n            //Currently on CEF UI Thread we can directly execute\n            if (CefThread.CurrentlyOnUiThread)\n            {\n                ExecuteDevToolsMethod(browserHost, messageId, method, parameters, methodResultContext);\n            }\n            //ExecuteDevToolsMethod can only be called on the CEF UI Thread\n            else if (CefThread.CanExecuteOnUiThread)\n            {\n                CefThread.ExecuteOnUiThread(() =>\n                {\n                    ExecuteDevToolsMethod(browserHost, messageId, method, parameters, methodResultContext);\n                });\n            }\n            else\n            {\n                queuedCommandResults.TryRemove(messageId, out methodResultContext);","sourceCodeStart":138,"sourceCodeEnd":174,"githubUrl":"https://github.com/cefsharp/CefSharp/blob/16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec/CefSharp.Core/DevTools/DevToolsClient.cs#L138-L174","documentation":"Thrown by DevToolsClient when ConcurrentDictionary.TryAdd for a freshly allocated messageId returns false. Message ids come from IBrowserHost.GetNextDevToolsMessageId() which is a monotonic native counter, so a collision is essentially impossible in correct operation. The exception exists as a defensive assertion: if it fires, something has corrupted the id generator or the same client is being used concurrently in an unexpected way.","triggerScenarios":"An integer overflow/wrap of the native message id counter; reuse of one DevToolsClient across browsers in a way that shares the counter incorrectly; a build of CEF/Chromium returning duplicate ids due to a native bug. In normal single-browser usage this is effectively unreachable.","commonSituations":"Should not occur in supported usage. If it does, it points to a CEF native regression or a threading bug where the same id space is shared. Almost always a sign to file a CefSharp issue rather than an application bug.","solutions":["Confirm you are not wrapping/reusing the same DevToolsClient across multiple concurrently-disposed browsers; obtain a per-browser client via GetDevToolsClient().","Check the CEF log file for native errors; update to the latest CefSharp/CEF patch release in case the id counter bug was fixed.","If reproducible, capture a minimal repro and report it to the CefSharp issue tracker - this guard indicates an internal invariant violation.","As a workaround, retry the DevTools call on a fresh client after disposing and recreating the browser."],"exampleFix":"// No caller-side fix; the id is allocated internally. Retry pattern only mitigates:\nasync Task<T> InvokeStable<T>(IBrowser browser, string method, IDictionary<string,object> p) where T : DevToolsDomainResponseBase\n{\n    for (int i = 0; i < 3; i++)\n    {\n        try { return await browser.GetDevToolsClient().ExecuteDevToolsMethodAsync<T>(method, p); }\n        catch (DevToolsClientException) when (i < 2) { await Task.Delay(50); }\n    }\n    throw;\n}","handlingStrategy":"retry","validationCode":"// No caller-side precondition; the id is allocated internally. Best validation is a fresh per-browser client.\nvar client = browser.GetDevToolsClient();","typeGuard":null,"tryCatchPattern":"try { return await client.ExecuteDevToolsMethodAsync<T>(method, p); }\ncatch (DevToolsClientException ex) when (ex.Message.Contains(\"queuedCommandResults\"))\n{ /* retry on a fresh client or surface as internal CefSharp bug */ throw; }","preventionTips":["Use one DevToolsClient per browser, do not share across browsers","Keep CefSharp package versions aligned so the native id allocator matches the managed wrapper","Report reproducible occurrences to CefSharp as an internal-invariant bug"],"tags":["csharp","cefsharp","devtools","internal-invariant","concurrency"],"backgroundTag":null,"analyzedSha":"16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec","analyzedAt":"2026-08-13T19:57:05.828Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}