From 8ff7cd865d532168a8e89ca6821ceab914ccebe0 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Tue, 8 Feb 2022 14:02:46 +0300 Subject: [PATCH] cli: ensure chain is properly stopped after chain-related commands Blockchain occupies resources (e.g. it opens log files for DB, etc.) on creation and running. We need to release these resources if something goes wrong during execution chain-related commands. This commit solves the following problem on Windows: ``` --- FAIL: TestServerStart (0.32s) --- FAIL: TestServerStart/stateroot_service_is_on_&&_StateRootInHeader=true (0.04s) testing.go:894: TempDir RemoveAll cleanup: remove C:\Users\Anna\AppData\Local\Temp\TestServerStart_stateroot_service_is_on_&&_StateRootInHeader=true460557297\001\neogotestchain\000001.log: The process cannot access the file because it is being used by another process. --- FAIL: TestServerStart/invalid_Oracle_config (0.03s) testing.go:894: TempDir RemoveAll cleanup: remove C:\Users\Anna\AppData\Local\Temp\TestServerStart_invalid_Oracle_config810064028\001\neogotestchain\000001.log: The process cannot access the file because it is being used by another process. --- FAIL: TestServerStart/invalid_consensus_config (0.04s) testing.go:894: TempDir RemoveAll cleanup: remove C:\Users\Anna\AppData\Local\Temp\TestServerStart_invalid_consensus_config217270091\001\neogotestchain\000001.log: The process cannot access the file because it is being used by another process. --- FAIL: TestServerStart/invalid_Notary_config (0.07s) --- FAIL: TestServerStart/invalid_Notary_config/malformed_config (0.04s) testing.go:894: TempDir RemoveAll cleanup: remove C:\Users\Anna\AppData\Local\Temp\TestServerStart_invalid_Notary_config_malformed_config754934830\001\neogotestchain\000001.log: The process cannot access the file because it is being used by another process. --- FAIL: TestServerStart/invalid_Notary_config/invalid_wallet (0.03s) testing.go:894: TempDir RemoveAll cleanup: remove C:\Users\Anna\AppData\Local\Temp\TestServerStart_invalid_Notary_config_invalid_wallet934249397\001\neogotestchain\000001.log: The process cannot access the file because it is being used by another process. --- FAIL: TestServerStart/good (0.11s) testing.go:894: TempDir RemoveAll cleanup: remove C:\Users\Anna\AppData\Local\Temp\TestServerStart_good596150160\001\neogotestchain\000001.log: The process cannot access the file because it is being used by another process. ``` This commit also unifies blockchain and services releasing code. --- cli/server/server.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/cli/server/server.go b/cli/server/server.go index 310fada08..885c0bf4f 100644 --- a/cli/server/server.go +++ b/cli/server/server.go @@ -304,9 +304,11 @@ func restoreDB(ctx *cli.Context) error { if err != nil { return err } - defer chain.Close() - defer prometheus.ShutDown() - defer pprof.ShutDown() + defer func() { + pprof.ShutDown() + prometheus.ShutDown() + chain.Close() + }() var start uint32 if ctx.Bool("incremental") { @@ -472,6 +474,11 @@ func startServer(ctx *cli.Context) error { if err != nil { return cli.NewExitError(err, 1) } + defer func() { + pprof.ShutDown() + prometheus.ShutDown() + chain.Close() + }() serv, err := network.NewServer(serverConfig, chain, chain.GetStateSyncModule(), log) if err != nil { @@ -534,9 +541,6 @@ Main: if serverErr := rpcServer.Shutdown(); serverErr != nil { shutdownErr = fmt.Errorf("error on shutdown: %w", serverErr) } - prometheus.ShutDown() - pprof.ShutDown() - chain.Close() break Main } }