Skip to content

Commit

Permalink
Fix deadlocking panic when checking for v2 upgrade when receiving han…
Browse files Browse the repository at this point in the history
…dshake
  • Loading branch information
anacrolix committed Mar 22, 2024
1 parent 2396e6c commit 93f3f6f
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,13 +603,12 @@ func (cl *Client) incomingConnection(nc net.Conn) {
network: nc.RemoteAddr().Network(),
connString: regularNetConnPeerConnConnString(nc),
})
defer func() {
cl.lock()
defer cl.unlock()
c.close()
}()
c.Discovery = PeerSourceIncoming
cl.runReceivedConn(c)

cl.lock()
c.close()
cl.unlock()
}

// Returns a handle to the given torrent, if it's present in the client.
Expand Down Expand Up @@ -1014,13 +1013,15 @@ func (cl *Client) receiveHandshakes(c *PeerConn) (t *Torrent, err error) {
if err != nil {
return nil, fmt.Errorf("during bt handshake: %w", err)
}

cl.lock()
t = cl.torrentsByShortHash[ih]
if t.infoHashV2.Ok && *t.infoHashV2.Value.ToShort() == ih {
if t != nil && t.infoHashV2.Ok && *t.infoHashV2.Value.ToShort() == ih {
torrent.Add("v2 handshakes received", 1)
c.v2 = true
}
cl.unlock()

return
}

Expand Down

0 comments on commit 93f3f6f

Please sign in to comment.