test/e2e: guard Process against double-Start and Stop-before-Start

Add started flag to prevent double-Start panics and allow Stop to
return immediately when the process was never started. Use sync.Once
for closing the done channel as defense-in-depth against double close.
This commit is contained in:
fatedier
2026-03-09 01:44:54 +08:00
parent f32bec9f4d
commit 8d1ab7d585

View File

@@ -3,7 +3,9 @@ package process
import (
"bytes"
"context"
"errors"
"os/exec"
"sync"
)
type Process struct {
@@ -12,9 +14,11 @@ type Process struct {
errorOutput *bytes.Buffer
stdOutput *bytes.Buffer
done chan struct{}
waitErr error
done chan struct{}
closeOne sync.Once
waitErr error
started bool
beforeStopHandler func()
stopped bool
}
@@ -40,26 +44,35 @@ func NewWithEnvs(path string, params []string, envs []string) *Process {
}
func (p *Process) Start() error {
if p.started {
return errors.New("process already started")
}
p.started = true
err := p.cmd.Start()
if err != nil {
p.waitErr = err
close(p.done)
p.closeDone()
return err
}
go func() {
p.waitErr = p.cmd.Wait()
close(p.done)
p.closeDone()
}()
return nil
}
func (p *Process) closeDone() {
p.closeOne.Do(func() { close(p.done) })
}
// Done returns a channel that is closed when the process exits.
func (p *Process) Done() <-chan struct{} {
return p.done
}
func (p *Process) Stop() error {
if p.stopped {
if p.stopped || !p.started {
return nil
}
defer func() {