Skip to content

Commit

Permalink
Close transport or listener just once
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo committed Nov 17, 2022
1 parent d8be6e3 commit 655117d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
8 changes: 7 additions & 1 deletion p2p/net/swarm/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,14 @@ func (s *Swarm) close() {
s.transports.m = nil
s.transports.Unlock()

var wg sync.WaitGroup
// Dedup transports that may be listening on multiple protocols
transportsToClose := make(map[transport.Transport]struct{}, len(transports))
for _, t := range transports {
transportsToClose[t] = struct{}{}
}

var wg sync.WaitGroup
for t := range transportsToClose {
if closer, ok := t.(io.Closer); ok {
wg.Add(1)
go func(c io.Closer) {
Expand Down
6 changes: 3 additions & 3 deletions p2p/net/swarm/swarm_listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (s *Swarm) Listen(addrs ...ma.Multiaddr) error {
// and address with `/quic`, then the QUIC listener will close and also close
// any `/quic-v1` address.
func (s *Swarm) ListenClose(addrs ...ma.Multiaddr) {
var listenersToClose []transport.Listener
listenersToClose := make(map[transport.Listener]struct{}, len(addrs))

s.listeners.Lock()
for l := range s.listeners.m {
Expand All @@ -52,12 +52,12 @@ func (s *Swarm) ListenClose(addrs ...ma.Multiaddr) {
}

delete(s.listeners.m, l)
listenersToClose = append(listenersToClose, l)
listenersToClose[l] = struct{}{}
}
s.listeners.cacheEOL = time.Time{}
s.listeners.Unlock()

for _, l := range listenersToClose {
for l := range listenersToClose {
l.Close()
}
}
Expand Down
8 changes: 1 addition & 7 deletions p2p/transport/quic/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,6 @@ type transport struct {

connMx sync.Mutex
conns map[quic.Connection]*conn

closeOnce sync.Once
}

var _ tpt.Transport = &transport{}
Expand Down Expand Up @@ -489,9 +487,5 @@ func (t *transport) String() string {
}

func (t *transport) Close() error {
// We may register multiple protocols, but they are all the same transport, so only support getting closed once.
t.closeOnce.Do(func() {
_ = t.connManager.Close()
})
return nil
return t.connManager.Close()
}

0 comments on commit 655117d

Please sign in to comment.