mirror of
https://github.com/SSLMate/certspotter.git
synced 2025-06-27 10:15:33 +02:00
Enforce a timeout when running sendmail
postfix's sendmail command sometimes retries forever instead of terminating on error (see #100)
This commit is contained in:
parent
f0e8b18d9a
commit
5430f737b0
@ -89,18 +89,23 @@ func sendEmail(ctx context.Context, to []string, notif *notification) error {
|
|||||||
args = append(args, "--")
|
args = append(args, "--")
|
||||||
args = append(args, to...)
|
args = append(args, to...)
|
||||||
|
|
||||||
sendmail := exec.CommandContext(ctx, sendmailPath(), args...)
|
sendmailCtx, cancel := context.WithDeadline(ctx, time.Now().Add(2*time.Minute))
|
||||||
|
defer cancel()
|
||||||
|
sendmail := exec.CommandContext(sendmailCtx, sendmailPath(), args...)
|
||||||
sendmail.Stdin = stdin
|
sendmail.Stdin = stdin
|
||||||
sendmail.Stderr = stderr
|
sendmail.Stderr = stderr
|
||||||
|
|
||||||
if err := sendmail.Run(); err == nil {
|
if err := sendmail.Run(); err == nil {
|
||||||
return nil
|
return nil
|
||||||
|
} else if sendmailCtx.Err() != nil && ctx.Err() == nil {
|
||||||
|
return fmt.Errorf("error sending email to %v: sendmail command timed out")
|
||||||
} else if ctx.Err() != nil {
|
} else if ctx.Err() != nil {
|
||||||
|
// if the context was canceled, we can't be sure that the error is the fault of sendmail, so ignore it
|
||||||
return ctx.Err()
|
return ctx.Err()
|
||||||
} else if exitErr, isExitError := err.(*exec.ExitError); isExitError && exitErr.Exited() {
|
} else if exitErr, isExitError := err.(*exec.ExitError); isExitError && exitErr.Exited() {
|
||||||
return fmt.Errorf("error sending email to %v: sendmail failed with exit code %d and error %q", to, exitErr.ExitCode(), strings.TrimSpace(stderr.String()))
|
return fmt.Errorf("error sending email to %v: sendmail failed with exit code %d and error %q", to, exitErr.ExitCode(), strings.TrimSpace(stderr.String()))
|
||||||
} else {
|
} else {
|
||||||
return fmt.Errorf("error sending email to %v: %w", to, err)
|
return fmt.Errorf("error sending email to %v: error running sendmail command: %w", to, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user