Skip to content

Commit 7438f22

Browse files
authored
Merge pull request #9704 from ellemouton/ctx6
discovery+autopilot: revert passing contexts to `Start` methods
2 parents 225a694 + 41eee0c commit 7438f22

File tree

13 files changed

+45
-70
lines changed

13 files changed

+45
-70
lines changed

autopilot/agent.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,10 @@ func New(cfg Config, initialState []LocalChannel) (*Agent, error) {
202202

203203
// Start starts the agent along with any goroutines it needs to perform its
204204
// normal duties.
205-
func (a *Agent) Start(ctx context.Context) error {
205+
func (a *Agent) Start() error {
206206
var err error
207207
a.started.Do(func() {
208-
ctx, cancel := context.WithCancel(ctx)
208+
ctx, cancel := context.WithCancel(context.Background())
209209
a.cancel = fn.Some(cancel)
210210

211211
err = a.start(ctx)

autopilot/agent_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ func setup(t *testing.T, initialChans []LocalChannel) *testContext {
221221

222222
// With the autopilot agent and all its dependencies we'll start the
223223
// primary controller goroutine.
224-
if err := agent.Start(context.Background()); err != nil {
224+
if err := agent.Start(); err != nil {
225225
t.Fatalf("unable to start agent: %v", err)
226226
}
227227

autopilot/manager.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
"github.com/btcsuite/btcd/btcec/v2"
99
"github.com/btcsuite/btcd/wire"
10-
"github.com/lightningnetwork/lnd/fn/v2"
1110
graphdb "github.com/lightningnetwork/lnd/graph/db"
1211
"github.com/lightningnetwork/lnd/lnwallet"
1312
"github.com/lightningnetwork/lnd/lnwire"
@@ -55,9 +54,8 @@ type Manager struct {
5554
// disabled.
5655
pilot *Agent
5756

58-
quit chan struct{}
59-
wg sync.WaitGroup
60-
cancel fn.Option[context.CancelFunc]
57+
quit chan struct{}
58+
wg sync.WaitGroup
6159
sync.Mutex
6260
}
6361

@@ -83,7 +81,6 @@ func (m *Manager) Stop() error {
8381
log.Errorf("Unable to stop pilot: %v", err)
8482
}
8583

86-
m.cancel.WhenSome(func(fn context.CancelFunc) { fn() })
8784
close(m.quit)
8885
m.wg.Wait()
8986
})
@@ -100,16 +97,14 @@ func (m *Manager) IsActive() bool {
10097

10198
// StartAgent creates and starts an autopilot agent from the Manager's
10299
// config.
103-
func (m *Manager) StartAgent(ctx context.Context) error {
100+
func (m *Manager) StartAgent() error {
104101
m.Lock()
105102
defer m.Unlock()
106103

107104
// Already active.
108105
if m.pilot != nil {
109106
return nil
110107
}
111-
ctx, cancel := context.WithCancel(ctx)
112-
m.cancel = fn.Some(cancel)
113108

114109
// Next, we'll fetch the current state of open channels from the
115110
// database to use as initial state for the auto-pilot agent.
@@ -125,7 +120,7 @@ func (m *Manager) StartAgent(ctx context.Context) error {
125120
return err
126121
}
127122

128-
if err := pilot.Start(ctx); err != nil {
123+
if err := pilot.Start(); err != nil {
129124
return err
130125
}
131126

@@ -169,8 +164,6 @@ func (m *Manager) StartAgent(ctx context.Context) error {
169164
return
170165
case <-m.quit:
171166
return
172-
case <-ctx.Done():
173-
return
174167
}
175168
}
176169

@@ -241,8 +234,6 @@ func (m *Manager) StartAgent(ctx context.Context) error {
241234
return
242235
case <-m.quit:
243236
return
244-
case <-ctx.Done():
245-
return
246237
}
247238
}
248239
}()

discovery/gossiper.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -643,10 +643,10 @@ func (d *AuthenticatedGossiper) PropagateChanPolicyUpdate(
643643

644644
// Start spawns network messages handler goroutine and registers on new block
645645
// notifications in order to properly handle the premature announcements.
646-
func (d *AuthenticatedGossiper) Start(ctx context.Context) error {
646+
func (d *AuthenticatedGossiper) Start() error {
647647
var err error
648648
d.started.Do(func() {
649-
ctx, cancel := context.WithCancel(ctx)
649+
ctx, cancel := context.WithCancel(context.Background())
650650
d.cancel = fn.Some(cancel)
651651

652652
log.Info("Authenticated Gossiper starting")
@@ -674,11 +674,11 @@ func (d *AuthenticatedGossiper) start(ctx context.Context) error {
674674
// Start the reliable sender. In case we had any pending messages ready
675675
// to be sent when the gossiper was last shut down, we must continue on
676676
// our quest to deliver them to their respective peers.
677-
if err := d.reliableSender.Start(ctx); err != nil {
677+
if err := d.reliableSender.Start(); err != nil {
678678
return err
679679
}
680680

681-
d.syncMgr.Start(ctx)
681+
d.syncMgr.Start()
682682

683683
d.banman.start()
684684

discovery/gossiper_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,7 @@ func createTestCtx(t *testing.T, startHeight uint32, isChanPeer bool) (
994994
ScidCloser: newMockScidCloser(isChanPeer),
995995
}, selfKeyDesc)
996996

997-
if err := gossiper.Start(context.Background()); err != nil {
997+
if err := gossiper.Start(); err != nil {
998998
return nil, fmt.Errorf("unable to start router: %w", err)
999999
}
10001000

@@ -1692,7 +1692,7 @@ func TestSignatureAnnouncementRetryAtStartup(t *testing.T) {
16921692
KeyLocator: tCtx.gossiper.selfKeyLoc,
16931693
})
16941694
require.NoError(t, err, "unable to recreate gossiper")
1695-
if err := gossiper.Start(context.Background()); err != nil {
1695+
if err := gossiper.Start(); err != nil {
16961696
t.Fatalf("unable to start recreated gossiper: %v", err)
16971697
}
16981698
defer gossiper.Stop()

discovery/reliable_sender.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ func newReliableSender(cfg *reliableSenderCfg) *reliableSender {
7676
}
7777

7878
// Start spawns message handlers for any peers with pending messages.
79-
func (s *reliableSender) Start(ctx context.Context) error {
79+
func (s *reliableSender) Start() error {
8080
var err error
8181
s.start.Do(func() {
82-
ctx, cancel := context.WithCancel(ctx)
82+
ctx, cancel := context.WithCancel(context.Background())
8383
s.cancel = fn.Some(cancel)
8484

8585
err = s.resendPendingMsgs(ctx)

discovery/sync_manager.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"time"
99

1010
"github.com/btcsuite/btcd/chaincfg/chainhash"
11-
"github.com/lightningnetwork/lnd/fn/v2"
1211
"github.com/lightningnetwork/lnd/lnpeer"
1312
"github.com/lightningnetwork/lnd/lnwire"
1413
"github.com/lightningnetwork/lnd/routing/route"
@@ -201,9 +200,8 @@ type SyncManager struct {
201200
// number of queries.
202201
rateLimiter *rate.Limiter
203202

204-
wg sync.WaitGroup
205-
quit chan struct{}
206-
cancel fn.Option[context.CancelFunc]
203+
wg sync.WaitGroup
204+
quit chan struct{}
207205
}
208206

209207
// newSyncManager constructs a new SyncManager backed by the given config.
@@ -248,13 +246,10 @@ func newSyncManager(cfg *SyncManagerCfg) *SyncManager {
248246
}
249247

250248
// Start starts the SyncManager in order to properly carry out its duties.
251-
func (m *SyncManager) Start(ctx context.Context) {
249+
func (m *SyncManager) Start() {
252250
m.start.Do(func() {
253-
ctx, cancel := context.WithCancel(ctx)
254-
m.cancel = fn.Some(cancel)
255-
256251
m.wg.Add(1)
257-
go m.syncerHandler(ctx)
252+
go m.syncerHandler()
258253
})
259254
}
260255

@@ -264,7 +259,6 @@ func (m *SyncManager) Stop() {
264259
log.Debugf("SyncManager is stopping")
265260
defer log.Debugf("SyncManager stopped")
266261

267-
m.cancel.WhenSome(func(fn context.CancelFunc) { fn() })
268262
close(m.quit)
269263
m.wg.Wait()
270264

@@ -288,7 +282,7 @@ func (m *SyncManager) Stop() {
288282
// much of the public network as possible.
289283
//
290284
// NOTE: This must be run as a goroutine.
291-
func (m *SyncManager) syncerHandler(ctx context.Context) {
285+
func (m *SyncManager) syncerHandler() {
292286
defer m.wg.Done()
293287

294288
m.cfg.RotateTicker.Resume()
@@ -386,7 +380,7 @@ func (m *SyncManager) syncerHandler(ctx context.Context) {
386380
}
387381
m.syncersMu.Unlock()
388382

389-
s.Start(ctx)
383+
s.Start()
390384

391385
// Once we create the GossipSyncer, we'll signal to the
392386
// caller that they can proceed since the SyncManager's
@@ -538,9 +532,6 @@ func (m *SyncManager) syncerHandler(ctx context.Context) {
538532

539533
case <-m.quit:
540534
return
541-
542-
case <-ctx.Done():
543-
return
544535
}
545536
}
546537
}

discovery/sync_manager_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package discovery
22

33
import (
44
"bytes"
5-
"context"
65
"fmt"
76
"io"
87
"reflect"
@@ -83,7 +82,7 @@ func TestSyncManagerNumActiveSyncers(t *testing.T) {
8382
}
8483

8584
syncMgr := newPinnedTestSyncManager(numActiveSyncers, pinnedSyncers)
86-
syncMgr.Start(context.Background())
85+
syncMgr.Start()
8786
defer syncMgr.Stop()
8887

8988
// First we'll start by adding the pinned syncers. These should
@@ -135,7 +134,7 @@ func TestSyncManagerNewActiveSyncerAfterDisconnect(t *testing.T) {
135134

136135
// We'll create our test sync manager to have two active syncers.
137136
syncMgr := newTestSyncManager(2)
138-
syncMgr.Start(context.Background())
137+
syncMgr.Start()
139138
defer syncMgr.Stop()
140139

141140
// The first will be an active syncer that performs a historical sync
@@ -188,7 +187,7 @@ func TestSyncManagerRotateActiveSyncerCandidate(t *testing.T) {
188187

189188
// We'll create our sync manager with three active syncers.
190189
syncMgr := newTestSyncManager(1)
191-
syncMgr.Start(context.Background())
190+
syncMgr.Start()
192191
defer syncMgr.Stop()
193192

194193
// The first syncer registered always performs a historical sync.
@@ -236,7 +235,7 @@ func TestSyncManagerNoInitialHistoricalSync(t *testing.T) {
236235
t.Parallel()
237236

238237
syncMgr := newTestSyncManager(0)
239-
syncMgr.Start(context.Background())
238+
syncMgr.Start()
240239
defer syncMgr.Stop()
241240

242241
// We should not expect any messages from the peer.
@@ -270,7 +269,7 @@ func TestSyncManagerInitialHistoricalSync(t *testing.T) {
270269
t.Fatal("expected graph to not be considered as synced")
271270
}
272271

273-
syncMgr.Start(context.Background())
272+
syncMgr.Start()
274273
defer syncMgr.Stop()
275274

276275
// We should expect to see a QueryChannelRange message with a
@@ -339,7 +338,7 @@ func TestSyncManagerHistoricalSyncOnReconnect(t *testing.T) {
339338
t.Parallel()
340339

341340
syncMgr := newTestSyncManager(2)
342-
syncMgr.Start(context.Background())
341+
syncMgr.Start()
343342
defer syncMgr.Stop()
344343

345344
// We should expect to see a QueryChannelRange message with a
@@ -373,7 +372,7 @@ func TestSyncManagerForceHistoricalSync(t *testing.T) {
373372
t.Parallel()
374373

375374
syncMgr := newTestSyncManager(1)
376-
syncMgr.Start(context.Background())
375+
syncMgr.Start()
377376
defer syncMgr.Stop()
378377

379378
// We should expect to see a QueryChannelRange message with a
@@ -411,7 +410,7 @@ func TestSyncManagerGraphSyncedAfterHistoricalSyncReplacement(t *testing.T) {
411410
t.Parallel()
412411

413412
syncMgr := newTestSyncManager(1)
414-
syncMgr.Start(context.Background())
413+
syncMgr.Start()
415414
defer syncMgr.Stop()
416415

417416
// We should expect to see a QueryChannelRange message with a
@@ -469,7 +468,7 @@ func TestSyncManagerWaitUntilInitialHistoricalSync(t *testing.T) {
469468
// We'll start by creating our test sync manager which will hold up to
470469
// 2 active syncers.
471470
syncMgr := newTestSyncManager(numActiveSyncers)
472-
syncMgr.Start(context.Background())
471+
syncMgr.Start()
473472
defer syncMgr.Stop()
474473

475474
// We'll go ahead and create our syncers.

discovery/syncer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,11 +405,11 @@ func newGossipSyncer(cfg gossipSyncerCfg, sema chan struct{}) *GossipSyncer {
405405

406406
// Start starts the GossipSyncer and any goroutines that it needs to carry out
407407
// its duties.
408-
func (g *GossipSyncer) Start(ctx context.Context) {
408+
func (g *GossipSyncer) Start() {
409409
g.started.Do(func() {
410410
log.Debugf("Starting GossipSyncer(%x)", g.cfg.peerPub[:])
411411

412-
ctx, _ := g.cg.Create(ctx)
412+
ctx, _ := g.cg.Create(context.Background())
413413

414414
// TODO(conner): only spawn channelGraphSyncer if remote
415415
// supports gossip queries, and only spawn replyHandler if we

0 commit comments

Comments
 (0)