Commit 4b144088 authored by OptimismBot's avatar OptimismBot Committed by GitHub

Merge pull request #5341 from ethereum-optimism/fix-p2p-sync-wait

op-node: fix p2p sync client wait group problem
parents 32ad9bdf 2d39a090
...@@ -202,11 +202,18 @@ type SyncClient struct { ...@@ -202,11 +202,18 @@ type SyncClient struct {
results chan syncResult results chan syncResult
receivePayload receivePayloadFn
// resource context: all peers and mainLoop tasks inherit this, and start shutting down once resCancel() is called.
resCtx context.Context resCtx context.Context
resCancel context.CancelFunc resCancel context.CancelFunc
receivePayload receivePayloadFn // wait group: wait for the resources to close. Adding to this is only safe if the peersLock is held.
wg sync.WaitGroup wg sync.WaitGroup
// Don't allow anything to be added to the wait-group while, or after, we are shutting down.
// This is protected by peersLock.
closingPeers bool
} }
func NewSyncClient(log log.Logger, cfg *rollup.Config, newStream newStreamFn, rcv receivePayloadFn, metrics SyncClientMetrics) *SyncClient { func NewSyncClient(log log.Logger, cfg *rollup.Config, newStream newStreamFn, rcv receivePayloadFn, metrics SyncClientMetrics) *SyncClient {
...@@ -239,7 +246,9 @@ func NewSyncClient(log log.Logger, cfg *rollup.Config, newStream newStreamFn, rc ...@@ -239,7 +246,9 @@ func NewSyncClient(log log.Logger, cfg *rollup.Config, newStream newStreamFn, rc
} }
func (s *SyncClient) Start() { func (s *SyncClient) Start() {
s.peersLock.Lock()
s.wg.Add(1) s.wg.Add(1)
s.peersLock.Unlock()
go s.mainLoop() go s.mainLoop()
} }
...@@ -250,6 +259,9 @@ func (s *SyncClient) AddPeer(id peer.ID) { ...@@ -250,6 +259,9 @@ func (s *SyncClient) AddPeer(id peer.ID) {
s.log.Warn("cannot register peer for sync duties, peer was already registered", "peer", id) s.log.Warn("cannot register peer for sync duties, peer was already registered", "peer", id)
return return
} }
if s.closingPeers {
return
}
s.wg.Add(1) s.wg.Add(1)
// add new peer routine // add new peer routine
ctx, cancel := context.WithCancel(s.resCtx) ctx, cancel := context.WithCancel(s.resCtx)
...@@ -269,7 +281,12 @@ func (s *SyncClient) RemovePeer(id peer.ID) { ...@@ -269,7 +281,12 @@ func (s *SyncClient) RemovePeer(id peer.ID) {
delete(s.peers, id) delete(s.peers, id)
} }
// Close will shut down the sync client and all attached work, and block until shutdown is complete.
// This will block if the Start() has not created the main background loop.
func (s *SyncClient) Close() error { func (s *SyncClient) Close() error {
s.peersLock.Lock()
s.closingPeers = true
s.peersLock.Unlock()
s.resCancel() s.resCancel()
s.wg.Wait() s.wg.Wait()
return nil return nil
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment