Error loading mscorwks.dll that may appear while trying to install a .NET Framework hotfix

I was working with a couple of different customers today who saw a .NET Framework initialization error dialog that says that setup failed to load mscorwks.dll appear when trying to install ASP.NET hotfix KB886903. The exact error dialog looks like this.

It turns out this is being caused by a race condition that can happen when trying to install one .NET Framework or ASP.NET hotfix immediately after installing another .NET Framework or ASP.NET hotfix. There is special logic in the .NET Framework hotfix and service pack setup wrapper that is used to rerun ngen.exe to regenerate native images for some of the .NET Framework binaries. This process is done when installing the .NET Framework, but the native images are invalidated by any changes made to them (such as when they are patched by a service pack). The process that reruns ngen.exe is separate from the original hotfix installation and in some cases it is scheduled to occur after the next reboot (to account for files in use and things like that).

In both of the scenarios I saw, the customers had installed .NET Framework 1.1 SP1 via Windows Update and then were required to reboot. After the reboot, the background process was launched by an entry in the RunOnce registry hive and at approximately the same time the setup for the ASP.NET hotfix was launched by Windows Update or Automatic Update. In some cases, these two processes try to access some of the same resources at the same time and it ends up resulting in an error message.

The end result of this error message is that one or more of the native images fails to be recreated. This does not cause the .NET Framework to fail in any way, but since ngen.exe is designed to improve performance for the binaries in question, it can lead to performance degradation for the .NET Framework.

If you hit this error and want to make sure that the files will have correct native images generated for them by ngen.exe you can reapply the service pack to cause these commands to be rerun.