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

fix: add lock at WriteTo #303

Merged
merged 1 commit into from
Oct 23, 2024
Merged

Conversation

YouShengLiu
Copy link

@YouShengLiu YouShengLiu commented Oct 17, 2024

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
}

Copy link
Owner

@wmnsk wmnsk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

@wmnsk wmnsk merged commit f103b7b into wmnsk:master Oct 23, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants