mirror of
https://github.com/fatedier/frp.git
synced 2026-04-20 16:09:10 +08:00
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:
@@ -3,7 +3,9 @@ package process
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Process struct {
|
type Process struct {
|
||||||
@@ -12,9 +14,11 @@ type Process struct {
|
|||||||
errorOutput *bytes.Buffer
|
errorOutput *bytes.Buffer
|
||||||
stdOutput *bytes.Buffer
|
stdOutput *bytes.Buffer
|
||||||
|
|
||||||
done chan struct{}
|
done chan struct{}
|
||||||
waitErr error
|
closeOne sync.Once
|
||||||
|
waitErr error
|
||||||
|
|
||||||
|
started bool
|
||||||
beforeStopHandler func()
|
beforeStopHandler func()
|
||||||
stopped bool
|
stopped bool
|
||||||
}
|
}
|
||||||
@@ -40,26 +44,35 @@ func NewWithEnvs(path string, params []string, envs []string) *Process {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Process) Start() error {
|
func (p *Process) Start() error {
|
||||||
|
if p.started {
|
||||||
|
return errors.New("process already started")
|
||||||
|
}
|
||||||
|
p.started = true
|
||||||
|
|
||||||
err := p.cmd.Start()
|
err := p.cmd.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.waitErr = err
|
p.waitErr = err
|
||||||
close(p.done)
|
p.closeDone()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
go func() {
|
go func() {
|
||||||
p.waitErr = p.cmd.Wait()
|
p.waitErr = p.cmd.Wait()
|
||||||
close(p.done)
|
p.closeDone()
|
||||||
}()
|
}()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Process) closeDone() {
|
||||||
|
p.closeOne.Do(func() { close(p.done) })
|
||||||
|
}
|
||||||
|
|
||||||
// Done returns a channel that is closed when the process exits.
|
// Done returns a channel that is closed when the process exits.
|
||||||
func (p *Process) Done() <-chan struct{} {
|
func (p *Process) Done() <-chan struct{} {
|
||||||
return p.done
|
return p.done
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Process) Stop() error {
|
func (p *Process) Stop() error {
|
||||||
if p.stopped {
|
if p.stopped || !p.started {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
|
|||||||
Reference in New Issue
Block a user