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

Windows: net.Listen binds on the same port using tcp and tcp4 as network #45150

Closed
drakkan opened this issue Mar 21, 2021 · 3 comments
Closed

Comments

@drakkan
Copy link
Member

drakkan commented Mar 21, 2021

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

$ go version
go version go1.16.2 windows/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\vbox\AppData\Local\go-build
set GOENV=C:\Users\vbox\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\vbox\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\vbox\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.16.2
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\vbox\AppData\Local\Temp\go-build406273817=/tmp/go-build -gno-record-gcc-switches

What did you do?

please try the following reproducer

package main

import (
	"fmt"
	"net"
	"os"
)

func main() {
	listen, err := net.Listen("tcp", ":2024")
	if err != nil {
		fmt.Printf("Listen error: %s\n", err)
		os.Exit(1)
	} else {
		fmt.Printf("Listening tcp ...\n")
	}

	go func() {
		_, err := net.Listen("tcp4", ":2024")
		if err != nil {
			fmt.Printf("Listen tcp4 error: %v\n", err)
			return
		}
		fmt.Printf("Listen on tcp4 should fail!\n")
	}()

	coon, err := listen.Accept()
	if err != nil {
		fmt.Println("Accept() err=", err)
	} else {
		fmt.Printf("Accept() conn=%v\n client's remote address=%v\n", coon, coon.RemoteAddr())
	}
}

What did you expect to see?

On Linux and macOS I have this output:

$ go run main.go 
Listening tcp ...
Listen tcp4 error: listen tcp4 :2024: bind: address already in use

What did you see instead?

On Windows I have this output:

Z:\>go run main.go
Listening tcp ...
Listen on tcp4 should fail!
@seankhliao
Copy link
Member

Duplicate of #2307 #30291

@drakkan
Copy link
Member Author

drakkan commented Mar 21, 2021

Hi,

I saw the closed bugs and opened a new one as I can't comment there.

I know that using tcp4 is a workaround.

However it seems that using Go it is not possible to listen to both IPv4 and IPv6 and detect port conflicts with other programs (on Windows).

The use case is quite simple:

  • another program is running on a certain port
  • start your Go program using tcp as the address, since you want to listen on all available network stacks, and using the same port as the other running program
  • the Go program should not start on Windows the same way it does on *NIX

Other programming languages don't have the same problem, and Go behaves differently based on the operating system.

How can I prevent my Go program from starting if another program is running on the same port? Only on Windows do I have to try the binding also on tcp4 and quit my program if it fails? I would have expected such a workaround within Go itself if it is the only way.

This looks like something that should be fixed. Thank you

@seankhliao
Copy link
Member

Based on #7598 I would say this is working as intended

For questions please refer to https://github.com/golang/go/wiki/Questions

@golang golang locked and limited conversation to collaborators Mar 21, 2022
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

3 participants