DllNotFoundException after .NET 5 in-place update
Description
If Microsoft Update (MU) installs an in-place update of .NET 5 framework (no major/minor feature update) while framework dependent applications are running, then these applications may crash after a time because of missing dlls (FileNotFoundException/DllNotFoundException).
The in-place update installs the new version of the .NET framework and then it tries to delete the old version. If dlls of the old version have been loaded, they can't be deleted and so they were moved to a temporary hidden directory that will be deleted after restart. Moved dlls stay loaded in running applications, but can't be loaded by name/path anymore. And that's exactly the problem. If one of these applications need to load an additional framework dll, the dotnet loader looks for this dll in the framework directory of the old version. This directory doesn't exist and so the application crashes because of DllNotFound or FileNotFound exception
Steps to reproduce
- Create a .NET 5 WPF application with a button. In the click handler call a framework method from an assembly that isn't already loaded (e.g. MessageBox.Show(typeof(System.Printing.PrintPort).Name); )
- Start this application on a system with installed .NET framework version 5.0.7
- Activate Microsoft Update, search for updates and wait until version 5.0.10 is installed
- Click the button of the WPF application
- Crash
Application: WpfApp1.exe CoreCLR Version: 5.0.721.25508 .NET Version: 5.0.7 Description: The process was terminated due to an unhandled exception. Exception Info: System.IO.FileNotFoundException: Could not load file or assembly 'System.Printing, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
With Procexp you can see the movement of loaded dlls.
Before .NET framework update

after .NET framework update

With Procmon you can see where the loader is searching for framework dlls after .NET framework update:

But this directory doesn't exist anymore.
You can reproduce this problem even easier. After an update of the .NET framework you only need to close the WPF application (no button click or custom code necessary). The application will crash again, but now you doesn't recognize it directly. You have to look into event log:
Application: WpfApp1.exe
CoreCLR Version: 5.0.721.25508
.NET Version: 5.0.7
Description: The process was terminated due to an unhandled exception.
Exception Info: System.DllNotFoundException: Unable to load DLL 'vcruntime140_cor3.dll' or one of its dependencies: Das angegebene Modul wurde nicht gefunden. (0x8007007E)
at __std_type_info_destroy_list(__type_info_node* )
at __scrt_uninitialize_type_info()
at _app_exit_callback()
at
Because Window Update is running behind the scenes, and the described problems occur much later, it’s not easy to comprehend why the applications are crashing. .NET framework will be updated monthly and so there is a high risk that these types of problems appear.
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.
@NikolaMilosavljevic any idea where this issue should go? WPF?
@NikolaMilosavljevic any idea where this issue should go? WPF?
This can stay in runtime as it is something that can be experienced with shared and desktop runtimes.
OK picked area-Setup.
@danmoseley Is there an actual time table, when we can expect some feedback on this? We are in the middle of the release of our biggest update in years (migration from 4.7.2 to 5.0) and as the first small wave of customers got the update, this issue appeared and it seems huge.
❗ @NikolaMilosavljevic @vitek-karas can you help find someone to localize this one? It is blocking @rkieslinger update.
@joeloff for the installer side of things. I don't think there's much the runtime can do here. If there's a way for the setup to leave the old version in place and only remove it upon next restart - that would probably be the best solution. But I don't know what is the contract with security servicing.
If I remember correctly, the .NET Runtime update actually did install side by side, but the updates of the .NET Desktop Runtime and ASP.NET Core Runtime did not and so they are the ones that are causing issues.
@TRZLGast can you confirm that, just to be sure?
I don't see much we can do from the setup side either. Upgrades uninstall. There's no way to defer a part of the upgrade until the next restart. Right now we install the new version and remove the old. That would break 99% of the cases where leveraging MU is removing the previously vulnerable pieces from the machine.
We introduced upgrades around 3.0 across the board. There are two models being used.
- The shared host upgrades the actual MSI itself
- For the standalone installs, runtimes + SDK, the new version of any MSI is laid down first before removing the old one.
This would be less of an issue for .NET Framework because assemblies would end up in the GAC I think
So to sum it up, we just have these 2 options?
- We tell our 20k+ customers, they gotta get used to a random monthly crash and that this is by design?
- We disable .net updates via microsoft update on our customers computers (registry key
BlockMU). Seems problematic, if there's other software on the computer using .net which is relying on them.
Doesn't MSI sometimes utilize PendingFileRenameOperations to delete files on the next reboot? Wouldn't that be a better solution, instead of snatching runtime files away from running applications?
Another approach which gets to my mind could be an entry in RunOnce to uninstall the old version safely at the next reboot?
If I remember correctly, the .NET Runtime update actually did install side by side, but the updates of the .NET Desktop Runtime and ASP.NET Core Runtime did not and so they are the ones that are causing issues.
@TRZLGast can you confirm that, just to be sure?
Yes that's right.
before update:

