@@ -150,6 +150,8 @@ type worker struct {
150150 resubmitIntervalCh chan time.Duration
151151 resubmitAdjustCh chan * intervalAdjust
152152
153+ wg sync.WaitGroup
154+
153155 current * environment // An environment for current running cycle.
154156 localUncles map [common.Hash ]* types.Block // A set of side blocks generated locally as the possible uncle blocks.
155157 remoteUncles map [common.Hash ]* types.Block // A set of side blocks as the possible uncle blocks.
@@ -225,6 +227,7 @@ func newWorker(config *Config, chainConfig *params.ChainConfig, engine consensus
225227 recommit = minRecommitInterval
226228 }
227229
230+ worker .wg .Add (4 )
228231 go worker .mainLoop ()
229232 go worker .newWorkLoop (recommit )
230233 go worker .resultLoop ()
@@ -323,6 +326,7 @@ func (w *worker) close() {
323326 }
324327 atomic .StoreInt32 (& w .running , 0 )
325328 close (w .exitCh )
329+ w .wg .Wait ()
326330}
327331
328332// recalcRecommit recalculates the resubmitting interval upon feedback.
@@ -349,6 +353,7 @@ func recalcRecommit(minRecommit, prev time.Duration, target float64, inc bool) t
349353
350354// newWorkLoop is a standalone goroutine to submit new mining work upon received events.
351355func (w * worker ) newWorkLoop (recommit time.Duration ) {
356+ defer w .wg .Done ()
352357 var (
353358 interrupt * int32
354359 minRecommit = recommit // minimal resubmit interval specified by user.
@@ -446,6 +451,7 @@ func (w *worker) newWorkLoop(recommit time.Duration) {
446451
447452// mainLoop is a standalone goroutine to regenerate the sealing task based on the received event.
448453func (w * worker ) mainLoop () {
454+ defer w .wg .Done ()
449455 defer w .txsSub .Unsubscribe ()
450456 defer w .chainHeadSub .Unsubscribe ()
451457 defer w .chainSideSub .Unsubscribe ()
@@ -548,6 +554,7 @@ func (w *worker) mainLoop() {
548554// taskLoop is a standalone goroutine to fetch sealing task from the generator and
549555// push them to consensus engine.
550556func (w * worker ) taskLoop () {
557+ defer w .wg .Done ()
551558 var (
552559 stopCh chan struct {}
553560 prev common.Hash
@@ -595,6 +602,7 @@ func (w *worker) taskLoop() {
595602// resultLoop is a standalone goroutine to handle sealing result submitting
596603// and flush relative data to the database.
597604func (w * worker ) resultLoop () {
605+ defer w .wg .Done ()
598606 for {
599607 select {
600608 case block := <- w .resultCh :
0 commit comments