1
0
mirror of https://github.com/robertkrimen/otto synced 2025-10-05 19:19:10 +08:00

fix: example to clean up watchdog goroutine (#431)

Fix the example to clean up watchdog goroutine after either the timeout or the VM completes.

Co-authored-by: Steven Hartland <steven.hartland@multiplay.co.uk>
This commit is contained in:
R. Aidan Campbell 2022-11-28 17:08:50 -07:00 committed by GitHub
parent ad8875609a
commit 4617108200
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -236,12 +236,18 @@ func runUnsafe(unsafe string) {
vm := otto.New()
vm.Interrupt = make(chan func(), 1) // The buffer prevents blocking
watchdogCleanup := make(chan struct{})
defer close(watchdogCleanup)
go func() {
time.Sleep(2 * time.Second) // Stop after two seconds
vm.Interrupt <- func() {
panic(halt)
select {
case <-time.After(2 * time.Second): // Stop after two seconds
vm.Interrupt <- func() {
panic(halt)
}
case <-watchdogCleanup:
}
close(vm.Interrupt)
}()
vm.Run(unsafe) // Here be dragons (risky code)