diff --git a/p2p/discover/table.go b/p2p/discover/table.go index 7781b5df6b..8f41f9793e 100644 --- a/p2p/discover/table.go +++ b/p2p/discover/table.go @@ -753,6 +753,11 @@ func (tab *Table) deleteNode(n *enode.Node) { // waitForNodes blocks until the table contains at least n nodes. func (tab *Table) waitForNodes(ctx context.Context, n int) error { + // done lets the forwarder goroutine exit when waitForNodes returns + // successfully (without ctx cancellation or table close). + done := make(chan struct{}) + defer close(done) + // Set up a notification channel that gets unblocked when there was any activity on // the table. Ultimately this reads from the table's nodeFeed, but can't use the feed // directly on the same goroutine that takes Table.mutex, it would deadlock. @@ -777,6 +782,8 @@ func (tab *Table) waitForNodes(ctx context.Context, n int) error { case <-tab.closeReq: notifyErr = errClosed return + case <-done: + return } } }()