Skip to content

Async code on the same thread corrupts async state causing scripts to restart #65

Open
@Arakade

Description

@Arakade

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 with SINGLE_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 own SynchronizationContext 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 SynchronizationContexts better than me could probably write one that does the same job.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions