syscall/js: handle interleaved functions correctly

Because of concurrent goroutines it is possible for multiple event
handlers to return at the same time. This was not properly supported
and caused the wrong goroutine to continue, which in turn caused
memory corruption.

This change adds a stack of events so it is always clear which is the
innermost event that needs to return next.

Fixes #35256

Change-Id: Ia527da3b91673bc14e84174cdc407f5c9d5a3d09
Reviewed-on: https://go-review.googlesource.com/c/go/+/204662
Run-TryBot: Richard Musiol <neelance@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
This commit is contained in:
Richard Musiol 2019-11-02 21:20:24 +01:00 committed by Richard Musiol
parent 688aa74857
commit c2e2296dd7
2 changed files with 54 additions and 28 deletions

View file

@ -419,6 +419,25 @@ func TestInvokeFunction(t *testing.T) {
}
}
func TestInterleavedFunctions(t *testing.T) {
c1 := make(chan struct{})
c2 := make(chan struct{})
js.Global().Get("setTimeout").Invoke(js.FuncOf(func(this js.Value, args []js.Value) interface{} {
c1 <- struct{}{}
<-c2
return nil
}), 0)
<-c1
c2 <- struct{}{}
// this goroutine is running, but the callback of setTimeout did not return yet, invoke another function now
f := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
return nil
})
f.Invoke()
}
func ExampleFuncOf() {
var cb js.Func
cb = js.FuncOf(func(this js.Value, args []js.Value) interface{} {