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:
parent
ad8875609a
commit
4617108200
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue
Block a user