Threading mode toggle does not work with realm
See original GitHub issueWhile 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:
- Created 2 years ago
- Comments:6 (4 by maintainers)
@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 theContextFactory
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
No worries. I’ll try and get an initial PR out today so you can leave some proper feedback on it.