Skip to content

Commit

Permalink
prevent nil cmd panic in stop if cmd never started, with a started chan
Browse files Browse the repository at this point in the history
  • Loading branch information
jrick committed Apr 4, 2024
1 parent 73ffcef commit eecdd07
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions dcrdtest/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,15 @@ func (n *nodeConfig) String() string {
type node struct {
config *nodeConfig

cmd *exec.Cmd
cmdDone chan error // Sent error and closed after cmdErr is assigned
cmdErr error // Must only be read after receiving on cmdDone
pidFile string
stderr io.ReadCloser
stdout io.ReadCloser
wg sync.WaitGroup
pid int
cmd *exec.Cmd
cmdStarted chan struct{} // Closed after cmd is stared and assigned
cmdDone chan error // Sent error and closed after cmdErr is assigned
cmdErr error // Must only be read after receiving on cmdDone
pidFile string
stderr io.ReadCloser
stdout io.ReadCloser
wg sync.WaitGroup
pid int

// Locally bound addresses for the subsystems.
p2pAddr string
Expand All @@ -204,9 +205,10 @@ func (n *node) logf(format string, args ...interface{}) {
// as the base for the log and data directories for dcrd.
func newNode(config *nodeConfig, dataDir string) (*node, error) {
return &node{
config: config,
dataDir: dataDir,
cmdDone: make(chan error, 1),
config: config,
dataDir: dataDir,
cmdStarted: make(chan struct{}),
cmdDone: make(chan error, 1),
}, nil
}

Expand Down Expand Up @@ -312,6 +314,7 @@ func (n *node) start(ctx context.Context) error {
return fmt.Errorf("%w: %v", errDcrdCmdExec, err)
}
n.cmd = cmd
close(n.cmdStarted)
n.pid = n.cmd.Process.Pid

// Unblock pipes now that pid is available.
Expand Down Expand Up @@ -365,6 +368,11 @@ func (n *node) stop() error {
log.Tracef("stop %p", n.cmd)
defer log.Tracef("stop done")

if _, ok := <-n.cmdStarted; !ok {
// has not been started (yet, or ever)
return nil
}

select {
case <-n.cmdDone:
// already stopped
Expand Down Expand Up @@ -398,8 +406,7 @@ func (n *node) stop() error {
// Wait for command to exit.
log.Tracef("stop cmd.Wait")
<-n.cmdDone
err = n.cmdErr
if err != nil {
if err := n.cmdErr; err != nil {
log.Debugf("stop cmd.Wait error: %v", err)
}

Expand Down

0 comments on commit eecdd07

Please sign in to comment.