Sharing violation on Android using Xamarin Forms
Hi,
Have randomly been getting the following error:
Unable to retrieve image data
System.IO.IOException: Sharing violation on path /data/user/0/<project>/cache/ImageService/7b8116362b9408c1d5e7437479980e90
Unfortunately I'm unable to provide an example to replicate the issue as it happens randomly. The app I'm using your awesome package with does a lot of image downloading on app load so perhaps there's too much happening at once?
Here's the full stack trace:
System.IO.IOException: Sharing violation on path /data/user/0/<project>/cache/ImageService/7b8116362b9408c1d5e7437479980e90
at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x0026d] in /Users/builder/data/lanes/2185/53fce373/source/mono/mcs/class/corlib/System.IO/FileStream.cs:257
at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean isAsync, Boolean anonymous) [0x00000] in /Users/builder/data/lanes/2185/53fce373/source/mono/mcs/class/corlib/System.IO/FileStream.cs:157
at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access) [0x00000] in /Users/builder/data/lanes/2185/53fce373/source/mono/mcs/class/corlib/System.IO/FileStream.cs:92
at (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess)
at FFImageLoading.IO.FileStore.GetOutputStream (System.String path) [0x00000] in <filename unknown>:0
at FFImageLoading.Cache.DiskCache+<AddOrUpdateAsync>c__async0.MoveNext () [0x00072] in <filename unknown>:0
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in /Users/builder/data/lanes/2185/53fce373/source/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:61
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/2185/53fce373/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:201
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/2185/53fce373/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:170
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/2185/53fce373/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:142
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable+ConfiguredTaskAwaiter.GetResult () [0x00000] in /Users/builder/data/lanes/2185/53fce373/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:477
at FFImageLoading.Cache.DownloadCache+<DownloadAndCacheAsync>c__async2.MoveNext () [0x003b6] in <filename unknown>:0
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in /Users/builder/data/lanes/2185/53fce373/source/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:61
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/2185/53fce373/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:201
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/2185/53fce373/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:170
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/2185/53fce373/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:142
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[System.IO.MemoryStream].GetResult () [0x00000] in /Users/builder/data/lanes/2185/53fce373/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:580
at FFImageLoading.Cache.DownloadCache+<GetStreamAsync>c__async1.MoveNext () [0x00102] in <filename unknown>:0
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in /Users/builder/data/lanes/2185/53fce373/source/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:61
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/2185/53fce373/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:201
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/2185/53fce373/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:170
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/2185/53fce373/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:142
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[FFImageLoading.Cache.CacheStream].GetResult () [0x00000] in /Users/builder/data/lanes/2185/53fce373/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:580
at FFImageLoading.Work.StreamResolver.UrlStreamResolver+<GetStream>c__async0.MoveNext () [0x0008f] in <filename unknown>:0
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in /Users/builder/data/lanes/2185/53fce373/source/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:61
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/2185/53fce373/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:201
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/2185/53fce373/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:170
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/2185/53fce373/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:142
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[FFImageLoading.Work.WithLoadingResult`1[System.IO.Stream]].GetResult () [0x00000] in /Users/builder/data/lanes/2185/53fce373/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:580
at FFImageLoading.Work.ImageLoaderTask+<GetStreamAsync>c__async5.MoveNext () [0x000e4] in <filename unknown>:0
@ahohns Thanks!
@molinch We could insert a lock with a new separate lock object, only for an image files write operation. It would force only one file write at a time but on mobile devices it could actually have a positive performance impact. There also might be some Android/Linux IO schedulers that won't handle as many concurrent writes and possibly lead to requests time-out. What do you think?
http://forum.xda-developers.com/showthread.php?t=1176334

@ahohns Could you give me the OS version and device model on which you got the issue? Could you also write the loading code that you use?
@daniel-luberda Yes we can have some locking mechanism but I first want to repro the issue
@molinch Link: https://github.com/daniel-luberda/FFImageLoading/commit/a760ba920411a7ce8f30ce59857ee507cbcdcb49 What do you think about it?
Hi @ahohns. There were some changes in 2.0.0-alpha4 - now, FFImageLoading queues file saving operations, so it shouldn't throw "too many open files" exceptions or sharing violations. If you still experience this bug - let us know. Thanks.
Thanks, love the effort you guys are putting in :) I haven't seen the issue again, and will try out alpha4 cheers!
Thanks :)
If you haven't seen the issue, I'll close it. Feel free to reopen it if you experience it once again.
Actually I am getting this error. My app creates a csv file to sdcard. when I attempt to open that csv later file I get following error
[0:] Test run failed: System.IO.IOException: Sharing violation on path /storage/emulated/0/UnitTestLogs/2018_01_23_12-16-52_Android_UnitTest_Results.csv 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) [0x001e4] in /Users/builder/jenkins/workspace/xamarin-android/xamarin-android/external/mono/mcs/class/corlib/System.IO/FileStream.cs:247 at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode) [0x00000] in /Users/builder/jenkins/workspace/xamarin-android/xamarin-android/external/mono/mcs/class/corlib/System.IO/FileStream.cs:81 at (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode)
using (Stream stream = new FileStream(m_resultsWriter.CsvFilePath, FileMode.Open)) \this line throws error
{
...
Hi @pMaske this has nothing to do with FFImageLoading, the lib is only about images, it has no business with CSV files. I guess you should look into your usage of that file, how you open/close the file streams.