{"record":{"id":"a7732aff06729cb6","repo":"dotnet/wpf","slug":"sr-cachereqestcantmodifywhileactive","errorCode":null,"errorMessage":"SR.CacheReqestCantModifyWhileActive","messagePattern":"SR\\.CacheReqestCantModifyWhileActive","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/CacheRequest.cs","lineNumber":472,"sourceCode":"        //\n        //  Private Methods\n        //\n        //------------------------------------------------------\n\n        #region Private Methods\n\n        // Ensure that this CacheRequest isn't currently in use\n        // Must be called within a lock(_instanceLock) to ensure\n        // thread consistency\n        private void CheckAccess()\n        {\n            // Make sure this isn't being used by any thread's\n            // CacheRequest stacks by using a refcount:\n            // (Also check for defaultCacheRequest explicitly, since it\n            // is never explicitly added to the stack)\n            if (_refCount != 0 || this == DefaultCacheRequest)\n            {\n                throw new InvalidOperationException(SR.CacheReqestCantModifyWhileActive);\n            }\n        }\n\n        // Called when state changes - sets _uiaCacheRequest to null\n        // to ensure that a clean one is generated next time Push is called\n        private void Invalidate()\n        {\n            _uiaCacheRequest = null;\n        }\n\n        #endregion Private Methods\n\n\n        //------------------------------------------------------\n        //\n        //  Private Fields\n        //\n        //------------------------------------------------------","sourceCodeStart":454,"sourceCodeEnd":490,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/CacheRequest.cs#L454-L490","documentation":"CheckAccess() throws InvalidOperationException when a CacheRequest is modified while it is active. A request is considered active if its refcount (_refCount) is non-zero (i.e., some thread has it pushed on its stack) or if it is the shared CacheRequest.DefaultCacheRequest, which is immutable by design.","triggerScenarios":"Setting TreeScope, TreeFilter, or AutomationElementMode (or calling Add for a property/pattern) while the request is currently pushed/activated on any thread; attempting to modify CacheRequest.DefaultCacheRequest at any time.","commonSituations":"Mutating a request inside a CacheRequest.Activate() scope; a long-lived request stored in a static field that other threads have pushed; code trying to add properties to DefaultCacheRequest to widen default caching; races where another thread still holds the request active.","solutions":["Create and configure a new CacheRequest instead of modifying the active one","Never mutate CacheRequest.DefaultCacheRequest; clone its settings into a new request","Move all configuration (Add/TreeScope/TreeFilter) before calling Push()/Activate()","Use the using(var act = request.Activate()) pattern and configure properties outside that block","Synchronize across threads so configuration happens only when no thread holds the request"],"exampleFix":"// before\nusing (req.Activate())\n{\n    req.Add(AutomationElement.NameProperty); // InvalidOperationException: active\n}\n// after\nreq.Add(AutomationElement.NameProperty);\nusing (req.Activate())\n{\n    var name = element.Cached.Name;\n}","handlingStrategy":"validation","validationCode":"if (request != CacheRequest.DefaultCacheRequest)\n{\n    request.TreeScope = ...; request.TreeFilter = ...; // configure before Activate\n}\nusing (request.Activate()) { /* consume cached values only */ }","typeGuard":null,"tryCatchPattern":"try { request.Add(prop); } catch (InvalidOperationException) { request = CloneConfigured(request); }","preventionTips":["Configure requests fully before Activate/Push","Treat DefaultCacheRequest as read-only","Do not mutate shared request instances from multiple threads","Keep mutation outside Activate() scopes"],"tags":["uiautomation","caching","immutability","invalid-operation","threading"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}