Skip to content

fix(api): bound post-commit notification emails by the request budget (#2888) - #2897

Open
melcheikh wants to merge 1 commit into
supabase:masterfrom
melcheikh:fix/bound-post-commit-notifications-2888
Open

melcheikh wants to merge 1 commit into
supabase:masterfrom
melcheikh:fix/bound-post-commit-notifications-2888

Conversation

@melcheikh

Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

Bug fix.

What is the current behavior?

Part of #2888.

UnenrollFactor commits the factor deletion, the audit entry and the session downgrade, and only then sends the "MFA factor unenrolled" notification. The notification runs on the request context, so a slow mail transport consumes the request budget (API.MaxRequestDuration, 10s by default). When it runs out, timeoutMiddleware answers 504 request_timeout for a change that is already committed. The reporter's gateway retried the DELETE and the second attempt returned 404 mfa_factor_not_found.

Two things make this possible:

  1. mailmeclient.Mail ignores its ctx. gomail.v2 DialAndSend only bounds the TCP connect, so an SMTP server that accepts the connection and stops responding blocks the handler until the server gives up. In the report the send failure was logged about 15s after the request started, after the 504.
  2. GOTRUE_MAILER_EMAIL_BACKGROUND_SENDING does not help here. apitask.Middleware runs inside timeoutMiddleware and waits for all tasks before the handler returns, and timeoutMiddleware buffers the response until then. That wait is intentional (feat: introduce request-scoped background tasks & async mail sending #2126), so this PR keeps it.

The same post-commit pattern exists in every handler that sends a notification after its transaction: factor enrolled (TOTP, phone, WebAuthn, recovery codes), recovery codes unenrolled and identity unlinked.

Scope

#2888 asks for two things: a committed change must not be reported as a failure, and the notification must still be delivered reliably. This PR only fixes the first one. It makes the response match what was committed. It does not make notification delivery more reliable (see "What this PR does not fix" below), so #2888 should stay open.

What is the new behavior?

  • mailmeclient.Mail returns ctx.Err() when the context is done instead of waiting for the SMTP exchange. It does not change whether a mail is sent: gomail cannot abort a send in progress, so the send keeps running in a goroutine until the SMTP exchange ends, exactly as it does today in the handler goroutine after timeoutMiddleware has answered. Transactions are already bound to the request context, so this does not change what gets committed either; callers only stop waiting sooner.
  • New API.notificationRequest(r) gives post-commit notifications a context that expires before the last fifth of the request budget (8s of 10s by default). The 7 post-commit call sites use it. A notification that does not finish in time is logged with the existing warning, and the handler answers with the result of the committed change.
  • The derived context is released with the request, not when the helper's caller returns, so it still bounds the mail when it runs as an apitask with background sending enabled.
  • No config changes. With MaxRequestDuration set to 0 there is no request deadline and nothing changes.

What this PR does not fix

  • Delivery is as fragile as before. If the SMTP server is slow or down, the notification can still be lost, and nothing retries it. The failure is logged, as it is today.
  • With an HTTP Send Email Hook, the hook call is cancelled when the notification deadline expires. A notification that today would arrive between 8s and 10s is dropped instead. Today the same call is already cancelled at 10s by the request deadline, so the window is small, but it is a change.
  • Notifications sent inside a transaction are not changed: user.go password changed, identity.go identity linked, and the verify.go email changed / phone changed / identity linked paths. There, cancelling the context early could abort the transaction when the Send Email Hook is a Postgres function, so the user change itself would be lost.

The fix for delivery is to stop tying security notifications to the request. One option is an outbox: write a pending notification row in the same transaction as the change, and have a background worker send it with retries. That makes delivery independent of the request, and it also covers the notifications that are sent inside a transaction today. It needs a new table and a worker, so it is a design decision for the Auth team. I am happy to write it if you want to go that way.

Tests

  • TestMFA/TestUnenrollFactorWithStalledNotification: unenroll with MaxRequestDuration = 1s and a mailer that blocks until its context is done. On master it fails with the 504 request_timeout from the report. With this change it returns 200, the factor is gone, and the mailer saw a deadline.
  • TestNotificationRequest: deadline math, no request deadline, MaxRequestDuration = 0, and the context ending with the request.
  • internal/mailer/mailmeclient: Mail returns context.DeadlineExceeded against an SMTP server that never sends its greeting (hangs until the go test timeout on master), still contacts the SMTP server when the context is already done, and still returns the send error when the connection is refused.

go vet and -race are clean on the changed packages, and the full internal/api/... and internal/mailer/... suites pass locally.

🤖 Generated with Claude Code

…supabase#2888)

Notification emails sent after a change is committed ran on the full
request context. A slow SMTP server could use up the whole request
budget, so timeoutMiddleware answered 504 for a change that had already
happened (e.g. a TOTP factor that was already removed), and a retry
then failed with mfa_factor_not_found.

- mailmeclient.Mail now returns ctx.Err() when the context is done
  instead of blocking until the SMTP exchange ends.
- Post-commit notifications (factor enrolled/unenrolled, recovery
  codes, identity unlinked) use a context that expires before the last
  fifth of API.MaxRequestDuration, so the handler always answers with
  the result of the committed change.

Notifications sent inside a transaction are left unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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