FileConfigurationProvider: Handle and forward IO exceptions to OnLoadException#126093
FileConfigurationProvider: Handle and forward IO exceptions to OnLoadException#126093mrek-msft wants to merge 35 commits intodotnet:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Unifies FileConfigurationProvider failure handling so IO failures during file open are handled the same way as parsing failures with respect to OnLoadException (including on reload-on-change), preventing unobserved/unhandled exceptions like the one reported in #113964.
Changes:
- Moves stream opening (
OpenRead) inside the exception-handling region so IO errors are routed throughHandleException/OnLoadException. - Adds an inner
try/catchto wrap exceptions thrown byLoad(stream)in anInvalidDataExceptionwith the standard “Failed to load…” message. - Adjusts the outer catch to forward the exception through
HandleException(enabling ignore behavior on reload).
|
Tagging subscribers to this area: @dotnet/area-extensions-filesystem |
… forwarding to OnLoadException Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/42d4769b-0756-4ab1-aaae-f73cf4cfaa3d Co-authored-by: mrek-msft <188900745+mrek-msft@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
| } | ||
| catch (Exception ex) | ||
| { | ||
| if (reload) |
There was a problem hiding this comment.
There is a small behavior change here. Before when the first load fail (reload is false), the OnReload was called. Now it is not. This is fine change with me but would be good if we document it somehow.
| catch (Exception ex) | ||
| { | ||
| // IO error on file open, preserve existing Data | ||
| HandleException(ExceptionDispatchInfo.Capture(ex)); |
There was a problem hiding this comment.
Consumers who registered OnLoadException callbacks expecting only FileNotFoundException or InvalidDataException will now receive:
- IOException (file locked, disk error)
- UnauthorizedAccessException (permission denied)
- Any exception from a custom IFileInfo.CreateReadStream()
Maybe it is worth documenting it as a breaking change?
tarekgh
left a comment
There was a problem hiding this comment.
Left minor comments I hope you address them. LGTM, otherwise.
Current behavior of FileConfigurationProvider on various types of failures at various times:
OnLoadExceptioncallback is calledAddJsonFilethrowsOnLoadExceptioncallback is calledprogram continues
no reload happen
AddJsonFilethrowsprogram continues
no reload happen
possibly raises
UnobservedTaskExceptionlaterThis PR unifies it in a way that both types of failure behave same as data parsing failure.
This brings some behavioral changes
OnLoadExceptioncallback is newly triggered if IO error happen on both types of events (first load, reload)UnobservedTaskExceptioncan no longer be observed in scenario when user registersOnLoadExceptioncallback. It can however happen when no callback is registered, so XML comment onOnLoadExceptionwas updated.OnLoadExceptioncallbacks now receives not only InvalidDataException, but also IOException (or any other exception which (possibly custom) implementation of IFileProvider can throw). If user cast to InvalidDataException, he may miss other exceptions or get cast exceptions.Based on follow up Copilot code reviews, I did few more behavior changes at edge cases, they do not relate directly to issue and can be reverted independently if not desired.
OnReload()now fires only when Data actually changed. Previously on first load, if a parse or IO error occurred andOnLoadExceptionsetIgnore = true,OnReload()was fired unconditionally even though Data was never modified. The new code firesOnReload()only when Data was successfully loaded or explicitly cleared (e.g., on reload or optional missing file). For consecutive reloads with parse errors, Data is cleared andOnReload()still fires. This change is independent on original fix and can be reverted independently if not desirable.Fix #113964