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

net: conn.WriteToUDP([]byte("not ok"), addr) #34758

Closed
gertcuykens opened this issue Oct 8, 2019 · 4 comments
Closed

net: conn.WriteToUDP([]byte("not ok"), addr) #34758

gertcuykens opened this issue Oct 8, 2019 · 4 comments

Comments

@gertcuykens
Copy link
Contributor

@gertcuykens gertcuykens commented Oct 8, 2019

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

$ go version
go version devel +ab00f89c27 Mon Oct 7 20:58:25 2019 +0000 darwin/amd64

Does this issue reproduce with the latest release?

Don't know. Using master see go version

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

OSX Mojave 10.14.6

go env Output
$ go env
O111MODULE=""
GOARCH="amd64"
GOBIN="/Users/gert/bin"
GOCACHE="/Users/gert/Library/Caches/go-build"
GOENV="/Users/gert/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/gert/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
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/dv/8tlwvjr91zjdyq4rk14lkkfm0000gn/T/go-build945555905=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

go build; ./example

What did you expect to see?

go build; ./udp 
listen address: 127.0.0.1:5127 
dial from: 127.0.0.1:50136 
dial to: 127.0.0.1:5127 
dial send: bytes=2 to=127.0.0.1:5127
listen received: bytes=2 from=127.0.0.1:50136
listen send: bytes=2 to=127.0.0.1:50136
dial received: bytes=2 from=127.0.0.1:5127

I expected using n, err := conn.WriteToUDP([]byte("not ok"), addr)
to have the same result as using n, err := conn.Write([]byte("ok"))

What did you see instead?

n, err := conn.WriteToUDP([]byte("not ok"), addr) get stuck trying to connect

go build; ./udp 
listen address: 127.0.0.1:5127 
dial from: 127.0.0.1:56423 
dial to: 127.0.0.1:5127 
dial send: bytes=0 to=127.0.0.1:5127
package main

import (
	"fmt"
	"net"
	"time"
)

func Listen() error {
	addr := &net.UDPAddr{Port: 5127, IP: net.ParseIP("127.0.0.1")}
	conn, err := net.ListenUDP("udp", addr)
	if err != nil {
		return err
	}
	defer conn.Close()

	if conn != nil {
		fmt.Printf("listen address: %s \n", conn.LocalAddr().String())
	}

	buffer := make([]byte, 1024)

	n, addr, err := conn.ReadFromUDP(buffer)
	fmt.Printf("listen received: bytes=%d from=%s\n", n, addr.String())

	n, err = conn.WriteToUDP([]byte("ok"), addr)
	fmt.Printf("listen send: bytes=%d to=%s\n", n, addr.String())

	return err
}

func Dial() error {
	addr := &net.UDPAddr{Port: 5127, IP: net.ParseIP("127.0.0.1")}
	conn, err := net.DialUDP("udp", nil, addr)
	if err != nil {
		return err
	}
	defer conn.Close()

	if conn != nil {
		fmt.Printf("dial from: %s \n", conn.LocalAddr().String())
		fmt.Printf("dial to: %s \n", conn.RemoteAddr().String())
	}

	n, err := conn.WriteToUDP([]byte("not ok"), addr)
	// n, err := conn.Write([]byte("ok"))
	fmt.Printf("dial send: bytes=%d to=%s\n", n, addr.String())

	buffer := make([]byte, 1024)
	n, addr, err = conn.ReadFromUDP(buffer)
	fmt.Printf("dial received: bytes=%d from=%s\n", n, addr.String())

	return err
}

func main() {
	go Listen()
	time.Sleep(1 * time.Second)
	err := Dial()
	if err != nil {
		panic(err)
	}
}

https://play.golang.org/p/8Fq1XyYTFs1

@bradfitz

This comment has been minimized.

Copy link
Member

@bradfitz bradfitz commented Oct 9, 2019

This isn't a great bug report.

You pasted a lot of code and said "all this code doesn't work".

You will likely get more attention on this if you use the recommended bug template (which you deleted) and tell us what you expected to see and what happened instead, and details about your system (which macOS version?), etc.

@bradfitz

This comment has been minimized.

Copy link
Member

@bradfitz bradfitz commented Oct 9, 2019

Here's almost certainly your problem, though:

	go Listen()
	err := Dial()

You have no coordination between starting to Listen and Dialing.

Your Dial might happen before you even start to Listen.

@gertcuykens

This comment has been minimized.

Copy link
Contributor Author

@gertcuykens gertcuykens commented Oct 9, 2019

Removed the first example above and added only the short version and a timeout between listen and Dial. So basically if you copy paste the code above and run it you can compare the result between

	n, err := conn.WriteToUDP([]byte("not ok get stuck"), addr) 
	// n, err := conn.Write([]byte("ok works perfect"))

Will add the rest of the template but in my opinion itsn't related to the issue but could be wrong. If I can make a guess I believe addr get changed under the hood and isn't trivial? If it had to do with coordination I still expected identical behavior between the two lines of code, not the second line always working and the first line always failing to connect.

https://play.golang.org/p/8Fq1XyYTFs1

@gertcuykens

This comment has been minimized.

Copy link
Contributor Author

@gertcuykens gertcuykens commented Oct 10, 2019

Thx Andrey Mirtchovski

err was write udp 127.0.0.1:59864->127.0.0.1:5127: use of WriteTo with pre-connected connection

closing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.