Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.IO.IOExceptionSharing violation on path /data/data/de.foo.android/files/carts/foobar.json #14

Open
armintelker opened this issue Apr 27, 2015 · 3 comments

Comments

@armintelker
Copy link

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 
@Szymaniuk
Copy link

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 

@Szymaniuk
Copy link

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 

@raver99
Copy link

raver99 commented Aug 1, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants