LiteDB icon indicating copy to clipboard operation
LiteDB copied to clipboard

[BUG] System.IO.IOException: Sharing violation on path /data/user/0/...

Open DamienDoumer opened this issue 2 years ago • 2 comments

I'm having this issue on Litedb with Android app (built with xamarin forms),

Version: 5.0.11

Note:

  • I ensure that there is only one instance of the LiteDatabase in my app, by registering it as a singleton in my service provider at startup.
  • I Dispose my LiteDatabase object Only when the application's MainActivity fires the "OnDestroyed" method. When testing on my local devices, I have no issues. But in prod, I find several android devices having the issue below. Please help, Is there something I'm doing wrong?

FileStreamFactory.GetStream (System.Boolean canWrite, System.Boolean sequencial) System.IO.IOException: Sharing violation on path /data/user/0/

Here is how I create the single instance of my LiteDatabase:

        var baseDir = FileSystem.AppDataDirectory;
        var dbPath = Path.Combine(baseDir, options.Value.DatabaseFileName);
        var dbDir = Path.GetDirectoryName(dbPath);
        if (!Directory.Exists(dbDir))
        {
            Directory.CreateDirectory(Path.GetDirectoryName(dbPath));
        }
        Connection = new ConnectionString
        {
            Filename = dbPath,
            Connection = ConnectionType.Direct
        };
        Database = new LiteDatabase(Connection);

Here is my stacktrace:

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) [0x001b7] in <9a2141e59f004b13b49be68b4ea0bbf4>:0 System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.IO.FileOptions options) [0x00000] in <9a2141e59f004b13b49be68b4ea0bbf4>:0 (wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int,System.IO.FileOptions) FileStreamFactory.GetStream (System.Boolean canWrite, System.Boolean sequencial) <.ctor>b__0 () Lazy1[T].ViaFactory (System.Threading.LazyThreadSafetyMode mode) Lazy1[T].ExecutionAndPublication (System.LazyHelper executionAndPublication, System.Boolean useDefaultConstructor) Lazy1[T].CreateValue () Lazy1[T].get_Value () StreamPool.get_Writer () LiteDB.Engine.DiskService..ctor (LiteDB.Engine.EngineSettings settings, System.Int32[] memorySegmentSizes) [0x000ab] in <7dd2c2ae6ae04572a54f6c996328226c>:0 LiteDB.Engine.LiteEngine..ctor (LiteDB.Engine.EngineSettings settings) [0x00173] in <7dd2c2ae6ae04572a54f6c996328226c>:0 ConnectionString.CreateEngine () LiteDB.LiteDatabase..ctor (LiteDB.ConnectionString connectionString, LiteDB.BsonMapper mapper) [0x00034] in <7dd2c2ae6ae04572a54f6c996328226c>:0 MyAppe.MobileApp.CacheServices.LiteDb.Shared.LiteDBConnection..ctor (Microsoft.Extensions.Options.IOptions1[TOptions] options) [0x00055] in <a0bb4854b1d6427388c3f8741e779843>:0 (wrapper managed-to-native) System.Reflection.RuntimeConstructorInfo.InternalInvoke(System.Reflection.RuntimeConstructorInfo,object,object[],System.Exception&) RuntimeConstructorInfo.InternalInvoke (System.Object obj, System.Object[] parameters, System.Boolean wrapExceptions) RuntimeConstructorInfo.DoInvoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) RuntimeConstructorInfo.Invoke (System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) CallSiteRuntimeResolver.VisitConstructor (Microsoft.Extensions.DependencyInjection.ServiceLookup.ConstructorCallSite constructorCallSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext context) CallSiteVisitor2[TArgument,TResult].VisitCallSiteMain (Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite callSite, TArgument argument) CallSiteRuntimeResolver.VisitRootCache (Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite callSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext context) CallSiteVisitor2[TArgument,TResult].VisitCallSite (Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite callSite, TArgument argument) CallSiteRuntimeResolver.VisitConstructor (Microsoft.Extensions.DependencyInjection.ServiceLookup.ConstructorCallSite constructorCallSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext context) CallSiteVisitor2[TArgument,TResult].VisitCallSiteMain (Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite callSite, TArgument argument) CallSiteRuntimeResolver.VisitDisposeCache (Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite transientCallSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext context) CallSiteVisitor2[TArgument,TResult].VisitCallSite (Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite callSite, TArgument argument) CallSiteRuntimeResolver.VisitConstructor (Microsoft.Extensions.DependencyInjection.ServiceLookup.ConstructorCallSite constructorCallSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext context) CallSiteVisitor2[TArgument,TResult].VisitCallSiteMain (Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite callSite, TArgument argument) CallSiteRuntimeResolver.VisitRootCache (Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite callSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext context) CallSiteVisitor2[TArgument,TResult].VisitCallSite (Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite callSite, TArgument argument) CallSiteRuntimeResolver.Resolve (Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite callSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope scope) ServiceProvider.CreateServiceAccessor (System.Type serviceType) ConcurrentDictionary2[TKey,TValue].GetOrAdd (TKey key, System.Func2[T,TResult] valueFactory) ServiceProvider.GetService (System.Type serviceType, Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope serviceProviderEngineScope) ServiceProvider.GetService (System.Type serviceType) ServiceProviderServiceExtensions.GetRequiredService (System.IServiceProvider provider, System.Type serviceType) ServiceProviderServiceExtensions.GetRequiredService[T] (System.IServiceProvider provider) MyAppe.MobileApp.App..ctor (System.Action1[T] configuration, System.Action`2[T1,T2] dependencyServiceConfiguration) [0x00057] in <4c7de3aa76d348f2b3428d0d66c81620>:0 MainActivity.OnCreate (Android.OS.Bundle savedInstanceState) AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) SyncContext+<>c__DisplayClass2_0.<Post>b__0 () Thread+RunnableImplementor.Run () IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.132(intptr,intptr)

. Can someone please help? is there a trick somewhere?

DamienDoumer avatar Mar 11 '22 01:03 DamienDoumer

On Android, you can have multiple instance of an app running and this create problem with connection=direct. A workaround for me is that I would catch the IOException then attempt connection=shared

LiteDatabase db;
try {
    db = new LiteDatabase("Filename=abc.db");
}
catch(IOException) {
    db = new LiteDatabase("Filename=abc.db;Connection=shared");
}

timdinhdotcom avatar May 04 '22 08:05 timdinhdotcom

Shared connection doesn't work everywhere. Android doesn't support it check this doc : https://www.litedb.org/docs/connection-string/

DamienDoumer avatar May 05 '22 13:05 DamienDoumer