Skip to content

Instantly share code, notes, and snippets.

@avidal
Created May 9, 2020 04:15
Show Gist options
  • Save avidal/4710a963fbc8a80feae84663473ea971 to your computer and use it in GitHub Desktop.
Save avidal/4710a963fbc8a80feae84663473ea971 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"time"
"github.com/bytecodealliance/wasmtime-go"
)
func prepare(wasmfile string) (*wasmtime.Store, *wasmtime.WasiInstance, *wasmtime.Module) {
config := wasmtime.NewConfig()
config.SetDebugInfo(true)
config.SetWasmMultiValue(true)
store := wasmtime.NewStore(wasmtime.NewEngineWithConfig(config))
wasm, err := wasmtime.Wat2Wasm(`
(module
(import "" "hello" (func $hello))
(func (export "run")
(call $hello))
)
`)
check(err)
module, err := wasmtime.NewModule(store, wasm)
check(err)
// These options ensure our wasm module can write to stdout/stderr
wasicfg := wasmtime.NewWasiConfig()
wasicfg.InheritStdout()
wasicfg.InheritStderr()
wasi, err := wasmtime.NewWasiInstance(store, wasicfg, "wasi_snapshot_preview1")
check(err)
return store, wasi, module
}
func main() {
store, wasi, module := prepare("bin/main.wasm")
_, _, _ = store, wasi, module
fmt.Println("Waiting..")
// Sleep for just over 2 minutes, which will get the runtime to force a GC
// Doesn't reliably work at 121 seconds, so I set to 130.
time.Sleep(130 * time.Second)
fmt.Println("And now...the crash.")
// And then do something else, letting the runtime force the GC
<-time.After(1 * time.Second)
// Note that the time.Sleep & time.After can be replaced with:
// http.ListenAndServe(":", nil)
// The end result is the same thing, in ~2 minutes the runtime forces a GC and we segfault
fmt.Println("Back up!")
}
func check(err error) {
if err != nil {
fmt.Printf("Got error: %s\n", err.Error())
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment