Skip to content

Commit

Permalink
Stop Process TTL goroutine when process is not ready (#28)
Browse files Browse the repository at this point in the history
- fix issue where the goroutine will continue even though the child
  process is no longer running and the Process' state is not Ready
- fix issue where some logs were going to stdout instead of p.logMonitor
  causing them to not show up in the /logs
  • Loading branch information
mostlygeek committed Dec 16, 2024
1 parent d6ca535 commit ef22b6f
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions proxy/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ func (p *Process) start() error {
maxDuration := time.Duration(p.config.UnloadAfter) * time.Second

for range time.Tick(time.Second) {
if p.state != StateReady {
return
}

// wait for all inflight requests to complete and ticker
p.inFlightRequests.Wait()

Expand Down Expand Up @@ -162,25 +166,25 @@ func (p *Process) Stop() {
// will be a source of pain in the future.

p.cmd.Process.Signal(syscall.SIGTERM)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
sigtermTimeout, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

done := make(chan error, 1)
sigtermNormal := make(chan error, 1)
go func() {
done <- p.cmd.Wait()
sigtermNormal <- p.cmd.Wait()
}()

select {
case <-ctx.Done():
fmt.Printf("!!! process for %s timed out waiting to stop\n", p.ID)
case <-sigtermTimeout.Done():
fmt.Fprintf(p.logMonitor, "!!! process for %s timed out waiting to stop\n", p.ID)
p.cmd.Process.Kill()
p.cmd.Wait()
case err := <-done:
case err := <-sigtermNormal:
if err != nil {
if err.Error() != "wait: no child processes" {
// possible that simple-responder for testing is just not
// existing right, so suppress those errors.
fmt.Printf("!!! process for %s stopped with error > %v\n", p.ID, err)
fmt.Fprintf(p.logMonitor, "!!! process for %s stopped with error > %v\n", p.ID, err)
}
}
}
Expand Down

0 comments on commit ef22b6f

Please sign in to comment.