Debian Codegraph @

gcc/testsuite/go.test/test/fixedbugs/issue38093.go

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