Skip to content

Commit

Permalink
fix program start-up hang if startsecs is 0
Browse files Browse the repository at this point in the history
  • Loading branch information
stou committed Sep 30, 2018
1 parent cdf7207 commit e2651a7
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,16 +428,21 @@ func (p *Process) run(finishCb func()) {
atomic.StoreInt32(p.retryTimes, 0)
startSecs := int64(p.config.GetInt("startsecs", 1))
endTime := time.Now().Add(time.Duration(startSecs) * time.Second)
monitorExited := int32(0)
monitorExited := int32(1)
var once sync.Once

// finishCb can be only called one time
finishCbWrapper := func() {
once.Do(finishCb)
}
//process is not expired and not stoped by user
for time.Now().Before(endTime) && !p.stopByUser {
for !p.stopByUser {
atomic.StoreInt32(&monitorExited, 1)
// if the start-up time reaches startSecs
if startSecs > 0 && time.Now().After(endTime) {
p.failToStartProgram(fmt.Sprintf("fail to start program because the start-up time reaches the startsecs %d", startSecs), finishCbWrapper)
break
}
// The number of serial failure attempts that supervisord will allow when attempting to
// start the program before giving up and putting the process into an FATAL state
// first start time is not the retry time
Expand All @@ -464,12 +469,12 @@ func (p *Process) run(finishCb func()) {
if p.StderrLog != nil {
p.StderrLog.SetPid(p.cmd.Process.Pid)
}
log.WithFields(log.Fields{"program": p.GetName()}).Info("success to start program")
//Set startsec to 0 to indicate that the program needn't stay
//running for any particular amount of time.
if startSecs <= 0 {
log.WithFields(log.Fields{"program": p.GetName()}).Info("success to start program")
p.changeStateTo(RUNNING)
go finishCbWrapper()
} else if atomic.LoadInt32(p.retryTimes) == 1 { // only start monitor for first try
atomic.StoreInt32(&monitorExited, 0)
go func() {
Expand All @@ -483,10 +488,7 @@ func (p *Process) run(finishCb func()) {
p.lock.Lock()
}
// wait for monitor thread exit
for {
if atomic.LoadInt32(&monitorExited) != 0 {
break
}
for atomic.LoadInt32(&monitorExited) == 0 {
time.Sleep(time.Duration(100) * time.Millisecond)
}

Expand Down

0 comments on commit e2651a7

Please sign in to comment.