Skip to content

Commit

Permalink
cli: ensure chain is properly stopped after chain-related commands
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
AnnaShaleva committed Feb 8, 2022
1 parent 3514231 commit 22ea3fb
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions cli/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,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") {
Expand Down Expand Up @@ -477,6 +479,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 {
Expand Down Expand Up @@ -539,9 +546,6 @@ Main:
if serverErr := rpcServer.Shutdown(); serverErr != nil {
shutdownErr = fmt.Errorf("error on shutdown: %w", serverErr)
}
prometheus.ShutDown()
pprof.ShutDown()
chain.Close()
break Main
}
}
Expand Down

0 comments on commit 22ea3fb

Please sign in to comment.