after update (and restart):

How did you acquire these? The runtime installers will always upgrade, but SDK feature bands are SxS, so if you installed SDK 5.0.100 and then 5.0.101, that will upgrade the runtime from 5.0.0 to 5.0.1.
However, you could then install SDK 5.0.205. the 2xx and 1xx bands are SxS, so that would allow you to have both the 5.0.1 and 5.0.8 runtimes.
If you installed just the runtime, e.g. dotnet-runtime-5.0.7-win-x64.exe and then dotnet-runtime-5.0.8-win-x64.exe, that will upgrade.
But if you installed dotnet-runtime-5.0.7-win-x64.exe and then windowsdesktop-runtime-5.0.7-win-x64.exe followed by dotnet-runtime-5.0.8-win-x64.exe, you'll have the 5.0.7 and 5.0.8 runtimes plus the 5.0.7 desktop runtime. That's because the installers are ref counted against each of the separate bundles (EXEs).
Adding @dleeapho since this is mostly about runtime installers.
+1 for this
Not sure, but seems it's worse for run dotnet.exe (instead of the output .exe) refs Microsoft.AspNetCore.App as windows services or website(aspnetcore+IIS).
Seems during upgrade, the installer will kill all dotnet.exe first before installer new one. But for IIS/services in windows, it will restart immediately(So it lost all already loaded dlls and will try to load them latter), but at that time the new framework is not installed, so it still use the old framework while installer begin to remove file of old framework, and random get some FileNotFoundException(maybe catched by framework, so it still running instead of restart again, but the services is broken)
For upgrade dotnet-hosting OPT_NO_X86 and capture with procmon:
- install dotnet-runtime-6.0.1
- killall
dotnet.exe//restart with dotnet6.0.1+aspnetcore6.0.0 - uninstall dotnet-runtime-6.0.0
- install aspnetcore-runtime-6.0.1
- uninstall aspnetcore-runtime-6.0.1
Seems killall dotnet.exe step should be do again at last or just moved to last step, to ensure restarted programs will not do a race between load dlls from framework and remove old frameworks, and only successful to load some of them.
I've also been seeing this randomly on an app (.NET5 Desktop), it's pretty annoying. I was suspecting that it had to do with .NET update, now I have confirmation.
Keeping the old .NET runtime seems like the best thing to do. I personnally have many runtimes installed in that folder:

So I'm not sure why it has to be different with Microsoft Update. We don't want it to grow indefinitely?
@rkieslinger I guess another option would be to deploy a self-contained app to your customers.
@rkieslinger I guess another option would be to deploy a self-contained app to your customers.
We evaluated that before migrating to .NET 5.0+, but decided to roll with a shared framework for couple reasons. We'll stick to that decision and implemented some error management so the user at least gets an info that he has to restart his computer.
As it happens only on .dll not loaded yet (in our case it often happens on automation DLL for instance) I was also thinking to preload them as startup.
New runtime versions were just released:

And we saw similar expections popping and it's clearly not a coincidence.
I was thinking of a new approach by using AssemblyResolve callback. If the requested .dll is not available why not trying to load it from the new version installed? As it's a patch, it's probably safe and assembly versions will match.
Something like:
public App()
{
var runtimeDirectory = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory().TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
AppDomain.CurrentDomain.AssemblyResolve += (_, args) =>
{
// Runtime directory is not available anymore, probably deleted by an auto-update, hopefully a new patch should be available
if (!Directory.Exists(runtimeDirectory) && Version.TryParse(Path.GetFileName(runtimeDirectory), out var runtimeVersion))
{
var basePath = Path.GetDirectoryName(runtimeDirectory)!;
// Look for the next runtime patch and load the .dll from there
for (int i = 1; i < 10; i++)
{
var newVersion = new Version(runtimeVersion.Major, runtimeVersion.Minor, runtimeVersion.Build + i);
var newAssemblyPath = Path.Combine(basePath, newVersion.ToString(), new AssemblyName(args.Name).Name + ".dll");
if (File.Exists(newAssemblyPath))
{
return Assembly.LoadFrom(newAssemblyPath);
}
}
}
return null;
};
}
NB: I'm not sure how Microsoft Updates manage to remove the old folder, if I try to do it manually, explorer will prevent me from doing so as it's being in use.