Skip to content

Commit 21b8b60

Browse files
committed
Optimize edge case where the assembly is downloaded but not loaded into context
1 parent 818ad10 commit 21b8b60

File tree

4 files changed

+54
-5
lines changed

4 files changed

+54
-5
lines changed

src/src/AssemblyLoader.Server/Services/DisposableAssemblyLoadContext.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,25 @@ public DisposableAssemblyLoadContext(
8282
return null;
8383
}
8484

85+
public Assembly? Load(AssemblyName assemblyName)
86+
{
87+
try
88+
{
89+
lock (_assemblyLoadContextLock)
90+
{
91+
if (_assemblyLoadContext == null)
92+
{
93+
return null;
94+
}
95+
96+
return _assemblyLoadContext.LoadFromAssemblyName(assemblyName);
97+
}
98+
}
99+
catch { }
100+
101+
return null;
102+
}
103+
85104
[MethodImpl(MethodImplOptions.NoInlining)]
86105
public void Dispose()
87106
{

src/src/AssemblyLoader.Wasm/Services/AppDomainAssemblyLoadContext.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,25 @@ public void Dispose()
102102
return null;
103103
}
104104
}
105+
106+
public Assembly? Load(AssemblyName assemblyName)
107+
{
108+
try
109+
{
110+
lock (_domainLock)
111+
{
112+
if (_domain == null)
113+
{
114+
return null;
115+
}
116+
117+
return _domain.Load(assemblyName);
118+
}
119+
}
120+
catch
121+
{
122+
return null;
123+
}
124+
}
105125
}
106126
}

src/src/AssemblyLoader/Abstractions/IAssemblyLoadContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@ public interface IAssemblyLoadContext : IDisposable
1212
ICollection<Assembly> OwnAssemblies { get; }
1313

1414
Assembly? Load(AssemblyData assemblyData);
15+
16+
Assembly? Load(AssemblyName assemblyName);
1517
}
1618
}

src/src/AssemblyLoader/Services/AssemblyLoader.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,19 @@ private bool TryGetAlreadyLoadedAssembly(
182182
IAssemblyComparer comparer,
183183
AssemblyLoaderContext context)
184184
{
185+
if (_assemblyLoadContext == null)
186+
{
187+
return null;
188+
}
189+
190+
// Try loading the assembly by name (this works when the assembly is part of the bootloader, but never used explicitly)
191+
Assembly? assembly = _assemblyLoadContext.Load(assemblyName);
192+
193+
if (assembly != null)
194+
{
195+
return assembly;
196+
}
197+
185198
AssemblyData? data = await _assemblyDataProvider.GetAssemblyDataAsync(assemblyName, context).ConfigureAwait(false);
186199

187200
if (data == null)
@@ -197,11 +210,6 @@ private bool TryGetAlreadyLoadedAssembly(
197210
return null;
198211
}
199212

200-
if (_assemblyLoadContext == null)
201-
{
202-
return null;
203-
}
204-
205213
return _assemblyLoadContext.Load(data);
206214
}
207215

0 commit comments

Comments
 (0)