RE-UE4SS icon indicating copy to clipboard operation
RE-UE4SS copied to clipboard

[BUG - 2.x] Lua Async APIs cause crashes

Open noita-player opened this issue 1 year ago • 1 comments

Branch or Release main 2.x

Game and Engine Version all

Describe the bug Currently async APIs will execute the same lua_state in a separate OS thread. Two instances of lua_newthread for the same lua_state cannot be safely used from two OS threads, as you corrupt memory if a simultaneous allocation, GC occurs, or access occurs.

All async APIs will eventually crash because of this. lua_lock is not sufficient, and all places where LuaMod runs the lua VM async or not needs to hold a shared lock. This may introduce stuttering for fast-running async tasks as the game thread waits for it in processevent. Async tasks could be turned into things executed at the start of each processevent if the time the task is registered for is past a certain delta. Long-running tasks will still cause hitches, but this is unavoidable.

To Reproduce For a given game, registerhook something frequent and LoopAsync, RegisterHotkeyAsync or ExecuteDeferred with a low delay. You will eventually crash.

noita-player avatar Jul 26 '23 16:07 noita-player

The current plan for this is to leave it alone in 2.X, and we'll make breaking changes in 3.0 to fix this. The changes will likely be switching as much stuff as possible to execute within the game thread, using queues if necessary, and functions that are explicitly async will not have direct access to any variables defined at file/global scope but hopefully we'll have a safe API to use for variable access.

UE4SS avatar Jul 26 '23 21:07 UE4SS