runtime icon indicating copy to clipboard operation
runtime copied to clipboard

.Net Core 2.2 console application : System.Private.CoreLib System.ArgumentOutOfRangeException (FileSystemWatcher)

Open patrick-blanc opened this issue 7 years ago • 4 comments

Getting unhandled exception from System.Private.CoreLib

System.ArgumentOutOfRangeException HResult=0x80131502 Message=startIndex cannot be larger than length of string. Parameter name: startIndex Source=System.Private.CoreLib StackTrace: at System.String.Substring(Int32 startIndex, Int32 length) at Microsoft.Extensions.FileProviders.Physical.PhysicalFilesWatcher.OnFileSystemEntryChange(String fullPath) at Microsoft.Extensions.FileProviders.Physical.PhysicalFilesWatcher.OnRenamed(Object sender, RenamedEventArgs e) at System.IO.FileSystemWatcher.NotifyRenameEventArgs(WatcherChangeTypes action, String name, String oldName) at System.IO.FileSystemWatcher.ParseEventBufferAndNotifyForEach(Byte[] buffer) at System.IO.FileSystemWatcher.ReadDirectoryChangesCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* overlappedPointer) at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)

My console application (running on windows machine) try to run "npm install" command in different task on different folders.

Sometimes, I get the exception sometimes not. I am not using any FileWatcher in my console application. So my question is why did I get this exception from .net core. Is a .net core console app using by default a file watcher ? Anyone to help me to get rid of that exception.

Here is information from dotnet --info


> .NET Core SDK (reflecting any global.json):
>  Version:   2.2.204
>  Commit:    8757db13ec
> 
> Runtime Environment:
>  OS Name:     Windows
>  OS Version:  10.0.17134
>  OS Platform: Windows
>  RID:         win10-x64
>  Base Path:   C:\Program Files\dotnet\sdk\2.2.204\
> 
> Host (useful for support):
>   Version: 2.2.5
>   Commit:  0a3c9209c0
> 
> .NET Core SDKs installed:
>   2.1.602 [C:\Program Files\dotnet\sdk]
>   2.1.604 [C:\Program Files\dotnet\sdk]
>   2.2.202 [C:\Program Files\dotnet\sdk]
>   2.2.204 [C:\Program Files\dotnet\sdk]
> 
> .NET Core runtimes installed:
>   Microsoft.AspNetCore.All 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
>   Microsoft.AspNetCore.All 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
>   Microsoft.AspNetCore.All 2.2.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
>   Microsoft.AspNetCore.All 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
>   Microsoft.AspNetCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
>   Microsoft.AspNetCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
>   Microsoft.AspNetCore.App 2.2.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
>   Microsoft.AspNetCore.App 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
>   Microsoft.NETCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
>   Microsoft.NETCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
>   Microsoft.NETCore.App 2.2.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
>   Microsoft.NETCore.App 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]

I see a thread related to same exception on aspnetcore (1 year ago) : #https://github.com/aspnet/AspNetCore/issues/2536

it seems it has been fixed in release 2.0.1 of Microsoft.Extensions.FileProviders.Physical, as I am using .net core 2.2 I don't think it's related. As said before I am not creating any FileWatcher. My code is basically checking if directory exists (like node_modules) if not it run a process (cmd) and run the npm install.

Notice that I find strange that PhysicalFilesWatcher.OnRenamed is throwing an exception on System.String.Substring(Int32 startIndex, Int32 length) because OnRenamed does not use this method but is using System.String.Substring(Int32 startIndex)

Thanks for your help any hints would be greatly appreciated

patrick-blanc avatar May 16 '19 13:05 patrick-blanc

After investigations, we found that problem seems to be related to this commands.

.AddJsonFile("appsettings.json", false, true)

By settings true for reload on change (that was a mistake done by c/c code), a file system watched is created but not only on the appsettings file. The file system start check the entire files in all folders, subfolders. This produce a memory leak done by the FileSystemWatcher that was taking memory and memory each time a npm install was done by a child process of the console app.

To fix it I just set the reloadOnChange to false. It does not create any watcher then and no memory problem at least.

.AddJsonFile("appsettings.json", false, false)

patrick-blanc avatar May 21 '19 10:05 patrick-blanc

This should be in https://github.com/aspnet/Extensions. @karelz do you have write access to transfer this or know who does?

Microsoft.Extensions.FileProviders.Physical.PhysicalFilesWatcher.OnFileSystemEntryChange should be hardened for the reported call stack.

JeremyKuhne avatar Jun 03 '19 21:06 JeremyKuhne

This issue was moved to aspnet/Extensions#1782

carlossanlop avatar Jun 03 '19 22:06 carlossanlop

This issue was moved to aspnet/Extensions#1783

carlossanlop avatar Jun 03 '19 22:06 carlossanlop