From 156f94e8403d7b58958d451e009f468de2c76793 Mon Sep 17 00:00:00 2001 From: sorrow404null Date: Thu, 17 Sep 2026 16:40:03 +0800 Subject: [PATCH] fix(webview2): fall back to byte[] assembly load and guard instance lock Assembly.LoadFrom can fail on volumes .NET misclassifies as remote; fall back to Assembly.Load(byte[]). Broaden exception handling to BaseException for pythonnet, and skip WebView2 warmup/taskkill when the instance lock is held by another process. --- core/webview2.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/core/webview2.py b/core/webview2.py index 25cf765..5742cc2 100644 --- a/core/webview2.py +++ b/core/webview2.py @@ -158,7 +158,18 @@ def get_environment(app): pass import clr global _System - clr.AddReference(CORE_DLL) + # 🐛 部分机器(本机 D: 卷).NET Framework 把工程所在本地卷误判为 + # “网络位置”,clr.AddReference(= Assembly.LoadFrom)报 0x80131515。 + # 快路径保持 LoadFrom(正常机器行为不变);失败回落 Assembly.Load(byte[]), + # 字节加载不做路径信任分类(本机实测:D: 字节加载 OK,类型解析 OK)。 + try: + clr.AddReference(CORE_DLL) + except BaseException as _ex: # pythonnet 的 .NET 异常未必是 Python Exception 子类 + _msg = str(_ex).splitlines()[0][:120] if str(_ex) else type(_ex).__name__ + print(f"[WV2] AddReference 路径加载失败({_msg})→ 回落 Assembly.Load(byte[])") + from System.Reflection import Assembly as _Assembly + with open(CORE_DLL, "rb") as _f: + _Assembly.Load(_f.read()) import System _System = System from Microsoft.Web.WebView2.Core import CoreWebView2Environment @@ -166,7 +177,7 @@ def get_environment(app): _env = _pump_wait(CoreWebView2Environment.CreateAsync(None, None, None), app) print(f"[WV2] {_ts()} Runtime ready: {_env.BrowserVersionString}") return _env - except Exception as ex: + except BaseException as ex: # pythonnet/.NET 异常未必是 Exception 子类,必须 BaseException _env = None print(f"[WV2] init failed → fallback to QtWebEngine: {ex}") return None