Skip to content

fix: make email ingress fail loudly and stop blocking on slow dependencies - #3

Merged
bmc08gt merged 1 commit into
mainfrom
fix/ingress-resilience
Oct 1, 2026
Merged

bmc08gt merged 1 commit into
mainfrom
fix/ingress-resilience

Conversation

@bmc08gt

@bmc08gt bmc08gt commented Oct 1, 2026

Copy link
Copy Markdown
Member

Ingest dropped mail silently on any API error, rejected anything over about 7 MB, and could be stalled by a slow SSE client or a transient Turso error.

Worker (workers/email-ingress)

  • The fetch to the API now times out at 25s. Any thrown fetch or non-2xx response calls message.setReject(...); before, it was logged and the sender saw success.
  • setReject is documented in @cloudflare/workers-types as a permanent SMTP error only, so 5xx, 429 and timeouts bounce with "Temporary failure, please retry" instead of being retried by the sender's MTA. There is no temporary-failure API.
  • att.content can be a string in the installed postal-mime types, which failed tsc; it is now encoded to bytes.

API

  • Body limit: 16 MB for POST /v1/inboxes/{address}/emails with a matching X-Worker-Secret, 10 MB elsewhere. Chunked ingress bodies get 411. BodyLimitTest checks the handler does not run after a 413.
  • SseManager.publish uses DROP_OLDEST and tryEmit, so it no longer suspends inside ingestEmail. SSE streams write a : keepalive comment every 25s and close when the session has expired.
  • TursoClient: 3 attempts (200ms, 600ms) on network errors, timeouts, 5xx and 429; never 4xx. Request timeout 30s to 10s. An empty response raises a clear error.
  • /health runs SELECT 1 and returns {status}. The COUNT(*) stats are removed; nothing in web/ or cli/ reads them. /livez returns 200 without the DB.
  • Attachments are decoded before the email row is inserted, so bad base64 no longer leaves an orphan row.
  • Decrypt failures log at ERROR. Bodies return [This message could not be decrypted]; attachment downloads fail instead of returning ciphertext. Emails stored before an inbox had an encryption key will also hit this path, since the old code fell back to the raw value.

Deploy

  • The API changes ship when blip-cloud bumps blip.sha.
  • The worker change only reaches production when the worker is deployed with wrangler deploy in workers/email-ingress; there is no automated deploy yet.
  • The 16 MB limit needs the worker deployed too to matter, but is harmless before that.

@bmc08gt bmc08gt self-assigned this Oct 1, 2026
…ncies

Worker: a non-2xx or thrown fetch to the API was logged and the email
dropped while the sender saw success. The worker now times the fetch out
at 25s and calls setReject on any failure. The Workers types document
setReject as a permanent SMTP error only, so 5xx/429/timeouts bounce
with "Temporary failure, please retry" rather than being retried by the
sender's MTA.

API:
- Ingress body limit is 16 MB for POST /v1/inboxes/{address}/emails with
  a valid worker secret (attachments are base64-encoded into one JSON
  body, so 10 MB of raw mail always 413'd); other routes stay at 10 MB.
  Chunked ingress bodies get 411. Test asserts the handler does not run
  after a 413.
- SseManager drops the oldest event instead of suspending, so a stalled
  SSE client can no longer block ingest. SSE streams send a keepalive
  comment every 25s and close when the session has expired.
- TursoClient retries network errors, timeouts, 5xx and 429 (3 attempts,
  200ms/600ms backoff), never 4xx; request timeout 30s -> 10s; empty
  responses raise a clear error instead of NoSuchElementException.
- /health is SELECT 1 only and returns {status}; the unauthenticated
  COUNT(*) stats are removed (nothing in web/ or cli/ reads them).
  /livez returns 200 without touching the DB.
- Attachments are decoded and validated before the email row is
  inserted, so bad base64 no longer leaves an orphan row.
- Decrypt failures log at ERROR and return a placeholder body (or fail
  the attachment download) instead of returning ciphertext.
@bmc08gt
bmc08gt force-pushed the fix/ingress-resilience branch from aa82fd5 to a333942 Compare October 1, 2026 02:20
@bmc08gt
bmc08gt merged commit a8cdced into main Oct 1, 2026
3 checks passed
@bmc08gt
bmc08gt deleted the fix/ingress-resilience branch October 1, 2026 02:32
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