Skip to content
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

syscall/js: Behavior for handling rejected promises changed between 1.16 and 1.17 #51041

Closed
Sean-Der opened this issue Feb 6, 2022 · 1 comment

Comments

@Sean-Der
Copy link

Sean-Der commented Feb 6, 2022

What version of Go are you using (go version)?

$ go version go1.17 linux/amd64

Does this issue reproduce with the latest release?

Yes. I haven't checked gotip yet.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/sean/.cache/go-build"
GOENV="/home/sean/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/sean/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/sean/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/opt/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/opt/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.17"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build27322006=/tmp/go-build -gno-record-gcc-switches"

What did you do?

In 1.16 and earlier I could use a promise based API and use then and catch. Starting with 1.17 the process just exits.

What did you expect to see?

root@659cea540963:/home/sean/go/src/wasm# node --version
v12.22.5

root@659cea540963:/home/sean/go/src/wasm# go version
go version go1.16.13 linux/amd64

root@659cea540963:/home/sean/go/src/wasm# GOOS=js GOARCH=wasm go build -o  wasm.wasm

root@659cea540963:/home/sean/go/src/wasm# node --require=./node_shim.js $(go env GOROOT)/misc/wasm/wasm_exec.js wasm.wasm
Promise Blocking
(node:1259) UnhandledPromiseRejectionWarning: TypeError: Expected an object
    at RTCPeerConnection.setRemoteDescription (/home/sean/go/src/wasm/node_modules/wrtc/lib/peerconnection.js:300:26)
    at syscall/js.valueCall (/usr/local/go/misc/wasm/wasm_exec.js:399:31)
    at syscall_js.valueCall (wasm-function[1306]:0x128df5)
    at syscall_js.Value.Call (wasm-function[1291]:0x126e9d)
    at main.main (wasm-function[1406]:0x14822d)
    at runtime.main (wasm-function[500]:0x75408)
    at wasm_pc_f_loop (wasm-function[948]:0xd12e1)
    at wasm_export_run (wasm-function[946]:0xd12b4)
    at global.Go.run (/usr/local/go/misc/wasm/wasm_exec.js:574:23)
    at /usr/local/go/misc/wasm/wasm_exec.js:627:14
(node:1259) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:1259) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Promise Unblocked

What did you see instead?

sean@SeanLaptop:~/go/src/wasm$ node --version
v16.13.2
sean@SeanLaptop:~/go/src/wasm$ go version
go version go1.17 linux/amd64
sean@SeanLaptop:~/go/src/wasm$ GOOS=js GOARCH=wasm go build -o  wasm.wasm

sean@SeanLaptop:~/go/src/wasm$ node --require=./node_shim.js $(go env GOROOT)/misc/wasm/wasm_exec.js wasm.wasm
Promise Blocking
/home/sean/go/src/wasm/node_modules/wrtc/lib/peerconnection.js:300
  var promise = this._pc.setRemoteDescription(description);
                         ^

TypeError: Expected an object
    at RTCPeerConnection.setRemoteDescription (/home/sean/go/src/wasm/node_modules/wrtc/lib/peerconnection.js:300:26)
    at syscall/js.valueCall (/opt/go/misc/wasm/wasm_exec.js:399:31)
    at syscall_js.valueCall (wasm://wasm/007b4f9e:wasm-function[1197]:0x113954)
    at syscall_js.Value.Call (wasm://wasm/007b4f9e:wasm-function[1182]:0x111a0c)
    at main.main (wasm://wasm/007b4f9e:wasm-function[1293]:0x13216c)
    at runtime.main (wasm://wasm/007b4f9e:wasm-function[541]:0x7a30b)
    at wasm_pc_f_loop (wasm://wasm/007b4f9e:wasm-function[955]:0xd29d3)
    at wasm_export_run (wasm://wasm/007b4f9e:wasm-function[953]:0xd29a6)
    at global.Go.run (/opt/go/misc/wasm/wasm_exec.js:570:23)
    at /opt/go/misc/wasm/wasm_exec.js:623:14

Code

package.json

{
  "dependencies": {
    "wrtc": "0.4.7",
    "request": "2.88.2"
  }
}

node_shim.js

const wrtc = require('wrtc')

global.window = {
  RTCPeerConnection: wrtc.RTCPeerConnection
}

global.RTCPeerConnection = wrtc.RTCPeerConnection

main.go

//go:build js && wasm
// +build js,wasm

package main

import (
        "fmt"
        "syscall/js"
)

func main() {
        promiseResult := make(chan interface{})
        peerConnection := js.Global().Get("window").Get("RTCPeerConnection").New(js.ValueOf(map[string]interface{}{}))

        promise := peerConnection.Call("setRemoteDescription", js.Undefined())
        thenFunc := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
                close(promiseResult)
                return nil
        })
        defer thenFunc.Release()
        promise.Call("then", thenFunc)

        catchFunc := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
                close(promiseResult)
                return nil
        })
        defer catchFunc.Release()
        promise.Call("catch", catchFunc)

        fmt.Println("Promise Blocking")
        <-promiseResult
        fmt.Println("Promise Unblocked")
}

go.mod

module wasm

go 1.17
@Sean-Der Sean-Der changed the title syscall/js: Behavior for handling promises changed between 1.16 and 1.17 syscall/js: Behavior for handling rejected promises changed between 1.16 and 1.17 Feb 7, 2022
@Sean-Der Sean-Der closed this as completed Feb 7, 2022
@Sean-Der
Copy link
Author

Sean-Der commented Feb 7, 2022

My mistake! This comes from a behavior change with the node.js upgrade. --unhandled-rejections=strict in v12 gets the same behavior.

@golang golang locked and limited conversation to collaborators Feb 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants