Skip to content

Commit

Permalink
fix: example to clean up watchdog goroutine (#431)
Browse files Browse the repository at this point in the history
Fix the example to clean up watchdog goroutine after either the timeout or the VM completes.

Co-authored-by: Steven Hartland <[email protected]>
  • Loading branch information
raidancampbell and stevenh authored Nov 29, 2022
1 parent ad88756 commit 4617108
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4617108

Please sign in to comment.