From fd040ec0d7ef36373d57303c7da4365bf1aecd7c Mon Sep 17 00:00:00 2001 From: Daniel Liu <139250065@qq.com> Date: Sat, 13 Sep 2025 10:38:34 +0800 Subject: [PATCH] core/tracing: add OnClose Trace Hook #29629 (#1474) --- core/blockchain.go | 4 ++++ core/tracing/hooks.go | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/core/blockchain.go b/core/blockchain.go index 145f49583b..0d0f64b57b 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1161,6 +1161,10 @@ func (bc *BlockChain) Stop() { bc.chainmu.Close() bc.wg.Wait() bc.saveData() + // Allow tracers to clean-up and release resources. + if bc.logger != nil && bc.logger.OnClose != nil { + bc.logger.OnClose() + } // Flush the collected preimages to disk if err := bc.stateCache.TrieDB().CommitPreimages(); err != nil { log.Error("Failed to commit trie preimages", "err", err) diff --git a/core/tracing/hooks.go b/core/tracing/hooks.go index 53fb07ac57..cb40d62598 100644 --- a/core/tracing/hooks.go +++ b/core/tracing/hooks.go @@ -115,6 +115,9 @@ type ( // BlockchainInitHook is called when the blockchain is initialized. BlockchainInitHook = func(chainConfig *params.ChainConfig) + // CloseHook is called when the blockchain closes. + CloseHook = func() + // BlockStartHook is called before executing `block`. // `td` is the total difficulty prior to `block`. BlockStartHook = func(event BlockEvent) @@ -177,6 +180,7 @@ type Hooks struct { OnGasChange GasChangeHook // Chain events OnBlockchainInit BlockchainInitHook + OnClose CloseHook OnBlockStart BlockStartHook OnBlockEnd BlockEndHook OnSkippedBlock SkippedBlockHook