-
Notifications
You must be signed in to change notification settings - Fork 3
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
Async code on the same thread corrupts async state causing scripts to restart #65
Comments
Pondering how to fix, I'm wondering if something like this might work. It seems to pass the test but I'll admit to not grokking the WattleScript source base or knowing all the implications :) |
This is a tricky one as lua states, and by extension both MoonSharp and WattleScript If it's not able to be modified to be non-allocating, I'd still be happy to accept this as a PR with the repro cleaned up, and some comments about it emulating unity's behaviours. |
OK. Thanks for reviewing it and replying. |
I just took a little look and wish to check my understanding of your concern re. allocation before proceeding. Potential 1 : extra async state machine allocation due to
|
TL;DR: Potential 1 + Potential 2 blocked path. It is mostly the allocation of the Changing the |
Hello again, @CallumDev I've been working on this more and think a different tack might work better. I changed to this idea after having implemented the zero-allocation blocking variant you asked for previously. Once I did that, I realised it would cause another problem -- deadlock. The project I'm working on specifically has async Lua code that awaits an async C# call which awaits a Lua call on the same VM! (message bus listeners) So I went back and looked into the original cause of the problem. That might be a sufficient solution but I suspect there are scenarios wherein other data could be corrupted (probably involving some interleaving of asyncs and delays in 2 methods). I feel it would be better to separate all async calls to their own What do you think? |
An initial implementation is surprisingly simple (see attached). I'd say it needs some multi-threading protection (since What else might need considering?
LMK. I'll work on this some more tomorrow GMT day-time. The single-threaded variant is sufficient for my project's needs (since we're in that single-threaded Simple__async_per_Processor__implementation___(no_MT_protection).patch |
We agree the VM code is not thread safe. We therefore need to lock/block entry to all Additionally running All of this suggests we must:
Does this sound reasonable? |
Provisional version that largely works (passes all new async tests but fails 22 existing tests). Switched from LMK any thoughts. |
This latest patch doesn't apply cleanly against 50e7a07, making it a bit tricky to test |
We have script A with 2 functions A1 and A2. A1 calls something async and awaits it. Before the await completes, something else calls A2. When A1 completes, rather than resuming A1, the whole script (A) runs from the beginning!
Reproduction
I've attached a zip with 2 files. There's a unit test to paste at the bottom of AsyncTests unit test file that attempts to demonstrate the problem. Define
SINGLE_THREAD
to see the problem.AsyncTest-repro.zip
Investigation of unit test
Both WattleScript and MoonSharp detect 2 threads trying to enter the same Lua VM and reject. For this reason, the problem doesn't happen for default command-line invocation. This is what you see without
SINGLE_THREAD
defined.The reproduction only happens when not using a thread-pool based
SynchronizationContext
. (Shown withSINGLE_THREAD
defined.) Specifically we use WattleScript with async to run Lua "mod" code in our WIP Unity game with the "auto await" facility enabled. Unity has its ownSynchronizationContext
that runs everything on the same thread.N.b. The test case contains a hacked-up version of the Unity sync context so I'm not proposing it as a PR to show the problem. Someone who understands
SynchronizationContext
s better than me could probably write one that does the same job.The text was updated successfully, but these errors were encountered: