Skip to content

Commit

Permalink
dcrdtest: Use dcrd --piperx to shutdown
Browse files Browse the repository at this point in the history
This provides a cleaner shutdown procedure in windows, where the
interrupt signal is not available.
  • Loading branch information
matheusd committed Nov 24, 2022
1 parent 95be839 commit 9ce3e22
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
35 changes: 26 additions & 9 deletions dcrdtest/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ type nodeConfig struct {
// pipeTX are the read/write ends of a pipe that is used with the
// --pipetx dcrd arg.
pipeTX ipcPipePair

// pipeRX are the read/write ends of a pipe that is used with the
// --piperx dcrd arg.
pipeRX ipcPipePair
}

// newConfig returns a newConfig with all default values.
Expand All @@ -57,6 +61,11 @@ func newConfig(prefix, certFile, keyFile string, extra []string) (*nodeConfig, e
if err != nil {
return nil, fmt.Errorf("unable to create pipe for dcrd IPC: %v", err)
}
pipeRX, err := newIPCPipePair()
if err != nil {
return nil, fmt.Errorf("unable to create pipe for dcrd IPC: %v", err)
}

a := &nodeConfig{
listen: "127.0.0.1:0",
rpcListen: "127.0.0.1:0",
Expand All @@ -70,6 +79,7 @@ func newConfig(prefix, certFile, keyFile string, extra []string) (*nodeConfig, e
keyFile: keyFile,

pipeTX: pipeTX,
pipeRX: pipeRX,
}
if err := a.setDefaults(); err != nil {
return nil, err
Expand Down Expand Up @@ -330,16 +340,23 @@ func (n *node) stop() error {
return nil
}

// Send kill command
n.tracef("stop send kill")
var err error
if runtime.GOOS == "windows" {
err = n.cmd.Process.Signal(os.Kill)
} else {
err = n.cmd.Process.Signal(os.Interrupt)
}
// Attempt a graceful dcrd shutdown by closing the pipeRX files.
err := n.config.pipeRX.close()
if err != nil {
n.t.Logf("stop Signal error: %v", err)
n.logf("Unable to close piperx ends: %v", err)

// Make a harder attempt at shutdown, by sending an interrupt
// signal.
n.tracef("stop send kill")
var err error
if runtime.GOOS == "windows" {
err = n.cmd.Process.Signal(os.Kill)
} else {
err = n.cmd.Process.Signal(os.Interrupt)
}
if err != nil {
n.t.Logf("stop Signal error: %v", err)
}
}

// Wait for pipes.
Expand Down
2 changes: 2 additions & 0 deletions dcrdtest/node_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import (
func setOSNodeCmdOptions(n *nodeConfig, cmd *exec.Cmd) {
cmd.ExtraFiles = []*os.File{
n.pipeTX.w,
n.pipeRX.r,
}
}

// appendOSNodeArgs appends platform-specific arguments needed to run dcrd.
func appendOSNodeArgs(n *nodeConfig, args []string) []string {
args = append(args, "--pipetx=3")
args = append(args, "--piperx=4")
return args
}
2 changes: 2 additions & 0 deletions dcrdtest/node_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ func setOSNodeCmdOptions(n *nodeConfig, cmd *exec.Cmd) {
cmd.SysProcAttr = &syscall.SysProcAttr{
AdditionalInheritedHandles: []syscall.Handle{
syscall.Handle(n.pipeTX.w.Fd()),
syscall.Handle(n.pipeRX.r.Fd()),
},
}
}

// appendOSNodeArgs appends platform-specific arguments needed to run dcrd.
func appendOSNodeArgs(n *nodeConfig, args []string) []string {
args = append(args, fmt.Sprintf("--pipetx=%d", n.pipeTX.w.Fd()))
args = append(args, fmt.Sprintf("--piperx=%d", n.pipeRX.r.Fd()))
return args
}

0 comments on commit 9ce3e22

Please sign in to comment.