Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Commit

Permalink
feat: close transports that implement io.Closer
Browse files Browse the repository at this point in the history
This way, transports with shared resources (e.g., reused sockets) can clean them
up.

fixes libp2p/go-libp2p#999
  • Loading branch information
Stebalien committed Aug 28, 2020
1 parent 58c167a commit 20a1064
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"io"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -176,6 +177,20 @@ func (s *Swarm) teardown() error {
// Wait for everything to finish.
s.refs.Wait()

// Now close out any transports (if necessary).
s.transports.Lock()
transports := s.transports.m
s.transports.m = nil
s.transports.Unlock()

for t := range transports {
if closer, ok := t.(io.Closer); ok {
if err := closer.Close(); err != nil {
log.Errorf("error when closing down transport: %s", err)
}
}
}

return nil
}

Expand Down

0 comments on commit 20a1064

Please sign in to comment.