goja_nodejs icon indicating copy to clipboard operation
goja_nodejs copied to clipboard

evenloop.Run hang with setInterval

Open monkeyWie opened this issue 2 years ago • 3 comments
trafficstars

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?

monkeyWie avatar Nov 24 '23 16:11 monkeyWie

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.

bubbajoe avatar Feb 11 '24 14:02 bubbajoe

Isn't this expected behavior? If there's a lingering setInterval, it would wouldn't exit in Node either.

matthewmueller avatar Apr 20 '24 19:04 matthewmueller

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.

monkeyWie avatar May 06 '24 02:05 monkeyWie