cmd, console, miner, mobile: proper node Close() termination

This commit is contained in:
Péter Szilágyi 2019-02-06 17:37:15 +02:00
parent bcfcccef49
commit 62a5482d10
No known key found for this signature in database
GPG key ID: E9AE538CEDF8293D
7 changed files with 15 additions and 5 deletions

View file

@ -282,7 +282,7 @@ func newFaucet(genesis *core.Genesis, port int, enodes []*discv5.Node, network u
// close terminates the Ethereum connection and tears down the faucet. // close terminates the Ethereum connection and tears down the faucet.
func (f *faucet) close() error { func (f *faucet) close() error {
return f.stack.Stop() return f.stack.Close()
} }
// listenAndServe registers the HTTP handlers for the faucet and boots it up // listenAndServe registers the HTTP handlers for the faucet and boots it up

View file

@ -288,6 +288,7 @@ func bzzd(ctx *cli.Context) error {
if err != nil { if err != nil {
utils.Fatalf("can't create node: %v", err) utils.Fatalf("can't create node: %v", err)
} }
defer stack.Close()
//a few steps need to be done after the config phase is completed, //a few steps need to be done after the config phase is completed,
//due to overriding behavior //due to overriding behavior
@ -365,6 +366,8 @@ func getPrivKey(ctx *cli.Context) *ecdsa.PrivateKey {
if err != nil { if err != nil {
utils.Fatalf("can't create node: %v", err) utils.Fatalf("can't create node: %v", err)
} }
defer stack.Close()
return getAccount(bzzconfig.BzzAccount, ctx, stack) return getAccount(bzzconfig.BzzAccount, ctx, stack)
} }

View file

@ -149,8 +149,8 @@ func (env *tester) Close(t *testing.T) {
if err := env.console.Stop(false); err != nil { if err := env.console.Stop(false); err != nil {
t.Errorf("failed to stop embedded console: %v", err) t.Errorf("failed to stop embedded console: %v", err)
} }
if err := env.stack.Stop(); err != nil { if err := env.stack.Close(); err != nil {
t.Errorf("failed to stop embedded node: %v", err) t.Errorf("failed to tear down embedded node: %v", err)
} }
os.RemoveAll(env.workspace) os.RemoveAll(env.workspace)
} }

View file

@ -69,7 +69,7 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer node.Stop() defer node.Close()
for node.Server().NodeInfo().Ports.Listener == 0 { for node.Server().NodeInfo().Ports.Listener == 0 {
time.Sleep(250 * time.Millisecond) time.Sleep(250 * time.Millisecond)

View file

@ -69,7 +69,7 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer node.Stop() defer node.Close()
for node.Server().NodeInfo().Ports.Listener == 0 { for node.Server().NodeInfo().Ports.Listener == 0 {
time.Sleep(250 * time.Millisecond) time.Sleep(250 * time.Millisecond)

View file

@ -192,6 +192,12 @@ func NewNode(datadir string, config *NodeConfig) (stack *Node, _ error) {
return &Node{rawStack}, nil return &Node{rawStack}, nil
} }
// Close terminates a running node along with all it's services, tearing internal
// state doen too. It's not possible to restart a closed node.
func (n *Node) Close() error {
return n.node.Close()
}
// Start creates a live P2P node and starts running it. // Start creates a live P2P node and starts running it.
func (n *Node) Start() error { func (n *Node) Start() error {
return n.node.Start() return n.node.Start()

View file

@ -47,6 +47,7 @@ func ExampleService() {
log.Fatalf("Failed to create network node: %v", err) log.Fatalf("Failed to create network node: %v", err)
} }
defer stack.Close() defer stack.Close()
// Create and register a simple network service. This is done through the definition // Create and register a simple network service. This is done through the definition
// of a node.ServiceConstructor that will instantiate a node.Service. The reason for // of a node.ServiceConstructor that will instantiate a node.Service. The reason for
// the factory method approach is to support service restarts without relying on the // the factory method approach is to support service restarts without relying on the