Skip to content

FileNotFoundException resource files is slowing things down ridiculously #36053

Closed

Description

@dgxhubbard

In .net core 3.1, on wpf we are getting a ridiculous number of missing resource file FileNotFoundException. I have read this is a part of normal processing. If the resource file
cannot be found and it will be searched for somewhere else why throw an exception which
has a huge processing time, why not return a result that says not found. We are getting these
exceptions constantly yet we still see our resource strings. Below is an example. The worst part is the same missing resource file FileNotFoundException get thrown multiple. The delays in program startup are very noticeable. If there is something that can be done to fix this I would appreciate it.

Exception thrown: 'System.IO.FileNotFoundException' in System.Private.CoreLib.dll
Failed to resolve C:\Repository\GtNext\bin\Debug\System.Configuration.ConfigurationManager.resources.dll

Exception thrown: 'System.IO.FileNotFoundException' in System.Private.CoreLib.dll
Failed to resolve C:\Repository\GtNext\bin\Debug\System.Configuration.ConfigurationManager.resources.dll

Exception thrown: 'System.IO.FileNotFoundException' in System.Private.CoreLib.dll
Failed to resolve C:\Repository\GtNext\bin\Debug\System.Data.SqlClient.resources.dll

Exception thrown: 'System.IO.FileNotFoundException' in System.Private.CoreLib.dll
Failed to resolve C:\Repository\GtNext\bin\Debug\System.Private.DataContractSerialization.resources.dll
Exception thrown: 'System.IO.FileNotFoundException' in System.Private.CoreLib.dll
Failed to resolve C:\Repository\GtNext\bin\Debug\System.Private.DataContractSerialization.resources.dll

Exception thrown: 'System.IO.FileNotFoundException' in System.Private.CoreLib.dll
Failed to resolve C:\Repository\GtNext\bin\Debug\System.ComponentModel.Composition.resources.dll
Exception thrown: 'System.IO.FileNotFoundException' in System.Private.CoreLib.dll
Failed to resolve C:\Repository\GtNext\bin\Debug\System.ComponentModel.Composition.resources.dll

Exception thrown: 'System.IO.FileNotFoundException' in System.Private.CoreLib.dll
Failed to resolve C:\Repository\GtNext\bin\Debug\System.Data.SqlClient.resources.dll
Exception thrown: 'System.IO.FileNotFoundException' in System.Private.CoreLib.dll
Failed to resolve C:\Repository\GtNext\bin\Debug\System.Data.SqlClient.resources.dll

