Reentrancy issue if async functions get callback called before yield.
@carlosg
Submitted by Carlos Garnacho Link to original bug (#789249)
Description
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.":
testcase.vala