question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Threading mode toggle does not work with realm

See original GitHub issue

While looking into #13580 on master, on most attempts to switch the threading mode, I get:

2021-06-19 15:33:42 [error]: System.Exception: Realm accessed from incorrect thread.
2021-06-19 15:33:42 [error]: at Realms.NativeException.ThrowIfNecessary(Func`2 overrider)
2021-06-19 15:33:42 [error]: at Realms.SharedRealmHandle.Refresh()
2021-06-19 15:33:42 [error]: at Realms.Realm.Refresh()
2021-06-19 15:33:42 [error]: at osu.Game.Database.RealmContextFactory.Update() in /home/dachb/Dokumenty/opensource/osu/osu.Game/Database/RealmContextFactory.cs:line 84

The context was created on the update thread, but is being accessed on the main thread after the threading mode change. I thought this was going to be pretty easy to get past by just purging realm contexts on threading mode switch, so I applied the following change. But then after some more debugging of the aforementioned issue I was hit by this:

2021-06-19 15:29:20 [error]: Realms.Exceptions.RealmClosedException: This object belongs to a closed realm.
2021-06-19 15:29:20 [error]: at Realms.NativeException.ThrowIfNecessary(Func`2 overrider)
2021-06-19 15:29:20 [error]: at Realms.ObjectHandle.GetValue(String propertyName, Metadata metadata, Realm realm)
2021-06-19 15:29:20 [error]: at Realms.RealmObjectBase.GetValue(String propertyName)
2021-06-19 15:29:20 [error]: at osu.Game.Input.Bindings.RealmKeyBinding.get_KeyCombinationString()
2021-06-19 15:29:20 [error]: at osu.Game.Input.Bindings.RealmKeyBinding.get_KeyCombination() in /home/dachb/Dokumenty/opensource/osu/osu.Game/Input/Bindings/RealmKeyBinding.cs:line 23
2021-06-19 15:29:20 [error]: at osu.Game.Overlays.Toolbar.ToolbarButton.updateKeyBindingTooltip() in /home/dachb/Dokumenty/opensource/osu/osu.Game/Overlays/Toolbar/ToolbarButton.cs:line 227

Which is worse to get past, and is why I’m opening this issue for discussion. It looks like any live objects are no longer safe to hold onto after a threading mode change and they need to all be invalidated. I think Live<T> will have to return in some capacity to resolve this; I don’t immediately see any other way out.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
peppycommented, Jun 22, 2021

@bdach opinions welcome on this WIP change:

https://github.com/ppy/osu/compare/master...peppy:realm-live-v2?expand=1#diff-4c57b58c12ff7dcf9a006014ce2b9be18a05c3af28aceb3b47e38bc003b49855R170-R179

The Wrap call could potentially be moved inside the ContextFactory to simplify things, but this general flow seems to work at least…

Note that the branch currently crashes due to issues pointed out in https://github.com/ppy/osu-framework/pull/4533#issuecomment-865875998

0reactions
peppycommented, Jun 23, 2021

No worries. I’ll try and get an initial PR out today so you can leave some proper feedback on it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Silent `Realm.Write` failure when Realm instance thread is ...
Goal When calling Realm.Write within a Timer event hander/descendent, the Realm.Write does not error and thus the data is not persisted and ...
Read more >
Realm Background Notifications access Realm from wrong ...
The problem is that after startWorker starts the thread, the app crashes, highlighting RunLoop.current.run(mode: .
Read more >
Realm Swift Tutorial failing offline-first usage
I've been a fan and using Realm (locally) since 2014 in multiple apps. Even discussed sync with the original team back in the...
Read more >
Cannot change Minecraft Realm mode - Server Support ...
Hello I play on bedrock edition Minecraft and have ran into this problem twice now, when dealing with online...
Read more >
Chaos Realms Narrative Toggle : r/totalwar
I took a few hours this morning to write a simple UI mod for a request I've seen a lot on the subreddit:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found