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

holepunch: s.holePuncher is nil on close #1448

Closed
Tracked by #1371
watjurk opened this issue Apr 23, 2022 · 3 comments · Fixed by #1473
Closed
Tracked by #1371

holepunch: s.holePuncher is nil on close #1448

watjurk opened this issue Apr 23, 2022 · 3 comments · Fixed by #1473
Labels
help wanted Seeking public contribution on this issue kind/bug A bug in existing code (including security flaws)

Comments

@watjurk
Copy link
Contributor

watjurk commented Apr 23, 2022

Sys info

go-libp2p@v0.19.0

Issue

When closing holepuncher it panics when it hadn't been created yet. Let me explain:

When holepuncher is closed, then the s.holePuncher is closed no matter what:

// Close closes the Hole Punch Service.
func (s *Service) Close() error {
err := s.holePuncher.Close()
s.tracer.Close()
s.host.RemoveStreamHandler(Protocol)
s.ctxCancel()
s.refCount.Wait()
return err
}

It's worth noting that s.holePuncher is a pointer that is nil by default:

type Service struct {
ctx context.Context
ctxCancel context.CancelFunc
host host.Host
ids identify.IDService
holePuncher *holePuncher
hasPublicAddrsChan chan struct{}
tracer *tracer
refCount sync.WaitGroup
}

And that s.holePuncher is only initialized in s.watchForPublicAddr func when some conditions are met:

s.holePuncher = newHolePuncher(s.host, s.ids, s.tracer)

This is a problem because if the s.holePuncher isn't initalized and we would call s.Close then s.holePuncher.Close() would panic (because it is not designed to be "zero value" closed).

Solution:

The simplest solution is to introduce a nil check when closing holepuncher, it will check if s.holePuncher is nil or not.

@Stebalien
Copy link
Member

Hm. This is also a race-condition.

@watjurk
Copy link
Contributor Author

watjurk commented Apr 24, 2022

Where is the race condition happening? I've run this code with --race enabled and no races were found.

@marten-seemann
Copy link
Contributor

s.holePuncher might be set just when you call Close. We might need to introduce a mutex here.

@marten-seemann marten-seemann added kind/bug A bug in existing code (including security flaws) help wanted Seeking public contribution on this issue labels Apr 25, 2022
@marten-seemann marten-seemann mentioned this issue Apr 25, 2022
65 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Seeking public contribution on this issue kind/bug A bug in existing code (including security flaws)
Projects
None yet
3 participants