gopherjs / gopherjs/gopherjs

Blocking in a deferred function in a promise crashes

Open
#1,211 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Go
Stars
13.2k
Forks
573
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproduction in main.inc.js and main.go, then inspect the generated test.js stack around $runScheduled, $schedule, queueEntry, and $send. The fix is complete when the deferred function in the promise callback no longer causes a TypeError and the promise chain handles the callback successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, javascript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.