p2p/discover: fix double lock

This commit is contained in:
Felix Lange 2024-04-26 14:04:19 +02:00
parent edb3a12a66
commit 1765712a83

View file

@ -369,12 +369,13 @@ loop:
tab.revalidation.handleResponse(tab, r) tab.revalidation.handleResponse(tab, r)
case op := <-tab.addNodeCh: case op := <-tab.addNodeCh:
tab.mutex.Lock()
tab.handleAddNode(op) tab.handleAddNode(op)
tab.mutex.Unlock()
tab.addNodeHandled <- struct{}{} tab.addNodeHandled <- struct{}{}
case op := <-tab.trackRequestCh: case op := <-tab.trackRequestCh:
tab.handleTrackRequest(op) tab.handleTrackRequest(op)
// TODO: handle failure by potentially dropping node
case <-refresh.C: case <-refresh.C:
if refreshDone == nil { if refreshDone == nil {
@ -492,6 +493,8 @@ func (tab *Table) removeIP(b *bucket, ip net.IP) {
b.ips.Remove(ip) b.ips.Remove(ip)
} }
// handleAddNode adds the node in the request to the table, if there is space.
// The caller must hold tab.mutex.
func (tab *Table) handleAddNode(req addNodeOp) { func (tab *Table) handleAddNode(req addNodeOp) {
if req.node.ID() == tab.self().ID() { if req.node.ID() == tab.self().ID() {
return return
@ -502,8 +505,6 @@ func (tab *Table) handleAddNode(req addNodeOp) {
return return
} }
tab.mutex.Lock()
defer tab.mutex.Unlock()
b := tab.bucket(req.node.ID()) b := tab.bucket(req.node.ID())
if tab.bumpInBucket(b, req.node.Node) { if tab.bumpInBucket(b, req.node.Node) {
// Already in bucket, update record. // Already in bucket, update record.