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 natManager to close natManager.nat #1468

Merged
merged 3 commits into from
Jun 15, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions p2p/host/basic/natmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewNATManager(net network.Network) NATManager {
// * closing the natManager closes the nat and its mappings.
type natManager struct {
net network.Network
natmu sync.RWMutex
natMx sync.RWMutex
Stebalien marked this conversation as resolved.
Show resolved Hide resolved
nat *inat.NAT

ready chan struct{} // closed once the nat is ready to process port mappings
Expand Down Expand Up @@ -65,7 +65,13 @@ func newNatManager(net network.Network) *natManager {
func (nmgr *natManager) Close() error {
nmgr.ctxCancel()
nmgr.refCount.Wait()
return nil
var err error
nmgr.natMx.Lock()
if nmgr.nat != nil {
err = nmgr.nat.Close()
}
nmgr.natMx.Unlock()
watjurk marked this conversation as resolved.
Show resolved Hide resolved
return err
}

// Ready returns a channel which will be closed when the NAT has been found
Expand All @@ -86,9 +92,9 @@ func (nmgr *natManager) background(ctx context.Context) {
return
}

nmgr.natmu.Lock()
nmgr.natMx.Lock()
nmgr.nat = natInstance
nmgr.natmu.Unlock()
nmgr.natMx.Unlock()
close(nmgr.ready)

// sign natManager up for network notifications
Expand Down Expand Up @@ -207,8 +213,8 @@ func (nmgr *natManager) doSync() {
// (a) the search process is still ongoing, or (b) the search process
// found no nat. Clients must check whether the return value is nil.
func (nmgr *natManager) NAT() *inat.NAT {
nmgr.natmu.Lock()
defer nmgr.natmu.Unlock()
nmgr.natMx.Lock()
defer nmgr.natMx.Unlock()
return nmgr.nat
}

Expand Down