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

InvalidCastException in SynchronizationContextScheduler on Unity #3681

Open
hankovich opened this issue Nov 18, 2024 · 3 comments
Open

InvalidCastException in SynchronizationContextScheduler on Unity #3681

hankovich opened this issue Nov 18, 2024 · 3 comments

Comments

@hankovich
Copy link

hankovich commented Nov 18, 2024

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). In the Unity Editor, I occasionally encounter an InvalidCastException when trying to open a Realm.

So I use a version from NuGet in Unity. I can't use Unity version directly, because my SDK targets .NET Standard, not Unity.

Repro steps

It usually occurs when you unload a game while it is opening a Realm.

Version

20.0.0

What Atlas Services are you using?

Local Database only

What type of application is this?

Unity

Client OS and version

Unity Editor (2022.3.51а1) on MacBook

Code snippets

await Realm.GetInstanceAsync(new RealmConfiguration("db.realm")))

Stacktrace of the exception/crash you're getting

at Realms.SynchronizationContextScheduler.IsOnSynchronizationContext (System.IntPtr context, System.IntPtr targetContext) [0x0001b] in <88190757fccb474da2767b2f4a5c18b3>:0
at (wrapper native-to-managed).(wrapper native-to-managed) Realms.SynchronizationContextScheduler.IsOnSynchronizationContext(intptr,intptr)
at (wrapper managed-to-native).(wrapper managed-to-native) Realms.SharedRealmHandle+NativeMethods.open(Realms.Native.Configuration,Realms.NativeException&)
at Realms.SharedRealmHandle.Open (Realms.Native.Configuration configuration) [0x00000] in <88190757fccb474da2767b2f4a5c18b3>:0
at Realms.RealmConfiguration.CreateHandle (Realms.Native.Configuration& configuration) [0x00000] in <88190757fccb474da2767b2f4a5c18b3>:0
at Realms.RealmConfiguration+<>c__DisplayClass24_0.<CreateHandleAsync>b__1 (System.Threading.Tasks.Task _) [0x00000] in <88190757fccb474da2767b2f4a5c18b3>:0
at System.Threading.Tasks.ContinuationResultTaskFromTask`1[TResult].InnerInvoke () [0x00024] in <10d2623021134ffe9e1b5223c5ae4e48>:0
at System.Threading.Tasks.Task.Execute () [0x00000] in <10d2623021134ffe9e1b5223c5ae4e48>:0
--- End of stack trace from previous location where exception was thrown ---
at Realms.RealmConfigurationBase.CreateRealmAsync (System.Threading.CancellationToken cancellationToken) [0x00098] in <88190757fccb474da2767b2f4a5c18b3>:0

Relevant log output

No response

Copy link

sync-by-unito bot commented Nov 18, 2024

➤ PM Bot commented:

Jira ticket: RNET-1178

@hankovich
Copy link
Author

As I see RNET-1178 is closed and marked as won't fix without any comments.

Could you please provide comments? How can I workaround this bug?

@nirinchev
Copy link
Member

We're moving away from duplicating github issues in Jira, which is why the Jira ticket was closed. There's no obvious workaround other than not using the asynchronous API here. It was primarily useful for synchronized Realms where you wanted to wait for the download to complete before you display the data, but for local use cases, there's no particular benefit. If you do want to keep it for the sake of executing migrations on a background thread, you could make it conditional, where it uses the async API in release builds, but the sync one in debug builds.

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