Repository navigation
fix(email): lengthen outbox claim timeout, log aborted claims, and harden prod DB detection (#273) - #305
Conversation
… DB JWT ref check (#273)
There was a problem hiding this comment.
If your organization's extra usage balance is empty, an organization admin can add extra usage credits at claude.ai/admin-settings/usage. If its monthly spend limit was reached, an admin can raise it on the same page. If neither applies, contact Anthropic support.
Once extra usage is available, someone with write access to this repository can comment @claude review on this pull request to trigger a review.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the
|
|
PO acceptance: merge at 02f03f1 Verified on GitHub:
|
Summary
App portion (Phase 2) of email outbox follow-ups from the
#254second reviews (Phase 1 DB migration merged inDOS/DOS.Me#1870at0223503c0195725ef56c6618bd229a5af8707468).Refs #273
Changes
apps/web/src/server/email/outbox.ts,outbox.test.ts):CLAIM_TIMEOUT_MSfrom10_000ms to15_000ms (safely above PostgREST's 8 sstatement_timeoutplus pooler/network jitter so a committingclaim_outboxcall is not aborted in flight, while remaining well underRUN_BUDGET_MS= 30 s).claimSignal.abortedandisAbortError(error)(excluding Postgres server-side rollback57014) to emit an explicit error log noting that the DB transaction may still have committed and leased rows for 5 minutes — on both first and subsequent claim calls.apps/web/src/server/email/provider.ts,provider.test.ts):usesProductionDatabase(env)to inspect the JWTrefclaim ofSUPABASE_SERVICE_ROLE_KEYandNEXT_PUBLIC_SUPABASE_ANON_KEYin addition toPRODUCTION_SUPABASE_HOSTS/PRODUCTION_SUPABASE_REF, so an unlisted custom domain is still caught whenever legacy JWT keys are used (while cleanly ignoring opaquesb_secret_*keys and malformed tokens).DOS/DOS.Memigrations (and Preview inherits migrated rows), so an in-DB marker cannot safely distinguish Production from Preview/Beta without violating migration-only state.Verification
pnpm format:check— PASSpnpm lint— PASSpnpm typecheck— PASSpnpm test— PASS (180/180 files, 2622/2622 tests)pnpm build— PASS📌 TL;DR
This PR hardens the email outbox system by increasing the claim timeout to prevent race conditions with PostgREST and adding specific logging for aborted transactions. It also enhances production environment detection to identify the production database via JWT project references, preventing non-production deployments from accidentally connecting to production data.
🎯 Type of Change
🔍 Changes Walkthrough
apps/web/src/server/email/outbox.tsCLAIM_TIMEOUT_MSfrom 10s to 15s to exceed PostgREST's 8s statement timeout. AddedisAbortErrorhelper to distinguish client-side aborts (where DB may have committed) from server-side rollbacks. Updated error handling insendDueEmailsto log specific warnings for aborted claims.apps/web/src/server/email/outbox.test.tsapps/web/src/server/email/provider.tsjwtProjectRefhelper to extract the Supabase project reference from JWT keys. UpdatedusesProductionDatabaseto check both the URL host and the JWTrefclaim, ensuring production detection works even if a custom domain is used that isn't in the static host list.apps/web/src/server/email/provider.test.tsusesProductionDatabaseto verify it correctly identifies production environments via JWT refs (service role and anon keys) and correctly ignores non-production or opaque keys.📊 Architectural Flow
flowchart TD subgraph "Email Outbox Execution" A[sendDueEmails] --> B{Claim Timeout?} B -- Yes --> C[AbortSignal.timeout 15s] B -- No --> D[Normal RPC] C --> E{Error Occurred?} E -- Yes --> F{isAbortError?} F -- Yes --> G[Log: 'May have committed'] F -- No --> H[Log: 'Claim failed'] E -- No --> I[Process Rows] end subgraph "Environment Detection" J[usesProductionDatabase] --> K{Check URL Host} K -- Match --> L[Return True] K -- No Match --> M{Check JWT Refs} M --> N[jwtProjectRef Service Key] M --> O[jwtProjectRef Anon Key] N -- Match Prod Ref --> L O -- Match Prod Ref --> L N -- No Match --> P[Return False] O -- No Match --> P end