Skip to content

Commit

Permalink
Merge pull request #475 from symfony-cli/hanging-fix
Browse files Browse the repository at this point in the history
Hanging fix
  • Loading branch information
fabpot committed May 10, 2024
2 parents b29665f + b84bb4f commit 870a3ce
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 31 deletions.
41 changes: 13 additions & 28 deletions local/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,6 @@ func (r *Runner) Run() error {
// Command exited
case err := <-cmdExitChan:
logger.Debug().Msg("Received exit")
err = errors.Wrapf(err, `command "%s" failed`, r.pidFile)

if err == nil && r.SuccessHook != nil {
logger.Debug().Msg("Running success hook")
r.SuccessHook(r, cmd)
Expand All @@ -254,36 +252,23 @@ func (r *Runner) Run() error {
logger.Debug().Msg("Removing pid file")
return r.pidFile.Remove()
}
logger.Debug().Msg("Looping")

// Command is set up to restart on exit (usually PHP builtin
// server), so we restart immediately without waiting
// Command is set up to restart on exit (usually PHP builtin server)
if r.AlwaysRestartOnExit {
logger.Error().Msg("command exited, restarting it immediately")
logger.Debug().Msg("Looping")
// In case of error we want to wait up-to 5 seconds before
// restarting the command, this avoids overloading the system with a
// failing command
if err != nil {
logger.Error().Msgf(`command exited: %s, waiting 5 seconds before restarting it`, err)
timer.Reset(5 * time.Second)
} else {
logger.Error().Msg("command exited, restarting it immediately")
}
continue
}

// In case of error we want to wait up-to 5 seconds before
// restarting the command, this avoids overloading the system with a
// failing command
if err != nil {
logger.Error().Msgf(`command exited: %s, waiting 5 seconds before restarting it`, err)
timer.Reset(5 * time.Second)
}

// Wait for a timer to expire or a file to be changed to restart
// or a signal to be received to exit
logger.Debug().Msg("Waiting for channels")
select {
case sig := <-sigChan:
logger.Info().Msgf(`Signal "%s" received, exiting`, sig)
return nil
case <-restartChan:
logger.Debug().Msg("Received restart")
timer.Stop()
case <-timer.C:
logger.Debug().Msg("Received timer message")
}
return nil
}

logger.Info().Msg("Restarting command")
Expand All @@ -295,7 +280,7 @@ func (r *Runner) buildCmd() (*exec.Cmd, error) {
cmd.Env = os.Environ()
cmd.Dir = r.pidFile.Dir

if err := buildCmd(cmd); err != nil {
if err := buildCmd(cmd, r.mode == RunnerModeOnce && terminal.Stdin.IsInteractive()); err != nil {
return nil, err
}

Expand Down
5 changes: 3 additions & 2 deletions local/runner_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (
"syscall"
)

func buildCmd(cmd *exec.Cmd) error {
func buildCmd(cmd *exec.Cmd, foreground bool) error {
cmd.SysProcAttr = &syscall.SysProcAttr{
// isolate each command in a new process group that we can cleanly send
// signal to when we want to stop it
Setpgid: true,
Setpgid: true,
Foreground: foreground,
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion local/runner_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package local

import "os/exec"

func buildCmd(*exec.Cmd) error {
func buildCmd(*exec.Cmd, bool) error {
return nil
}

0 comments on commit 870a3ce

Please sign in to comment.