Reentrancy issue if async functions get callback called before yield.
Submitted by Carlos Garnacho
Created attachment 361955 testcase showing the error.
The way valac works, the same function (suffixed _co()) handles both the first steps of the async function and the after effect, it just increments data->state to 1 right before returning for the next time it's called.
There is a problem though if the async method manages to be completed before the yield call (eg. because of it being quickly dispatched in another thread). The data->state change happens after the callback function is called, so it enters again on the _co() function with state == 0, which does the first part of the function again.
It would be expected that at the time of reentering the _co() function, state is 1 so it does handle the second part inside the first one, doing data->state = 1 early in the _state0: path seems a thread safe way to get this.
I'm attaching a simple test case that asserts on the async foo() method if the portions before the yield get called more than once.
Attachment 361955, "testcase showing the error.":