go-gtp
go-gtp copied to clipboard
fix: add lock at WriteTo
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
}