Skip to content

Commit

Permalink
Revert "Do notifications synchronously"
Browse files Browse the repository at this point in the history
This reverts commit 6ee2ac3.
  • Loading branch information
anacrolix committed Feb 19, 2019
1 parent 76866ef commit c8c717c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
14 changes: 13 additions & 1 deletion p2p/net/swarm/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ func (s *Swarm) addConn(tc transport.Conn, dir inet.Direction) (*Conn, error) {
// * The other will be decremented when Conn.start exits.
s.refs.Add(2)

// Take the notification lock before releasing the conns lock to block
// Disconnect notifications until after the Connect notifications done.
c.notifyLk.Lock()
s.conns.Unlock()

// We have a connection now. Cancel all other in-progress dials.
Expand All @@ -217,6 +220,7 @@ func (s *Swarm) addConn(tc transport.Conn, dir inet.Direction) (*Conn, error) {
s.notifyAll(func(f inet.Notifiee) {
f.Connected(s, c)
})
c.notifyLk.Unlock()

c.start()

Expand Down Expand Up @@ -434,10 +438,18 @@ func (s *Swarm) Backoff() *DialBackoff {

// notifyAll sends a signal to all Notifiees
func (s *Swarm) notifyAll(notify func(inet.Notifiee)) {
var wg sync.WaitGroup

s.notifs.RLock()
wg.Add(len(s.notifs.m))
for f := range s.notifs.m {
notify(f)
go func(f inet.Notifiee) {
defer wg.Done()
notify(f)
}(f)
}

wg.Wait()
s.notifs.RUnlock()
}

Expand Down
15 changes: 11 additions & 4 deletions p2p/net/swarm/swarm_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,17 @@ func (c *Conn) doClose() {
s.Reset()
}

c.swarm.notifyAll(func(f inet.Notifiee) {
f.Disconnected(c.swarm, c)
})
c.swarm.refs.Done() // taken in Swarm.addConn
// do this in a goroutine to avoid deadlocking if we call close in an open notification.
go func() {
// prevents us from issuing close notifications before finishing the open notifications
c.notifyLk.Lock()
defer c.notifyLk.Unlock()

c.swarm.notifyAll(func(f inet.Notifiee) {
f.Disconnected(c.swarm, c)
})
c.swarm.refs.Done() // taken in Swarm.addConn
}()
}

func (c *Conn) removeStream(s *Stream) {
Expand Down

0 comments on commit c8c717c

Please sign in to comment.