Skip to content

Unhandled exception from FileConfigurationProvider when re-loading a locked file #113964

Closed

Description

@znakeeye

Description

Verified in .NET 8. In our application, we have an intermittent crash where FileConfigurationProvider throws an IOException when reloading the configuration file. This is easily reproducible by locking the file immediately after saving it, commonly performed by Antivirus software.

E.g.: You will not be able to catch the exception using LoadException:

source.ReloadOnChange = true;
source.ReloadDelay = 10000; // Give us some time to lock the file from some other process...
source.OnLoadException = OnLoadException; // NOT called

Please have a look at FileConfigurationProvider.cs(96). The mistake is obvious. Opening of the stream should be inside the try block!

using Stream stream = OpenRead(file); // Outside try block! 
try
{
    Load(stream);
}

Exception details

System.AggregateException: A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread. (One or more errors occurred. (The process cannot access the file 'C:\Foo\settings.json' because it is being used by another process.))
 ---> System.AggregateException: One or more errors occurred. (The process cannot access the file 'C:\Foo\settings.json' because it is being used by another process.)
 
 ---> System.IO.IOException: The process cannot access the file 'C:\Foo\settings.json' because it is being used by another process.
 
   at Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options)
 
   at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
 
   at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
 
   at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
 
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, Int64 preallocationSize)
 
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
 
   at Microsoft.Extensions.Configuration.FileConfigurationProvider.<Load>g__OpenRead|6_0(IFileInfo fileInfo)
 
   at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload)
 
   at Microsoft.Extensions.Configuration.FileConfigurationProvider.<.ctor>b__1_1()
 
   at Microsoft.Extensions.Primitives.ChangeToken.<>c.<OnChange>b__0_0(Action callback)
 
   at Microsoft.Extensions.Primitives.ChangeToken.ChangeTokenRegistration`1.OnChangeTokenFired()
 
   at Microsoft.Extensions.Primitives.ChangeToken.ChangeTokenRegistration`1.<>c.<RegisterChangeTokenCallback>b__7_0(Object s)
 
   at System.Threading.CancellationTokenSource.Invoke(Delegate d, Object state, CancellationTokenSource source)
 
   at System.Threading.CancellationTokenSource.ExecuteCallbackHandlers(Boolean throwOnFirstException)
 
   --- End of inner exception stack trace ---
 
   at System.Threading.CancellationTokenSource.ExecuteCallbackHandlers(Boolean throwOnFirstException)
 
   at Microsoft.Extensions.FileProviders.Physical.PhysicalFilesWatcher.<>c.<.cctor>b__43_0(Object state)
 
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
 
--- End of stack trace from previous location ---
 
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
 
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
 
   --- End of inner exception stack trace ---

Reproduction Steps

Run attached sample project.

FileConfigurationProviderCrash.zip

Expected behavior

The attached sample project should not throw an IOException.

Actual behavior

IOException is unhandled even though we tell the FileConfigurationSource to ignore exceptions.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Activity

  1. ghost added
    needs-area-label
    on Mar 27, 2025
  2. added
    untriaged
    on Mar 27, 2025
  3. added and removed
    needs-area-label
    on Mar 27, 2025
  4. dotnet-policy-service commented on Mar 27, 2025

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-extensions-configuration
    See info in area-owners.md if you want to be subscribed.

  5. dotnet-policy-service commented on Mar 27, 2025

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-extensions-filesystem
    See info in area-owners.md if you want to be subscribed.

  6. KalleOlaviNiemitalo commented on Mar 27, 2025

    @KalleOlaviNiemitalo

    Although the Exception.Message claims "the unobserved exception was rethrown by the finalizer thread", .NET does not actually rethrow the exception; it just raises the TaskScheduler.UnobservedTaskException event. Because your sample application does not handle this event, I don't see how the exception could crash the application.

    .NET Framework 4.5 and greater behave likewise, unless configured. Exceptions during unobserved processing in System.Threading.Tasks.Task no longer propagate on finalizer thread

  7. znakeeye commented on Mar 28, 2025

    @znakeeye
    Author

    The original exception was caught in a WPF application where the UnobservedTaskException is indeed handled.

    The bug is pretty clear though. The intent of the OnLoadException is for the consumer to have a crack at the exception. Here, we see a case where an exception escapes this callback. I hope a fix will be considered for .NET 10.

  8. KalleOlaviNiemitalo commented on Mar 28, 2025

    @KalleOlaviNiemitalo

    Currently, if Load(Stream) throws, then Load(bool) catches the exception and wraps it in InvalidDataException. If instead OpenRead(IFileInfo) throws, then I don't think InvalidDataException is appropriate. So the OpenRead call shouldn't simply be moved into the same try statement.

    There is also a bug in that the InvalidDataException message always uses file.PhysicalPath even when that is null; should fall back to Source.Path when there is invalid data in a non-physical file.

  9. added this to the Future milestone on Jul 26, 2025
  10. removed
    untriaged
    on Jul 26, 2025
  11. cincuranet commented on Mar 12, 2026

    @cincuranet
    Contributor

    Triage: We will investigate.

  12. modified the milestones: Future, 11.0.0 on Jul 1, 2026
  13. added a commit that references this issue on Jul 27, 2026
    da60813
  14. locked and limited conversation to collaborators on Aug 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions