1// run
2
3//go:build js
4
5// Copyright 2020 The Go Authors. All rights reserved.
6// Use of this source code is governed by a BSD-style
7// license that can be found in the LICENSE file.
8
9// Test race condition between timers and wasm calls that led to memory corruption.
10
11package main
12
13import (
14	"os"
15	"syscall/js"
16	"time"
17)
18
19func main() {
20	ch1 := make(chan struct{})
21
22	go func() {
23		for {
24			time.Sleep(5 * time.Millisecond)
25			ch1 <- struct{}{}
26		}
27	}()
28	go func() {
29		for {
30			time.Sleep(8 * time.Millisecond)
31			ch1 <- struct{}{}
32		}
33	}()
34	go func() {
35		time.Sleep(2 * time.Second)
36		os.Exit(0)
37	}()
38
39	for range ch1 {
40		ch2 := make(chan struct{}, 1)
41		f := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
42			ch2 <- struct{}{}
43			return nil
44		})
45		defer f.Release()
46		fn := js.Global().Get("Function").New("cb", "cb();")
47		fn.Invoke(f)
48		<-ch2
49	}
50}
51