MonoTouch File.ReadAllText throws an exception from the internal FileStream.Dispose ()

It seems that the following code sometimes creates this stack. It seems like this happens within the framework when the dispose method is called on the internal FileStream object? It seems like a mistake to me, any ideas?

 var previousVersion = long.Parse(File.ReadAllText(Paths.VersionFile)); 

Stacktrace:

  System.IO.IOException: Invalid parameter at System.IO.FileStream.Dispose (Boolean disposing) [0x00000] in <filename unknown>:0 at System.IO.Stream.Close () [0x00000] in <filename unknown>:0 at System.IO.StreamReader.Dispose (Boolean disposing) [0x00000] in <filename unknown>:0 at System.IO.TextReader.Dispose () [0x00000] in <filename unknown>:0 at System.IO.File.ReadAllText (System.String path) [0x00000] in <filename unknown>:0 at CatalystHD.Shared.BaseLoginController.CheckIfFirstRunThisVersion () [0x00000] in <filename unknown>:0 at CatalystHD.Shared.BaseLoginController.ViewDidLoad () [0x00000] in <filename unknown>:0 at MonoTouch.UIKit.UIViewController.get_View () [0x00000] in <filename unknown>:0 at CatalystHD.IPad.RootViewController.AnimateTo (MonoTouch.UIKit.UIViewController aController, UIViewAnimationTransition transition) [0x00000] in <filename unknown>:0 at CatalystHD.IPad.RootViewController.Logout (Boolean timeout) [0x00000] in <filename unknown>:0 at CatalystHD.IPad.NotebookSelectionController.logoutButton_Clicked (System.Object sender, System.EventArgs e) [0x00000] in <filename unknown>:0 at MonoTouch.UIKit.UIBarButtonItem Callback.Call (MonoTouch.Foundation.NSObject sender) [0x00000] in <filename unknown>:0 at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00000] in <filename unknown>:0 at MonoTouch.UIKit.UIApplication.Main (System.String[] args) [0x00000] in <filename unknown>:0 at CatalystHD.Application.Main (System.String[] args) [0x00000] in <filename unknown>:0 

VersionFile path captured in this way:

 public static string VersionFile { get { var path = Environment.GetFolderPath(Environment.SpecialFolder.Personal); return Path.Combine(path, "version.dat"); } } 
+6
source share
1 answer

If I had to guess, I think you might encounter a race condition due to the implicit Flush() that occurs during deletion under the hood of a FileStream in this ReadAllText call.

(caveat: not in front of mono sources, therefore cannot confirm the exact behavior)

Is it possible that:

  • You replace / open / etc. same file path in another thread?
  • Removing this path immediately after or in another thread?
  • Otherwise, loss of access to the checked path? (file share sharing?)
0
source

Source: https://habr.com/ru/post/910105/


All Articles