quickjs-go icon indicating copy to clipboard operation
quickjs-go copied to clipboard

Error ignored / infinite hang with async function in Promise constructor

Open yurivish opened this issue 7 months ago • 1 comments

I was playing around and noticed another funny behavior... Documenting it here in case it's helpful! (Sorry for all the issues 😄)

Minimal example:

package main

import (
	"fmt"

	"github.com/buke/quickjs-go"
)

func main() {
	rt := quickjs.NewRuntime(quickjs.WithExecuteTimeout(1))
	ctx := rt.NewContext()
	fmt.Println(ctx.LoadModule(`
		export default 123;
		//  This "async"  VVVVV  causes this code to hang forever.
		await new Promise(async (res, rej) => {
		  // This line should error.
		  // Try removing the "async" to see.
		  undefinedVariable(3) 
		  res(true)
		});
	`, "mod"))
}

While I don't know why anybody would use this pattern intentionally, is legal code, and in my testing, Chrome throws the error and shows in the console.

As far as I can tell, this is not an encouraged pattern of Promise usage since exceptions inside the callback will not be properly handled, but can be helpful if you want to use eg. for await loops inside of a promise.

And I think hanging forever is probably not the most useful behavior here - if you replace the error line with while(true); then the execution limit still does not trigger.

yurivish avatar Jun 18 '25 02:06 yurivish

yeah, I track the code ,it will hanging forever in https://github.com/buke/quickjs-go/blob/1189365c62de07db3a85015132ab467600e38b18/bridge.c#L587

it seems quicks C js_std_await function loop for ever, maybe should report to https://github.com/bellard/quickjs

buke avatar Jun 18 '25 08:06 buke