Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ignore interrupt and killed errors when closing a process #47

Merged
merged 1 commit into from
May 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/basic/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type Controller struct {
}

func (c *Controller) Index() string {
return "hello world"
return "hello world."
}

func (c *Controller) Show(id string) string {
Expand Down
18 changes: 17 additions & 1 deletion package/exe/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (c *Cmd) Close() error {
}
}
if err := cmd.Wait(); err != nil {
if !isExitStatus(err) && !isWaitError(err) {
if !canIgnore(err) {
return err
}
}
Expand All @@ -37,10 +37,26 @@ func (c *Cmd) Wait() error {
return c.cmd().Wait()
}

// Errors we can safely ignore when closing the process
func canIgnore(err error) bool {
return isExitStatus(err) ||
isInterrupt(err) ||
isKilled(err) ||
isWaitError(err)
}

func isExitStatus(err error) bool {
return err != nil && strings.Contains(err.Error(), "exit status ")
}

func isInterrupt(err error) bool {
return err != nil && err.Error() == `signal: interrupt`
}

func isKilled(err error) bool {
return err != nil && err.Error() == `signal: killed`
}

func isWaitError(err error) bool {
return err != nil && strings.Contains(err.Error(), "Wait was already called")
}
Expand Down