Skip to content

fix(mail): SMTP connect timeout + circuit-breaker so a down mail server can't stall the cron#236

Merged
fabiodalez-dev merged 1 commit into
mainfrom
fix/smtp-circuit-breaker-timeout
Jul 8, 2026
Merged

fix(mail): SMTP connect timeout + circuit-breaker so a down mail server can't stall the cron#236
fabiodalez-dev merged 1 commit into
mainfrom
fix/smtp-circuit-breaker-timeout

Conversation

@fabiodalez-dev

Copy link
Copy Markdown
Owner

Observed on a real instance whose SMTP host was unreachable: the automatic-notifications cron logged a wall of Could not connect to SMTP host — a full 3-attempt retry cycle (with 1s sleeps) for every overdue-loan recipient. Two robustness gaps:

  • No Timeout on the PHPMailer path → PHPMailer's 300s default. An SMTP host that accepts the TCP connection but then stalls would block the run for 5 minutes per email. Capped at 10s (configurable via mail.smtp.timeout); the raw-socket path already used 10s.
  • No circuit-breaker → every recipient got the full retry cycle even though they all fail identically when the server is down. Added Mailer::isSmtpReachable() — a single short TCP probe (4s), cached per request, logged once — and short-circuit sendWithRetry on it, so a down SMTP costs one probe instead of N × 3 connection timeouts.

The batch still runs (loans still marked overdue, in-app notifications still created); only the email attempts become cheap no-ops when the server is unreachable.

Verified

Probe returns false in 0.00s on connection-refused and ~4s on an unroutable host (never the 300s default). php -l + PHPStan L5 clean.

Note: this is defensive robustness — the reported instance's actual problem is its own SMTP config being unreachable, which no code change fixes.

…er can't stall the cron

Observed on a real instance whose SMTP host was unreachable: the automatic-notifications
cron logged a wall of "Could not connect to SMTP host" — one full 3-attempt retry cycle
(with 1s sleeps) for every overdue-loan recipient. Two robustness gaps:

- The PHPMailer path never set Timeout, so it used PHPMailer's 300s default: an SMTP host
  that accepts the TCP connection but then stalls would block the run for 5 minutes per
  email. Cap it at 10s (configurable via mail.smtp.timeout); the raw-socket path already
  used 10s.
- No circuit-breaker: every recipient got the full retry cycle even though they all fail
  the same way when the server is down. Add Mailer::isSmtpReachable() — a single short TCP
  probe (4s), cached per request, logged once — and short-circuit sendWithRetry on it.

The batch still runs (loans still marked overdue, in-app notifications still created);
only the email attempts become cheap no-ops when the server is unreachable.

Verified: probe returns false in 0.00s on connection-refused and ~4s on an unroutable
host (never the 300s default). php -l + PHPStan L5 clean.
@coderabbitai

coderabbitai Bot commented Jul 8, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@fabiodalez-dev, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 50 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 08dc6bb3-71df-4002-9111-8810055249cd

📥 Commits

Reviewing files that changed from the base of the PR and between eabab72 and ad46da5.

📒 Files selected for processing (2)
  • app/Support/Mailer.php
  • app/Support/NotificationService.php
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/smtp-circuit-breaker-timeout

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@fabiodalez-dev fabiodalez-dev merged commit fc2a61c into main Jul 8, 2026
6 checks passed
@fabiodalez-dev fabiodalez-dev deleted the fix/smtp-circuit-breaker-timeout branch July 8, 2026 15:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant