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

relay: fix deadlock when closing #2171

Merged
merged 3 commits into from
Mar 5, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions p2p/host/relaysvc/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestReachabilityChangeEvent(t *testing.T) {
require.Eventually(
t,
func() bool { rmgr.mutex.Lock(); defer rmgr.mutex.Unlock(); return rmgr.relay == nil },
1*time.Second,
3*time.Second,
100*time.Millisecond,
"relay should be nil on private reachability")

Expand All @@ -45,7 +45,7 @@ func TestReachabilityChangeEvent(t *testing.T) {
require.Eventually(
t,
func() bool { rmgr.mutex.Lock(); defer rmgr.mutex.Unlock(); return rmgr.relay == nil },
1*time.Second,
3*time.Second,
100*time.Millisecond,
"relay should be nil on unknown reachability")

Expand Down
16 changes: 11 additions & 5 deletions p2p/protocol/circuitv2/relay/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type Relay struct {
constraints *constraints
scope network.ResourceScopeSpan
notifiee network.Notifiee
wg sync.WaitGroup

mx sync.Mutex
rsvp map[peer.ID]time.Time
Expand Down Expand Up @@ -98,24 +99,27 @@ func New(h host.Host, opts ...Option) (*Relay, error) {
h.SetStreamHandler(proto.ProtoIDv2Hop, r.handleStream)
r.notifiee = &network.NotifyBundle{DisconnectedF: r.disconnected}
h.Network().Notify(r.notifiee)

r.wg.Add(1)
go r.background()

return r, nil
}

func (r *Relay) Close() error {
r.mx.Lock()
defer r.mx.Unlock()
if !r.closed {
r.closed = true
r.mx.Unlock()

r.host.RemoveStreamHandler(proto.ProtoIDv2Hop)
r.host.Network().StopNotify(r.notifiee)
r.scope.Done()
r.cancel()
for p := range r.rsvp {
r.host.ConnManager().UntagPeer(p, "relay-reservation")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Where does the untagging happen now?

Copy link
Member Author

@sukunrt sukunrt Mar 5, 2023

Choose a reason for hiding this comment

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

r.wg.Wait()
return nil
}
r.mx.Unlock()
return nil
}

Expand Down Expand Up @@ -564,6 +568,8 @@ func (r *Relay) background() {
case <-ticker.C:
r.gc()
case <-r.ctx.Done():
r.gc()
r.wg.Done()
return
}
}
Expand All @@ -576,7 +582,7 @@ func (r *Relay) gc() {
now := time.Now()

for p, expire := range r.rsvp {
if expire.Before(now) {
if r.closed || expire.Before(now) {
delete(r.rsvp, p)
r.host.ConnManager().UntagPeer(p, "relay-reservation")
}
Expand Down