Repository navigation
Closed
Closed
Copy link
Description
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 calledPlease 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
dotnet-policy-service commentedon Mar 27, 2025
Tagging subscribers to this area: @dotnet/area-extensions-configuration
See info in area-owners.md if you want to be subscribed.
dotnet-policy-service commentedon Mar 27, 2025
Tagging subscribers to this area: @dotnet/area-extensions-filesystem
See info in area-owners.md if you want to be subscribed.
KalleOlaviNiemitalo commentedon Mar 27, 2025
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
znakeeye commentedon Mar 28, 2025
The original exception was caught in a
WPFapplication where theUnobservedTaskExceptionis indeed handled.The bug is pretty clear though. The intent of the
OnLoadExceptionis 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.KalleOlaviNiemitalo commentedon Mar 28, 2025
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
trystatement.There is also a bug in that the InvalidDataException message always uses
file.PhysicalPatheven when that is null; should fall back toSource.Pathwhen there is invalid data in a non-physical file.cincuranet commentedon Mar 12, 2026
Triage: We will investigate.
FileConfigurationProvider: Handle and forward IO exceptions to OnLoad…