Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Blocking in a deferred function in a promise crashes #1211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
flimzy opened this issue Jun 24, 2023 · 1 comment
Open

Blocking in a deferred function in a promise crashes #1211

flimzy opened this issue Jun 24, 2023 · 1 comment

Comments

@flimzy
Copy link
Member

flimzy commented Jun 24, 2023

Reproduction case:

main.inc.js:

$global.p = new Promise((resolve, reject) => {
    setTimeout(() => {
        resolve("foo");
    }, 300);
});

main.go:

package main

import (
	"fmt"

	"github.com/gopherjs/gopherjs/js"
)

func main() {
	p := js.Global.Get("p")
	p.Call("then", func(r *js.Object) {
		defer func() {
			fmt.Println("deferred")
		}()
	}).Call("catch", func(e *js.Object) {
	})
}

When I execute this code, I get the following output:

deferred
/home/jonhall/src/test/test.js:1737
            r();
            ^

TypeError: r is not a function
    at $runScheduled (/home/jonhall/src/test/test.js:1737:13)
    at $schedule (/home/jonhall/src/test/test.js:1761:9)
    at queueEntry (/home/jonhall/src/test/test.js:1834:9)
    at $send (/home/jonhall/src/test/test.js:1793:9)
    at $b (/home/jonhall/src/test/test.js:15011:9)
    at $b (/home/jonhall/src/test/test.js:14318:9)
    at /home/jonhall/src/test/test.js:68:74
    at FSReqCallback.wrapper [as oncomplete] (node:fs:816:5)

Hypothesis: The rule that blocking code in a callback must be executed in a goroutine should apply to deferred functions within callbacks as well. If I move the fmt.Println call to a goroutine within the deferred function, the error goes away.

@nevkontakte
Copy link
Member

Yes, I think your hypothesis is correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants