From b99ef2541f098d522e30ca7df58b27987a4b3998 Mon Sep 17 00:00:00 2001 From: Larry <92799281+brilliant-lx@users.noreply.github.com> Date: Wed, 15 Mar 2023 17:43:18 +0800 Subject: [PATCH] fix: crash on nil access when TxPool shutdown (#1353) --- miner/worker.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/miner/worker.go b/miner/worker.go index 599032c217..d44187f928 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -1126,7 +1126,12 @@ LOOP: // subscribe before fillTransactions txsCh := make(chan core.NewTxsEvent, txChanSize) sub := w.eth.TxPool().SubscribeNewTxsEvent(txsCh) - defer sub.Unsubscribe() + // if TxPool has been stopped, `sub` would be nil, it could happen on shutdown. + if sub == nil { + log.Info("commitWork SubscribeNewTxsEvent return nil") + } else { + defer sub.Unsubscribe() + } // Fill pending transactions from the txpool fillStart := time.Now() @@ -1196,7 +1201,9 @@ LOOP: } // if sub's channel if full, it will block other NewTxsEvent subscribers, // so unsubscribe ASAP and Unsubscribe() is re-enterable, safe to call several time. - sub.Unsubscribe() + if sub != nil { + sub.Unsubscribe() + } } // get the most profitable work bestWork := workList[0]