go-gtp icon indicating copy to clipboard operation
go-gtp copied to clipboard

fix: add lock at WriteTo

Open YouShengLiu opened this issue 1 year ago • 0 comments

In my situation, I have two goroutines: one uses UPlaneConn.ListenAndServe to receive packets, and the other uses UPlaneConn.WriteTo to send packets. It reports a race condition warning when I execute the Go program with the -race option.

func (u *UPlaneConn) ListenAndServe(ctx context.Context) error {
	if u.pktConn == nil {
		var err error
		u.mu.Lock()
		u.pktConn, err = newPktConn(u.laddr)   // <- race here
		u.mu.Unlock()
		if err != nil {
			return err
		}
	}
	return u.listenAndServe(ctx)
}
func (u *UPlaneConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
	return u.pktConn.WriteToWithDSCPECN(p, addr, 0)  // <- race here
}

YouShengLiu avatar Oct 17 '24 04:10 YouShengLiu