Fix corrupted comtypes.gen cache from concurrent generation - #358
Conversation
…uch#357) Wrap comtypes.client.GetModule in a cross-process file lock and add automatic cache clearing + regeneration when a corrupted cache is detected (ImportError/AttributeError on import, or a generated module missing an expected attribute). Fixes startup crashes when the host (e.g. Claude Desktop) spawns multiple server instances concurrently and both generate the comtypes.gen cache at the same time. Tested: clean generation, corrupted-cache recovery, and 4 concurrent processes generating on an empty cache.
PR Summary by QodoPrevent comtypes.gen corruption with cross-process locking and self-healing regeneration
AI Description
Diagram
High-Level Assessment
Files changed (3)
|
Code Review by Qodo
Context used✅ Compliance rules (platform):
16 rules 1.
|
…t, guarded cache clear
|
Thanks |
Fixes #357
Problem
When the MCP host (e.g. Claude Desktop) spawns multiple server instances near-simultaneously, both processes generate the comtypes cache in
comtypes/genconcurrently on first run. Concurrent writes leave the GUID module and the friendly wrapper (UIAutomationClient.py) mismatched or partial. Every subsequent startup then crashes withImportError/AttributeErroruntil the cache is manually deleted. Recurs after every venv rebuild.Fix
New module
windows_mcp/uia/comtypes_cache.pyprovidingsafe_get_module():sys.prefix) so only one process generates the cache at a time.ImportError/AttributeError(or when the generated module lacks an expected attribute) the generated files are cleared,sys.modulesentries purged, and generation retried once.Used at both generation sites:
uia/core.py(UIAutomationCore.dll) andtree/ia2.py(oleacc.dll). This also self-heals installs that already have a corrupted cache — no manual venv deletion needed.Testing