From f82b169b846be342dfc23261dd5dd9db3cb04b69 Mon Sep 17 00:00:00 2001 From: Csaba Kiraly Date: Fri, 8 May 2026 10:51:22 +0200 Subject: [PATCH] p2p/discover: simplify forwarder cleanup with context.WithCancel --- p2p/discover/table.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/p2p/discover/table.go b/p2p/discover/table.go index 8f41f9793e..016a2d1af3 100644 --- a/p2p/discover/table.go +++ b/p2p/discover/table.go @@ -753,10 +753,10 @@ 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) + // Wrap ctx so the forwarder goroutine exits when waitForNodes returns, + // regardless of whether the caller's ctx is canceled. + ctx, cancel := context.WithCancel(ctx) + defer cancel() // 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 @@ -782,8 +782,6 @@ func (tab *Table) waitForNodes(ctx context.Context, n int) error { case <-tab.closeReq: notifyErr = errClosed return - case <-done: - return } } }()