-
Notifications
You must be signed in to change notification settings - Fork 547
Prevent AssemblyX.OnAssemblyResolve recursion on satellite resource loads causing startup stack overflow
#182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,4 +1,7 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using NewLife; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using System.IO; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using System.Reflection; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using NewLife; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using NewLife.Configuration; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using NewLife.Reflection; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using Xunit; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -25,4 +28,30 @@ public void GetCompileTime() | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Assert.Equal("2022-04-27 03:44:00".ToDateTime(), time.ToUniversalTime()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [Fact] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public void OnAssemblyResolve_ResourceAssembly_DontInitSetting() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var currentField = typeof(Config<Setting>).GetField("_Current", BindingFlags.NonPublic | BindingFlags.Static); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Assert.NotNull(currentField); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var old = currentField.GetValue(null); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| currentField.SetValue(null, null); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var method = typeof(AssemblyX).GetMethod("OnAssemblyResolve", BindingFlags.NonPublic | BindingFlags.Static); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Assert.NotNull(method); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var args = new ResolveEventArgs("System.IO.FileSystem.Watcher.resources, Version=8.0.0.0, Culture=zh-CN, PublicKeyToken=b03f5f7f11d50a3a", typeof(FileSystemWatcher).Assembly); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var rs = method.Invoke(null, [null, args]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Assert.Null(rs); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Assert.Null(currentField.GetValue(null)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| finally | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| currentField.SetValue(null, old); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+39
to
+55
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try | |
| { | |
| currentField.SetValue(null, null); | |
| var method = typeof(AssemblyX).GetMethod("OnAssemblyResolve", BindingFlags.NonPublic | BindingFlags.Static); | |
| Assert.NotNull(method); | |
| var args = new ResolveEventArgs("System.IO.FileSystem.Watcher.resources, Version=8.0.0.0, Culture=zh-CN, PublicKeyToken=b03f5f7f11d50a3a", typeof(FileSystemWatcher).Assembly); | |
| var rs = method.Invoke(null, [null, args]); | |
| Assert.Null(rs); | |
| Assert.Null(currentField.GetValue(null)); | |
| } | |
| finally | |
| { | |
| currentField.SetValue(null, old); | |
| } | |
| var method = typeof(AssemblyX).GetMethod("OnAssemblyResolve", BindingFlags.NonPublic | BindingFlags.Static); | |
| Assert.NotNull(method); | |
| var args = new ResolveEventArgs("System.IO.FileSystem.Watcher.resources, Version=8.0.0.0, Culture=zh-CN, PublicKeyToken=b03f5f7f11d50a3a", typeof(FileSystemWatcher).Assembly); | |
| var rs = method.Invoke(null, [null, args]); | |
| Assert.Null(rs); | |
| Assert.Same(old, currentField.GetValue(null)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[ThreadStatic] _resolving的重入保护目前是“同线程只要在解析中就直接返回 null”。这会屏蔽掉解析过程中触发的二次AssemblyResolve(例如Assembly.LoadFrom加载 A 时,其依赖 B 触发的AssemblyResolve),可能导致原本可被解析的依赖无法加载,产生回归。建议将重入保护改为“按程序集名去重”(例如线程内记录正在解析的 name 集合/栈,仅当解析同一个 name 再次进入时短路),或仅包裹会触发配置/日志初始化的分支而非整个解析器。