Repository navigation
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What kind of change does this PR introduce?
Bug fix.
What is the current behavior?
Part of #2888.
UnenrollFactorcommits 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,timeoutMiddlewareanswers504 request_timeoutfor a change that is already committed. The reporter's gateway retried theDELETEand the second attempt returned404 mfa_factor_not_found.Two things make this possible:
mailmeclient.Mailignores itsctx.gomail.v2DialAndSendonly 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.GOTRUE_MAILER_EMAIL_BACKGROUND_SENDINGdoes not help here.apitask.Middlewareruns insidetimeoutMiddlewareand waits for all tasks before the handler returns, andtimeoutMiddlewarebuffers 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.Mailreturnsctx.Err()when the context is done instead of waiting for the SMTP exchange. It does not change whether a mail is sent:gomailcannot 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 aftertimeoutMiddlewarehas answered. Transactions are already bound to the request context, so this does not change what gets committed either; callers only stop waiting sooner.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.apitaskwith background sending enabled.MaxRequestDurationset to 0 there is no request deadline and nothing changes.What this PR does not fix
user.gopassword changed,identity.goidentity linked, and theverify.goemail 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 withMaxRequestDuration = 1sand a mailer that blocks until its context is done. Onmasterit fails with the504 request_timeoutfrom 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:Mailreturnscontext.DeadlineExceededagainst an SMTP server that never sends its greeting (hangs until thego testtimeout onmaster), still contacts the SMTP server when the context is already done, and still returns the send error when the connection is refused.go vetand-raceare clean on the changed packages, and the fullinternal/api/...andinternal/mailer/...suites pass locally.🤖 Generated with Claude Code