Skip to content

net: Listen always uses nextPort() instead of the desired port on JS #31294

Open
@albrow

Description

@albrow

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

$ go version
go version go1.12.1 darwin/amd64

Since this bug has to do with compiling to WebAssembly and running in Node.js, here is my Node.js version too:

$ node --version
v11.7.0

Does this issue reproduce with the latest release?

Yes.

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

go env Output
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/alex/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/alex/programming/go"
GOPROXY=""
GORACE=""
GOROOT="/Users/alex/.go"
GOTMPDIR=""
GOTOOLDIR="/Users/alex/.go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/h1/vhbpm31925nd0whbv4qd8mr00000gn/T/go-build857099932=/tmp/go-build -gno-record-gcc-switches -fno-common"

However, it's worth noting that the bug only occurs when you set GOOS=js GOARCH=wasm.

What did you do?

Consider the following Go program:

package main

import (
	"fmt"
	"net"
)

func main() {
	ln, err := net.Listen("tcp", "127.0.0.1:8080")
	if err != nil {
		panic(err)
	}
	defer ln.Close()
	fmt.Println(ln.Addr())
}

Run the program with the following command (From the Go WebAssembly Wiki):

GOOS=js GOARCH=wasm go run -exec="$(go env GOROOT)/misc/wasm/go_js_wasm_exec" main.go

What did you expect to see?

The program should output 127.0.0.1:8080 since that is the address I passed to net.Listen.

If you run the program with go run main.go (not compiling to WebAssembly) you see the output that I would expect.

What did you see instead?

127.0.0.1:1

I believe the root of the problem is the socket function in net_fake.go. It always uses nextPort() instead of using the port in the laddr argument.

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsFixThe path to resolution is known, but the work has not been done.OS-JSarch-wasmWebAssembly issues

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions