Repository navigation
fix(mails): end a mail the project's own SMTP refused for good - #14027
Conversation
The SMTP client knows whether a reply is 4xx or 5xx, but the adapter flattened every failure into a string, so a caller could only guess from the text whether sending again could go differently. Each result now says so: a 5xx reply, a refused AUTH carrying one, or a server that cannot carry the message is permanent; a dropped connection, a timeout, a 4xx or a reply out of protocol is not. With several hosts it is permanent only when every host refused for good. The verdict is tested against the reply lines production servers sent, spoken by a real socket in a child process.
A project's own server answering 5xx -- a 535 for stale credentials, a 550 for an unverified sender domain, a 554 in place of the greeting -- answers every attempt the same way. On a broker that retries, each retry repeats the failed login from the shared egress IP and holds one of the consumer's ack slots through its backoff. In fra1 one project's stale MailerSend credentials drew 714 refusals in a week, and MailerSend then answered 3,182 more of its sends with "Too many failed login requests from" our egress address -- a throttle on the address, not the account, so it reaches any project sending through MailerSend from there. Such a failure now throws PermanentFailure, so it is dead-lettered at the first attempt. 4xx replies, timeouts and unreachable hosts keep their retries, and so does anything sent through Appwrite's own provider, where a refusal is an incident on our side. On Redis nothing changes: a failed mail already stayed on the failed list.
🟢 Tier S · Ready to merge
The SMTP adapter exposes permanent-refusal verdicts, with recipient-specific replies and conservative aggregation across hosts. Mails and notifications end permanent project-SMTP refusals while retaining transient failures for retry; notifications also verify acceptance before persistence and prioritize retryable fan-out failures. SMTP client changes preserve recipient refusals across reset errors and restrict HELO fallback to permanent EHLO refusals. Latest changes: The newest commit removes the two added project-SMTP worker unit tests and their unused imports, resolving the worker-test placement finding.
📂 Walkthrough · 13
✅ Fixed since the last review · 1
Reviewed the commits since |
|
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
Workers are covered end to end, not in the unit tier (AGENTS.md). The permanence verdict stays covered where it is decided, in the messaging package's SMTPRefusalTest. On the Redis broker the E2E stack runs, a terminal verdict is deliberately parked like any failure, so no E2E test there can observe it.
…essage-wide refusal to all
|
Addressed the findings from the latest review summary in 3c4f43b:
|
There was a problem hiding this comment.
Your trial has ended. Reactivate Greptile to resume code reviews.
|
Review complete. 🟠 1 high · 🟡 2 medium 💬 Inline comments (1)
📍 Findings outside the diff (2) — 🟠 1 high · 🟡 1 medium — defects on lines GitHub can't attach comments to🟠 High — HELO fallback runs on a session a 421 EHLO already discarded · Client.php:270–279 · // packages/smtp/src/Client.php
270 private function hello(): void
271 {
272 try {
273 $this->capabilities = Capabilities::fromReply($this->command("EHLO {$this->domain}", [250]));
274 } catch (TransactionException) {
275 // A server too old for ESMTP still has to answer HELO.
276 $this->command("HELO {$this->domain}", [250]);
277 $this->capabilities = Capabilities::none();
278 }
279 }
🟡 Medium — Propagate The new permanent-failure mechanism — read 🧹 Nitpicks (2) — 🟢 2 low
The PR makes SMTP recipient refusals (5xx) terminate delivery with a
Reviewed commit: 5a53276 |
There was a problem hiding this comment.
Expand to see previous review
This PR adds permanent-failure detection to the SMTP messaging adapter, propagating it through Response details and the Mails worker so refused messages are no longer retried forever, with a scripted SMTP test harness.
Key findings
- 🟠 HELO fallback runs on a session a 421 EHLO already discarded — Client.php:270
- 🟡 Treat
CapabilityExceptionas permanent inpermanent()— SMTP.php:218 - 🟡 Propagate
PermanentFailureto the Notifications worker — Notifications.php
A 4xx to EHLO is a busy or closing server, not one without ESMTP, and a 421 has already ended the session: HELO was written on the closed transport, which throws a LogicException no SmtpException catch sees. The fake transport now refuses reads and writes after close(), as Native and Swoole do, which is what let this pass unnoticed.
No STARTTLS when it is required, no SMTPUTF8 for a non-ASCII address, a message over the advertised SIZE: none can change on a retry, now that a HELO session only follows a server that does not speak ESMTP at all.
…for good The adapter records refusals in its result instead of throwing, and the worker discarded that result, so a refused notification was persisted as sent and never retried. As in the mails worker: a project's own server that refused permanently ends with PermanentFailure, anything else is retried.
|
@tenki-reviewer thanks. Each finding is addressed below. 🟠 HELO fallback on a session a 421 EHLO discarded: valid, fixed in e6b12c7.
🟡 CapabilityException should be permanent: valid, fixed in 5b3981c. Answered on the inline thread. It's sound only together with the HELO fix, which is why both are in this round. 🟡 Notifications worker: valid, and worse than described. Fixed in 6eaefcb. The SMTP adapter records a refusal in its result rather than throwing, and
CE doesn't unit-test workers, and no E2E reaches a refusing project SMTP for notifications, so this path is covered by the adapter tests only. 🟢 Nitpicks
Checks:
|
|
Review complete. No blocking issues — approved ✅; 3 nitpicks below. 🧹 Nitpicks (3) — 🟢 3 low
This change distinguishes permanent (5xx) from transient (4xx/connection-drop) SMTP failures end to end, letting workers throw
Overall the implementation is logically consistent; the verified findings are low-severity polish items (stale Reviewed commit: 6eaefcb |
Security rulesNo new WARNING or ERROR findings from security rules. 32 existing findings tracked in
|
…n a fan-out action() kept only the first failure, so a permanent refusal for one recipient dead-lettered a message that another recipient's transient failure still needed retried.
AGENTS.md keeps workers out of the unit suite. The precedence rule is covered by review and documented as an E2E gap in the PR.
…refused for good Reconciles main's 4ab36cb, which ended every project-SMTP failure, with this change's split: a 5xx refusal ends the job with PermanentFailure, while an unreachable or busy server stays retryable so a one-time code is not lost to a moment's outage. The test for an unreachable project server now asserts the retry, and a scripted 535 asserts the end.
…suite AGENTS.md keeps workers out of the unit suite. The retry-or-end verdict is pinned where it is decided, in packages/messaging's SMTPRefusalTest over a real socket; main's testProjectSmtpFailureIsNotRetried encoded the behaviour this change replaces.
Summary
The SMTP adapter now reports whether a failure is permanent:
Responserows gain apermanentkey. When a project's own SMTP server refuses for good, the mails worker throwsUtopia\Queue\PermanentFailure, so the message is dead-lettered on the first attempt instead of being retried.Part of appwrite-labs/cloud#5815, tracked in appwrite-labs/cloud#6216.
Why
A 535, 550 or 554 from a project's own server gives every attempt the same answer. On a broker that retries, each attempt:
maxAckPending=12, backoff 30/120/600s).The adapter already knew the reply class, but flattened it into a string such as
No SMTP host answered: host:587 (Authentication failed: 535 …). Rather than parse that text, the adapter now passes its own verdict:NoHostAnswered, still aConnectionException).The worker acts on it only for the project's own SMTP. Appwrite's own provider refusing is an incident on our side, so it keeps its retries. Messages and codes (401/500) are unchanged.
Evidence (Appwrite Cloud
worker-mails, 7 days, all production regions)535login refused, many providers535 Too many failed login requests from <egress IP>554in place of the greeting551Spamhaus listing550/553/530/525(unverified domain, invalid recipient, sender rejected, …)450/454/4213,904 of the 4,142 failures in the busiest region came from one project: 714 bad-credential refusals, then 3,182 throttle refusals caused by its own repeated logins. The throttle names our shared egress IP.
Tests
packages/messagingSMTPRefusalTest: 17 tests running the real adapter against a real socket that sends production reply lines. With the adapter change reverted, the permanent cases fail. The conservative cases also pass: no login offered with no reply code behind it is retried, and several recipients refused together are not marked permanent.PermanentFailureon the failed list like any other error (packages/queue/src/Broker/Redis.php:192-201). No test on that stack can tell a permanent failure from a retryable one; the difference only exists on NATS. So the one-line mapping in the worker (project SMTP plus permanent →PermanentFailure) has no automated coverage.Not covered
client()→dial()) uses the same classifier but has no test of its own.isRetryableError()could readpermanentinstead. That's a follow-up.Follow-ups after review (2026-10-07)
TransactionExceptioninstead of aLogicExceptionfrom writing on a closed transport.CapabilityException(no STARTTLS when required, no SMTPUTF8, over the advertised SIZE) as permanent.PermanentFailure. In a fan-out, a retryable failure takes precedence over a permanent one.main's4ab36cb364ended every project-SMTP failure without a retry. This branch keeps that for refusals that can't change (5xx →PermanentFailure). An unreachable or busy server (connection refused, 421, 450, 454) stays retryable, so a one-time code isn't lost to a moment's outage. A failure the adapter throws is retryable too, since no server reply says it will repeat.MailsTest::testProjectSmtpFailureIsNotRetriedis removed: it encoded the replaced behaviour, and AGENTS.md keeps workers out of the unit suite. The verdict is pinned inpackages/messagingSMTPRefusalTest; the worker branch has no E2E coverage.Note for Redis deployments (corrected): on Redis, a retryable failure and a
PermanentFailureboth land on the queue'sfailedlist (Broker\Redis::reject()), and failed payloads never expire. So every project-SMTP failure is kept there again, payload included (project SMTP password, mail body). On NATS, a transient project-SMTP failure is retried up tomaxDeliverand holds an ack slot while it backs off. This is under review before merge.