PCLStorage
PCLStorage copied to clipboard
System.IO.IOExceptionSharing violation on path /data/data/de.foo.android/files/carts/foobar.json
Hey Daniel, we got in some cases an error. At the most times we got it when the app is in background.
Do you know something about this kind of error?
public async Task Save(string path, string content)
{
var subFolder = await rootFolder.CreateFolderAsync(Path.GetDirectoryName(path), CreationCollisionOption.OpenIfExists);
var file = await subFolder.CreateFileAsync(Path.GetFileName(path), CreationCollisionOption.ReplaceExisting);
await file.WriteAllTextAsync(content);
}
at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000] in <filename unknown>:0
at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share) [0x00000] in <filename unknown>:0
at System.IO.File.Open (System.String path, FileMode mode, FileAccess access) [0x00000] in <filename unknown>:0
at PCLStorage.FileSystemFile+<OpenAsync>d__0.MoveNext () [0x00000] in <filename unknown>:0
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[System.IO.Stream].GetResult () [0x00000] in <filename unknown>:0
at PCLStorage.FileExtensions+<WriteAllTextAsync>d__7.MoveNext () [0x00000] in <filename unknown>:0
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0
at System.Runtime.CompilerServices.TaskAwaiter.GetResult () [0x00000] in <filename unknown>:0
at Foo.Common.Storage.FileStorage+<Save>c__async0.MoveNext () [0x00000] in <filename unknown>:0
Same for me - this exception occurs from time to time:
System.IO.IOException: Sharing violation on path /data/user/0/com.my.project/files/Logo/Logo.png
at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x001f1] in <3fd174ff54b146228c505f23cf75ce71>:0
at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize) [0x00000] in <3fd174ff54b146228c505f23cf75ce71>:0
at (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int)
at System.IO.File.Create (System.String path, System.Int32 bufferSize) [0x00000] in <3fd174ff54b146228c505f23cf75ce71>:0
at System.IO.File.Create (System.String path) [0x00000] in <3fd174ff54b146228c505f23cf75ce71>:0
at PCLStorage.FileSystemFolder.InternalCreateFile (System.String path) [0x00000] in <fb422c97cc834baea6a4c42c2408b899>:0
at PCLStorage.FileSystemFolder+<CreateFileAsync>d__0.MoveNext () [0x001ea] in <fb422c97cc834baea6a4c42c2408b899>:0
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <3fd174ff54b146228c505f23cf75ce71>:0
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <3fd174ff54b146228c505f23cf75ce71>:0
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <3fd174ff54b146228c505f23cf75ce71>:0
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <3fd174ff54b146228c505f23cf75ce71>:0
at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in <3fd174ff54b146228c505f23cf75ce71>:0
at MyProject.Data.FileStorageService+<CreateRawFile>d__2.MoveNext () [0x00117] in C:\workspace\MyProject\MyProject.Data\FileStorageService.cs:50
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <3fd174ff54b146228c505f23cf75ce71>:0
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <3fd174ff54b146228c505f23cf75ce71>:0
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <3fd174ff54b146228c505f23cf75ce71>:0
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <3fd174ff54b146228c505f23cf75ce71>:0
at System.Runtime.CompilerServices.TaskAwaiter.GetResult () [0x00000] in <3fd174ff54b146228c505f23cf75ce71>:0
at MyProject.Sync.Cache.CommonCache+<SaveCustomerLogoFile>d__5.MoveNext () [0x00051] in C:\workspace\MyProject\MyProject.Sync\Cache\CommonCache.cs:42
Happened again with latest nuget release version, at Android device, during debbuging:
Xamarin caused by: android.runtime.JavaProxyThrowable: System.IO.IOException: Sharing violation on path /data/user/0/com.mycompany.myproject/files/Attachments/IMG_2017-10-23T10:24:12Z.JPG
at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x001f1] in <896ad1d315ca4ba7b117efb8dacaedcf>:0
at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share) [0x00000] in <896ad1d315ca4ba7b117efb8dacaedcf>:0
at (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
at System.IO.File.Open (System.String path, System.IO.FileMode mode, System.IO.FileAccess access) [0x00000] in <896ad1d315ca4ba7b117efb8dacaedcf>:0
at PCLStorage.FileSystemFile+<OpenAsync>d__0.MoveNext () [0x000ad] in <fb422c97cc834baea6a4c42c2408b899>:0
We also had this in production. The root cause is, like the message says, a "Sharing violation", you are trying to access the file while it is being in use. Like reading and writing at the same time. This can happen in a multithreading application environment.
I fixed this by using a semaphore (SemaphoreSlim) to prevent concurrent access.