goja_nodejs
goja_nodejs copied to clipboard
evenloop.Run hang with setInterval
There is a setInterval that has not been cleared, it will be hang at evenloop.Run, see the code below:
func main() {
loop := eventloop.NewEventLoop()
var test goja.Callable
loop.Run(func(vm *goja.Runtime) {
vm.RunString(`
// Simulated users forget to clear setInterval
function leak(){
setInterval(() => {
},500)
}
async function test(){
leak()
return await new Promise((resolve, reject) => {
setTimeout(() => {
console.log('done')
resolve('done')
}, 1000)
})
}
`)
test, _ = goja.AssertFunction(vm.Get("test"))
})
var (
result goja.Value
err error
)
// hanged this line
loop.Run(func(vm *goja.Runtime) {
result, err = test(nil)
})
if err != nil {
panic(err)
}
if vp, ok := result.Export().(*goja.Promise); ok {
fmt.Println("result", vp.State(), vp.Result())
}
}
How do I deal with this?
My personal issues with this was setTimeout, so i'm not sure about setInterval; however, I think the design points to using something like RunOnLoop for something like this, but I didn't have any luck with that either.
I dealt with this issue by using the runtime outside of the callback function.
Isn't this expected behavior? If there's a lingering setInterval, it would wouldn't exit in Node either.
Isn't this expected behavior? If there's a lingering
setInterval, it would wouldn't exit in Node either.
Ya, but I can't got test() result, there's only one trick I can do with it now, check the promise state util it is finished, then stop the eventloop.