Activity

  1. added
    untriaged
    on May 7, 2020
  2. Dotnet-GitSync-Bot commented on May 7, 2020

    @Dotnet-GitSync-Bot
    Collaborator

    I couldn't figure out the best area label to add to this issue. Please help me learn by adding exactly one area label.

  3. danmoseley commented on May 7, 2020

    @danmoseley
    Contributor

    @dgxhubbard can you please provide the callstack for the exception? Attach a debugger, if it's Visual Studio please swithc of "just my code" and load symbols.

  4. tarekgh commented on May 8, 2020

    @tarekgh
    Member

    @dgxhubbard do you have a small sample to reproduce the issue so we can take a look? also, the stack trace will help too.

  5. tarekgh commented on May 8, 2020

    @tarekgh
    Member
  6. added
    tenet-performance
    enhancement
    and removed
    untriaged
    on May 8, 2020
  7. added this to the 5.0 milestone on May 8, 2020
  8. danmoseley commented on May 8, 2020

    @danmoseley
    Contributor

    I am not sure where "Failed to resolve" comes from. I cannot find it in runtime/wpf/corefx/coreclr.

  9. tarekgh commented on May 8, 2020

    @tarekgh
    Member

    I think this properly a wpf issue but wanted to get repro first to confirm. @danmosemsft does it repro with any simple wpf app?

  10. dgxhubbard commented on May 8, 2020

    @dgxhubbard
    Author

    I have the debugger running. I will stop on the exception and get you a stack trace

  11. dgxhubbard commented on May 8, 2020

    @dgxhubbard
    Author

    We are running 3.1.3 and when we load our assemblies for MEF using code below, and with "Just My Code" disabled we get a FileNotFound exception even though the path and assembly name are correct. At that point we added an assembly resolver to try and clear up the problem. This is where the errors come from. We get System.Private.DataContractSerialization.resources, Version=4.1.5.0, Culture=en-US, PublicKeyToken=b03f5f7f11d50a3a as the argument name and then add "dll" to it to try and resolve. Which causes an exception when LoadFrom is called. If I turn on "Enable Just My Code" and disable the assembly resolver then all problems with loading our assemblies for MEF using code below disappear. I had forgotten about the problem with creating the assembly catalog.

    Stack Trace

    at System.Runtime.Loader.AssemblyLoadContext.LoadFromPath(IntPtr ptrNativeAssemblyLoadContext, String ilPath, String niPath, ObjectHandleOnStack retAssembly)

    Assembly Resolver:

        public BootstrapperBase ()
        {
            AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
        }
    
        private Assembly CurrentDomain_AssemblyResolve ( object sender, ResolveEventArgs args )
        {
            Assembly assembly = null;
    
            var name = args.Name;
    
            var pos = name.IndexOf ( "," );
            if ( pos != -1 )
            {
                name = name.Substring ( 0, pos );
            }
    
            name += ".dll";
    
            var fullExeNameAndPath = Assembly.GetEntryAssembly ().Location;
            var path = Path.GetDirectoryName ( fullExeNameAndPath );
    
            var filepath = Path.Combine ( path, name );
    
            try
            {
                assembly = Assembly.LoadFrom ( filepath );
            }
            catch ( Exception ex )
            {
                Debug.WriteLine ( "Failed to resolve " + filepath, ex );
                LogProvider.Logger.LogException ( "Failed to resolve " + filepath, ex );
            }
    
            return assembly;
        }
    

    MEF Code to create assembly catalog:

    `public static AssemblyCatalog GetCatalog( string assemblyName )
    {
    AssemblyCatalog assemblyCatalog = null;

            var assembly = System.Reflection.Assembly.GetExecutingAssembly();
            var codebase = assembly.GetName().CodeBase;
    
            var path = codebase.Replace( @"file:///", string.Empty );
            var basePath = System.IO.Path.GetDirectoryName( path );
            var assemblyPath = Path.Combine( basePath, assemblyName );
    
            try
            {
                assemblyCatalog = new AssemblyCatalog( assemblyPath );
            }
            catch ( Exception ex )
            {
                LogProvider.Logger.LogException( "Failed to load assembly catalog " + assemblyPath, ex );
            }
    
            return assemblyCatalog;
        }`
    
  12. 33 remaining items

  13. steveharter commented on Aug 10, 2022

    @steveharter
    Contributor

    So the fix here is to add an exists check here (and we might be able to delete that catch FNF 😆)

    That seems like a reasonable approach however it is somewhat risky to do at this point in the release cycle. It should be done earlier.

    Also adding a File.Exists may have to take into consideration embedded assemblies, as exists with browser\wasm scenarios.

  14. modified the milestones: 7.0.0, 8.0.0 on Aug 10, 2022
  15. reijerh commented on Aug 11, 2022

    @reijerh

    And even if you check for existence, you still need to catch the FNF exception because the file might not be there anymore.

  16. modified the milestones: 8.0.0, 9.0.0 on Aug 9, 2023
  17. ericstj commented on Jan 2, 2024

    @ericstj
    Member

    For others landing on this issue - the most common cause of this problem is an AssemblyResolve handler which doesn't do an exists check before doing a loadfrom - those get invoked as part of the resource assembly resolution process (which itself tries to avoid raising exceptions).

    This issue tracks fixing this for Assembly.LoadFromResolveHandler. I see @tarekgh had some similar feedback in a related fix dotnet/coreclr#12329. @reijerh shared a great RCA in #36053 (comment). @jkotas what do you think about adding an exists check to LoadFromResolveHandler to avoid these first-chance exceptions?

  18. jkotas commented on Jan 2, 2024

    @jkotas
    Member

    @jkotas what do you think about adding an exists check to LoadFromResolveHandler to avoid these first-chance exceptions?

    Sounds good to me.

  19. ghost added
    in-pr
    on Jan 3, 2024
  20. ghost removed
    in-pr
    on Jan 4, 2024
  21. fowl2 commented on Jan 4, 2024

    @fowl2

    exists check before doing a loadfrom

    Exists() before Open() is an anti-pattern - it leads to TOCTOU races and wastes syscalls.

    This should block on #27217

  22. locked and limited conversation to collaborators on Feb 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions