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

Mod unsafely accesses client world off-thread #172

Closed
embeddedt opened this issue Nov 14, 2023 · 63 comments
Closed

Mod unsafely accesses client world off-thread #172

embeddedt opened this issue Nov 14, 2023 · 63 comments
Assignees
Labels
bug Something isn't working

Comments

@embeddedt
Copy link

Bug description

Sound Physics Remastered accesses the client world from the sound thread. The client world is not really thread-safe and doing this can lead to all sorts of issues. In my case, it seems to cause ghost Create mechanical belts to occasionally render in the world with Embeddium. I am not 100% sure of the exact action that causes that issue, but it seems related to block retrieval off-thread triggering creation of block entities as well as other side effects. A stack trace from my debug mixin is attached below so you can see what I mean (it's using SRG names, sorry about that).

To fix this issue properly I believe the mod will need to be refactored so that all block retrieval is done on the main thread and stored in a snapshot data structure that the sound thread then accesses. Otherwise, this will likely continue to cause all sorts of subtle problems that will be very difficult for players/other mod authors to track down.

This issue was discovered on 1.20.1 but likely affects earlier versions too unless the code changed significantly.

java.lang.Exception: null
	at net.minecraft.world.level.chunk.LevelChunk.handler$zbb000$yellForOffThread(LevelChunk.java:1338) ~[client-1.20.1-20230612.114412-srg.jar%23156!/:?] {re:mixin,re:classloading,pl:mixin:APP:flywheel.mixins.json:instancemanage.InstanceAddMixin,pl:mixin:APP:rubidium.mixins.json:core.render.world.WorldMixin,pl:mixin:A}
	at net.minecraft.world.level.chunk.LevelChunk.m_62934_(LevelChunk.java) ~[client-1.20.1-20230612.114412-srg.jar%23156!/:?] {re:mixin,re:classloading,pl:mixin:APP:flywheel.mixins.json:instancemanage.InstanceAddMixin,pl:mixin:APP:rubidium.mixins.json:core.render.world.WorldMixin,pl:mixin:A}
	at net.minecraft.world.level.chunk.LevelChunk.m_5685_(LevelChunk.java:315) ~[client-1.20.1-20230612.114412-srg.jar%23156!/:?] {re:mixin,re:classloading,pl:mixin:APP:flywheel.mixins.json:instancemanage.InstanceAddMixin,pl:mixin:APP:rubidium.mixins.json:core.render.world.WorldMixin,pl:mixin:A}
	at net.minecraft.world.level.Level.m_7702_(Level.java:560) ~[client-1.20.1-20230612.114412-srg.jar%23156!/:?] {re:mixin,pl:accesstransformer:B,re:classloading,pl:accesstransformer:B}
	at com.simibubi.create.foundation.block.IBE.getBlockEntity(IBE.java:72) ~[create-1.20.1-0.5.1.f.jar%23151!/:0.5.1.f] {re:classloading}
	at com.simibubi.create.foundation.block.IBE.getBlockEntityOptional(IBE.java:53) ~[create-1.20.1-0.5.1.f.jar%23151!/:0.5.1.f] {re:classloading}
	at com.simibubi.create.content.kinetics.belt.BeltBlock.m_5939_(BeltBlock.java:370) ~[create-1.20.1-0.5.1.f.jar%23151!/:0.5.1.f] {re:classloading}
	at net.minecraft.world.level.block.state.BlockBehaviour$BlockStateBase.m_60742_(BlockBehaviour.java:684) ~[client-1.20.1-20230612.114412-srg.jar%23156!/:?] {re:mixin,pl:accesstransformer:B,re:classloading,pl:accesstransformer:B,pl:mixin:APP:modernfix-common.mixins.json:bugfix.chunk_deadlock.BlockStateBaseMixin,pl:mixin:APP:ferritecore.blockstatecache.mixin.json:BlockStateBaseMixin,pl:mixin:APP:modernfix-common.mixins.json:perf.reduce_blockstate_cache_rebuilds.BlockStateBaseMixin,pl:mixin:A}
	at net.minecraft.world.level.ClipContext$Block.m_7544_(ClipContext.java:62) ~[client-1.20.1-20230612.114412-srg.jar%23156!/:?] {re:classloading}
	at net.minecraft.world.level.ClipContext.m_45694_(ClipContext.java:40) ~[client-1.20.1-20230612.114412-srg.jar%23156!/:?] {re:classloading}
	at net.minecraft.world.level.BlockGetter.m_151358_(BlockGetter.java:63) ~[client-1.20.1-20230612.114412-srg.jar%23156!/:?] {re:mixin,re:classloading}
	at net.minecraft.world.level.BlockGetter.m_151361_(BlockGetter.java:119) ~[client-1.20.1-20230612.114412-srg.jar%23156!/:?] {re:mixin,re:classloading}
	at net.minecraft.world.level.BlockGetter.m_45547_(BlockGetter.java:58) ~[client-1.20.1-20230612.114412-srg.jar%23156!/:?] {re:mixin,re:classloading}
	at com.sonicether.soundphysics.SoundPhysics.raycast(SoundPhysics.java:618) ~[soundphysics-forge-1.20.1-1.2.1.jar%23155!/:1.20.1-1.2.1] {re:mixin,re:classloading}
	at com.sonicether.soundphysics.SoundPhysics.evaluateEnvironment(SoundPhysics.java:287) ~[soundphysics-forge-1.20.1-1.2.1.jar%23155!/:1.20.1-1.2.1] {re:mixin,re:classloading}
	at com.sonicether.soundphysics.SoundPhysics.processSound(SoundPhysics.java:180) ~[soundphysics-forge-1.20.1-1.2.1.jar%23155!/:1.20.1-1.2.1] {re:mixin,re:classloading}
	at com.sonicether.soundphysics.SoundPhysics.onPlaySound(SoundPhysics.java:147) ~[soundphysics-forge-1.20.1-1.2.1.jar%23155!/:1.20.1-1.2.1] {re:mixin,re:classloading}
	at com.mojang.blaze3d.audio.Channel.handler$zjj000$play(Channel.java:533) ~[client-1.20.1-20230612.114412-srg.jar%23156!/:?] {re:mixin,re:classloading,pl:mixin:APP:assets/sound_physics_remastered/sound_physics_remastered.mixins.json:SourceMixin,pl:mixin:APP:assets/sound_physics_remastered/sound_physics_remastered.mixins.json:ChannelAccessor,pl:mixin:A}
	at com.mojang.blaze3d.audio.Channel.m_83672_(Channel.java) ~[client-1.20.1-20230612.114412-srg.jar%23156!/:?] {re:mixin,re:classloading,pl:mixin:APP:assets/sound_physics_remastered/sound_physics_remastered.mixins.json:SourceMixin,pl:mixin:APP:assets/sound_physics_remastered/sound_physics_remastered.mixins.json:ChannelAccessor,pl:mixin:A}
	at net.minecraft.client.sounds.SoundEngine.lambda$play$6(SoundEngine.java:404) ~[client-1.20.1-20230612.114412-srg.jar%23156!/:?] {re:mixin,pl:accesstransformer:B,pl:runtimedistcleaner:A,re:classloading,pl:accesstransformer:B,pl:mixin:APP:assets/sound_physics_remastered/sound_physics_remastered.mixins.json:SoundSystemMixin,pl:mixin:A,pl:runtimedistcleaner:A}
	at net.minecraft.client.sounds.ChannelAccess$ChannelHandle.m_120157_(ChannelAccess.java:34) ~[client-1.20.1-20230612.114412-srg.jar%23156!/:?] {re:mixin,re:classloading}
	at net.minecraft.util.thread.BlockableEventLoop.m_6367_(BlockableEventLoop.java:156) ~[client-1.20.1-20230612.114412-srg.jar%23156!/:?] {re:mixin,pl:accesstransformer:B,re:computing_frames,pl:accesstransformer:B,re:classloading,pl:accesstransformer:B}
	at net.minecraft.util.thread.BlockableEventLoop.m_7245_(BlockableEventLoop.java:130) ~[client-1.20.1-20230612.114412-srg.jar%23156!/:?] {re:mixin,pl:accesstransformer:B,re:computing_frames,pl:accesstransformer:B,re:classloading,pl:accesstransformer:B}
	at net.minecraft.util.thread.BlockableEventLoop.m_18701_(BlockableEventLoop.java:139) ~[client-1.20.1-20230612.114412-srg.jar%23156!/:?] {re:mixin,pl:accesstransformer:B,re:computing_frames,pl:accesstransformer:B,re:classloading,pl:accesstransformer:B}
	at net.minecraft.client.sounds.SoundEngineExecutor.m_120336_(SoundEngineExecutor.java:42) ~[client-1.20.1-20230612.114412-srg.jar%23156!/:?] {re:classloading}
	at java.lang.Thread.run(Thread.java:840) ~[?:?] {re:mixin}

Mods installed

  • NeoForge 47.1.79
  • Embeddium 0.2.9
  • Create 0.5.1f
  • FerriteCore 6.0.1
  • ModernFix 5.9.3
  • Sound Physics Remastered 1.20.1-1.2.1
@henkelmax
Copy link
Owner

Please provide the full log files of this crash.
Please also make sure this also happens in the latest version of the mod.

@embeddedt
Copy link
Author

This isn't a specific crash report. It's a technical explanation of a design flaw that I believe is responsible for causing random glitches or crashes in other mods, plus an example stacktrace outlining how the problem occurs.

@CelestialAbyss
Copy link

CelestialAbyss commented Dec 14, 2023

Here's a screenshot of one of the issues encountered on 1.19.2 after installing SPR:

image-67.png

@henkelmax
Copy link
Owner

The mod did always access the client world from another thread. This was already a thing back then for the original mod from Sonic Ether. The mod is designed around being able to do this. I also think this mod wouldn't be feasible if blocks need to be accessed from the main thread. If all evaluation rays need to be casted in the main thread, the game frame rate would be unplayable. For now this has always worked without any issues. Its definitely possible that this causes the issues with the create mod, but usually issues from non-thread safe operations are way more inconsistent, so I'm assuming create does some very weird hacky stuff to the world rendering or changes very fundamental stuff. I also always used the mod in conjunction with Sodium and never had any issues. Maybe Embeddium does some things differently.
Interactions from off thread are also limited to read-only calls, which in general shouldn't cause many issues.

All I can do is declare Create or Embeddium as an incompatible mod, as its impossible to make the mod "Thread safe". If anyone has any ideas on how to work around this issue, please let me know, but Thread safety isn't really possible to my knowledge.

@henkelmax henkelmax closed this as not planned Won't fix, can't repro, duplicate, stale Dec 16, 2023
@henkelmax henkelmax added bug Something isn't working wontfix This will not be worked on and removed triage labels Dec 16, 2023
@embeddedt
Copy link
Author

If anyone has any ideas on how to work around this issue, please let me know, but Thread safety isn't really possible to my knowledge.

The only way to do it correctly (as far as I know) is to make a snapshot of the needed block data on the main thread, and then work with that on the sound thread (without ever accessing the client world directly). This is the same approach vanilla uses to update the chunk meshes using worker threads (16x16x16 snapshots are made on main and then used by the workers).

Anyways, the next release of Embeddium will warn users that support won't be provided if Sound Physics (or other unsafe threading mods like Entity Culling) are installed, as I'm getting too many concurrency-related bug reports which are not caused by me, and that I don't have an easy way to fix.

@IThundxr
Copy link

IThundxr commented Jan 9, 2024

This also causes https://mclo.gs/cWFrIAu by creating a CME when light updater is invoked since it is not thread safe

@MehVahdJukaar
Copy link

Why not just wrapping the level to avoid unsafe calls such as getBlockEntity? That seems to be what's causing the issue above

@IThundxr
Copy link

IThundxr commented Mar 24, 2024

Marked SPR as broken in create fabric due to the mass amounts of concurrency issues we've gotten, If the suggestion above works and fixes this please let me know so i can remove the breaks.

@NolanHewitt
Copy link

The suggestion above looks like it would fix it to me. But only way to know for sure is for it to be implemented and tested.

@henkelmax henkelmax reopened this Apr 8, 2024
@henkelmax henkelmax pinned this issue Apr 8, 2024
@henkelmax henkelmax removed the wontfix This will not be worked on label Apr 8, 2024
@PaulRV0709
Copy link

The create latest patch actually added an incompatibility flag for this mod, so now you can't play with both installed because of this non thread safety.

@henkelmax
Copy link
Owner

henkelmax commented Apr 8, 2024

The create latest patch actually added an incompatibility flag for this mod, so now you can't play with both installed because of this non thread safety.

Yes, as mentioned here:

#172 (comment)

@augustsaintfreytag
Copy link

Sound Physics is an amazing mod and is definitely worth it to see this change implemented. I'd say it gives it some solid future-proofing to use an access-by-snapshot approach, especially if vanilla already does it this way.

@PeglinX
Copy link

PeglinX commented Apr 20, 2024

just to be clear. Is this planned to be worked on?

@Spiderach
Copy link

just to be clear. Is this planned to be worked on?

Looks like it was reopened, pinned, and marked as a bug. If not immediately it looks like there will at least be an attempt to fix it.

@henkelmax
Copy link
Owner

Looks like it was reopened, pinned, and marked as a bug. If not immediately it looks like there will at least be an attempt to fix it.

Yes, I currently don't have the capacity to look into it, but its on my list. If anyone is interested to take a look at it or make a PR, any help is always appreciated!

@henkelmax
Copy link
Owner

This should already be fixed with 1.4.2

@henkelmax
Copy link
Owner

In case it still happens, I'd appreciate if you'd send me logs of that version again.

@Fyoncle
Copy link

Fyoncle commented Jun 3, 2024

What about Create compatibility? Will it be fixed or no

@IThundxr
Copy link

IThundxr commented Jun 3, 2024

What about Create compatibility? Will it be fixed or no

that's fixed in dev already

@Fyoncle
Copy link

Fyoncle commented Jun 3, 2024

What about Create compatibility? Will it be fixed or no

that's fixed in dev already

Oh yea just noticed, sorry. Seems like we need to create a config to fix it, thats probably now in Create's side since they marked the mod as broken.

@henkelmax
Copy link
Owner

This is out for a while now and it seems that there are no major issues, so I'll close this. Thanks again to everyone that helped with this issue!

@AClon314
Copy link

@Fyoncle

For anyone who want to do a quick fix: Fabricators-of-Create/Create#1503 (comment)

@tajemniktv
Copy link

tajemniktv commented Aug 22, 2024

This should already be fixed with 1.4.2

@henkelmax
Hey, oddly the issue might be back? Not sure, but the logs do seem similiar. The game freezes on loading into the server, but after (some, sometimes about 3minutes, it loads fine) https://mclo.gs/XXNEB5w

@henkelmax
Copy link
Owner

[14:44:05] [Sound engine/WARN]: [LevelAccessUtils#getClientLevelProxy] Can not return client level proxy, client level clone has not been cached. This might only occur once on load.

@augustsaintfreytag there might be an issue with the caching in combination with a certain mod.

@tajemniktv Could you check which mod in combination with SPR causes the issue?

@Kirrasu
Copy link

Kirrasu commented Aug 25, 2024

So I've got the newest version of Create Fabric and Sound Physics Remastered and I'm getting a error that doesn't let the game start at all. I've tried troubleshooting and looking through this thread and one that was marked as spam and directed to this one, but I haven't found anything. I dunno what to do or if there is a version of either mod that will allow me to use the other.
image_2024-08-25_184054134

@embeddedt
Copy link
Author

Create may not have made a release removing the incompatible declaration yet.

@IThundxr
Copy link

Correct, use a dependency override like people have mentioned above or wait for the next patch

@jacklollz2
Copy link

How would I make this dependency override on quilt?

@Yummy-cookie
Copy link

forge create0.5.1h also has this bug

@Yummy-cookie
Copy link

forge create0.5.1h also has this bug

1.19.2-latest

@PeglinX
Copy link

PeglinX commented Sep 16, 2024

forge create0.5.1h also has this bug

1.19.2-latest

It is known, create only has to update now for it to work. There’s a temporal fix as stated above by making an override file

@Yummy-cookie
Copy link

forge create0.5.1h也有这个bug

1.19.2-最新

众所周知,create 现在只需更新即可工作。如上所述,可以通过创建覆盖文件来临时修复

how?

@PeglinX
Copy link

PeglinX commented Sep 16, 2024

I’m not sure. Check the messages above and see if one of the possible solutions apply to your case

@Yummy-cookie
Copy link

我不确定。检查上面的消息,看看是否有一个可能的解决方案适用于你的情况

I looked and didn't find a way to forge

@henkelmax
Copy link
Owner

I believe Forge doesn't have a similar thing to dependency overrides.

@augustsaintfreytag
Copy link

augustsaintfreytag commented Sep 16, 2024

Only just noticed the discussion here, is there a confirmed deadlock issue in SPR? I'd say it could only happen in the caching logic, the only part that happens on the main thread. And it's not like building an initial cache would take longer than any future pass. I personally haven't encountered anything suspicious yet and I do play with quite a number of mods. If people are having issues (@KostromDan), I could take a look, though, if this happens in SPR > 1.4.0.

@tajemniktv If this is actually a server log, none of the sound physics code should actually be running server-side, so that'd be the heart of the issue. That line is logged if a sound is played before the main thread mixin had a chance to cache a radius of the client world which it should always do the first tick. If this runs server-side, there never would be a cache to access so I can picture this just being logged eternally, for every played sound. Not sure why it would cause a lock-up, though. I see this is 1.21.x, there may have been changes I don't know about, haven't touched that version.

On the Create topic, at least on 1.20.1 releases for Fabric, the compatibility rule has been updated for months already, we've had maintainers of Create in this thread following along. I can only imagine this being a problem for older versions or forks of Create.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests