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

Realm.GetInstanceAsync never completes on some Android devices #3690

Open
hankovich opened this issue Jan 15, 2025 · 3 comments
Open

Realm.GetInstanceAsync never completes on some Android devices #3690

hankovich opened this issue Jan 15, 2025 · 3 comments

Comments

@hankovich
Copy link

What happened?

I am working on an SDK for our backend services, which is a .NET Standard 2.1 NuGet package but is used in Unity (via UnityNuGet). So I use a version from NuGet in Unity. I can't use Unity version directly, because my SDK targets .NET Standard, not Unity.

On some Android devices that we test our application (for example Xiaomi Redmi Note 13 Pro, but only on the physical device, not in browserstack or in emulator) GetInstanceAsync returns a task that never completes. GetInstance works perfectly.

Repro steps

var path = Application.persistentDataPath;

using var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
using var currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");

var pipePath = currentActivity.Call<AndroidJavaObject>("getFilesDir").Call<string>("getAbsolutePath");

var configuration = new RealmConfiguration(Path.Combine(path, "file.realm"))
{
    FallbackPipePath = pipePath, // https://github.com/realm/realm-dotnet/issues/3286
};

return Realm.GetInstanceAsync(configuration); // never completes

Version

20.1.0

What Atlas Services are you using?

Local Database only

What type of application is this?

Unity

Client OS and version

Android 14

Code snippets

No response

Stacktrace of the exception/crash you're getting

Relevant log output

@nirinchev
Copy link
Member

Does this work if you replace the GetInstanceAsync call with GetInstance? Asking because for non-synchronized Realms, the async version doesn't really do anything super interesting. The other consideration is that Application.persistentDataPath doesn't work reliably for Android. You can see that in the Realm.Unity package we're using a bit of reflection to get to a writeable path:

if (Application.platform != RuntimePlatform.Android)
{
return Application.persistentDataPath;
}
// On Android persistentDataPath returns the external folder, where the app may not have permissions to write.
// we use reflection to call File.getAbsolutePath(currentActivity.getFilesDir())
using var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
using var currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
var filesDir = currentActivity.Call<AndroidJavaObject>("getFilesDir");
return filesDir.Call<string>("getAbsolutePath");
. My memory is hazy for why this was necessary, but we definitely reproduced issues and went with that workaround, which resolved them for all we know.

@hankovich
Copy link
Author

Does this work if you replace the GetInstanceAsync call with GetInstance?

Yes, everything loads without any problems. As I see the async version runs migrations on a background thread, so I'm afraid a bit the the sync version will freeze my UI sometimes. I think it would be nice to understand the original issue, becuase GetInstanceAsync seems to be not very stable (I've already reported an issue in it: #3681)

As I see Application.persistentDataPath works without problems if a correct (writable for named pipe) FallbackPipePath is specified

@nirinchev
Copy link
Member

I guess if your app explicitly asks for permissions to write to external storage, you would be fine with persistentDataPath. On the migration part - you're right, migrations would be run on a background thread with GetInstanceAsync. Unfortunately, it's not obvious to me what could cause the method to fail to return and those are notoriously difficult to debug. If you're able to get a stacktrace or some error, that could point us to the root cause, but considering the Realm SDKs have beed transitioned to a community-supported product, MongoDB wouldn't be in a position to dedicate resources to actively investigate it. That being said, we'd be happy to assist by reviewing a PR or providing pointers in case you manage to get to the underlying error that occurs with the async method.

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

No branches or pull requests

2 participants