From 915d9d9de5c54bac566f7a418dc994d49689f19e Mon Sep 17 00:00:00 2001 From: Delweng Date: Wed, 12 Mar 2025 19:20:40 +0800 Subject: [PATCH] cmd: ctrl-c to halt the whole import process (#31360) When I press Ctrl-C during the import of multiple files, the import process will still attempt to import the subsequent files. However, in normal circumstances, users would expect the import to stop immediately upon pressing Ctrl-C. And because the current file was not finished importing, subsequent import tasks often fail due to an `unknown ancestor` error. --------- Signed-off-by: jsvisa Co-authored-by: Felix Lange --- cmd/geth/chaincmd.go | 12 ++++++++---- cmd/utils/cmd.go | 5 ++++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/cmd/geth/chaincmd.go b/cmd/geth/chaincmd.go index eb0dda0952..c30927d9ff 100644 --- a/cmd/geth/chaincmd.go +++ b/cmd/geth/chaincmd.go @@ -103,11 +103,12 @@ if one is set. Otherwise it prints the genesis from the datadir.`, utils.StateHistoryFlag, }, utils.DatabaseFlags), Description: ` -The import command imports blocks from an RLP-encoded form. The form can be one file -with several RLP-encoded blocks, or several files can be used. +The import command allows the import of blocks from an RLP-encoded format. This format can be a single file +containing multiple RLP-encoded blocks, or multiple files can be given. -If only one file is used, import error will result in failure. If several files are used, -processing will proceed even if an individual RLP-file import failure occurs.`, +If only one file is used, an import error will result in the entire import process failing. If +multiple files are processed, the import process will continue even if an individual RLP file fails +to import successfully.`, } exportCommand = &cli.Command{ Action: exportChain, @@ -319,6 +320,9 @@ func importChain(ctx *cli.Context) error { if err := utils.ImportChain(chain, arg); err != nil { importErr = err log.Error("Import error", "file", arg, "err", err) + if err == utils.ErrImportInterrupted { + break + } } } } diff --git a/cmd/utils/cmd.go b/cmd/utils/cmd.go index de5918a722..33c93c2cc6 100644 --- a/cmd/utils/cmd.go +++ b/cmd/utils/cmd.go @@ -55,6 +55,9 @@ const ( importBatchSize = 2500 ) +// ErrImportInterrupted is returned when the user interrupts the import process. +var ErrImportInterrupted = errors.New("interrupted") + // Fatalf formats a message to standard error and exits the program. // The message is also printed to standard output if standard error // is redirected to a different file. @@ -191,7 +194,7 @@ func ImportChain(chain *core.BlockChain, fn string) error { for batch := 0; ; batch++ { // Load a batch of RLP blocks. if checkInterrupt() { - return errors.New("interrupted") + return ErrImportInterrupted } i := 0 for ; i < importBatchSize; i++ {