You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(mailer): set a connection deadline before the SMTP greeting is read - #2063
An email send hangs forever when the SMTP server accepts the connection but never sends its greeting. In gopkg.in/mail.v2, Dialer.Dial reads the greeting (inside smtpNewClient) before it sets the connection deadline from Dialer.Timeout, so that first read has no deadline. Every mail path goes through this dialer — login OTP, magic link, invitations, PAT alerts, org and user delete notices — and most run inside an HTTP request, so the request hangs with the send.
Changes
pkg/mailer/dialer.go: NewDialerImpl now installs (once, via sync.Once) a wrapper around the library's mail.NetDialTimeout hook that sets a deadline on each new connection as soon as it opens, so the greeting read is covered. After the greeting, the library re-arms the same deadline as before, so the rest of the session is unchanged.
pkg/mailer/dialer_test.go: new tests for a silent server and a cooperative server.
Technical Details
The deadline is derived from the timeout argument the library passes to the hook for each dial, so every dialer keeps its own Timeout, and a timeout of zero keeps the library's previous unbounded behaviour. If setting the deadline fails, the connection is closed and the error returned.
NetDialTimeout is a package-level hook, so the wrapper applies process-wide; pkg/mailer is the only place in the repo that creates a mail dialer (grep NewDialer / NewDialerImpl call sites).
The alternative from the issue — moving to an SMTP library whose send takes a context — is a much larger change; this stays within the seam the library already exposes.
Disclosure:@RyoKusnadi already implemented a fix for this in #2043 and closed it himself on 2026-11-08 ("happy to reopen if it's useful"), without any maintainer review or rejection. This PR covers the same ground, written independently from the current code — the main difference is that the deadline here comes from the per-dial timeout argument rather than a captured dialer timeout. Full credit to him for identifying the approach first. If he reopens #2043, I will close this PR in favour of his.
Test Plan
Manual testing completed (not done against a real mail server)
Build and type checking passes
TestDialerImpl_DialAndSend_SilentServer: a local listener accepts and stays silent; with a 300 ms dialer timeout the send must fail with a timeout error instead of blocking.
Before the fix: FAIL — DialAndSend still blocked after 10s: no deadline on the greeting read
After the fix: passes in ~0.3 s
TestDialerImpl_DialAndSend_CooperativeServer: a minimal fake SMTP server completes a full delivery; passes before and after (regression guard).
go test ./pkg/mailer/ — ok; also go test ./pkg/mailer/ -race -count=2 — ok. go vet ./pkg/mailer/... and gofmt -l pkg/mailer clean. The full repo suite was not run.
No actionable comments were generated in the recent review.
Summary
Summary by CodeRabbit
Bug Fixes
Mail delivery now applies the configured timeout to network connections. When a mail server does not respond in time, the delivery attempt can time out instead of remaining stalled. Connections that cannot have a deadline applied are closed, and the resulting error is returned.
The mail dialer now installs a one-time wrapper that sets a deadline on successful connections with a positive timeout. Tests cover a silent SMTP server and a cooperative server.
Changes: SMTP greeting timeout
Layer / File(s)
Summary
Install the connection deadline wrapper pkg/mailer/dialer.go
A one-time wrapper sets a deadline after a successful dial when the timeout is positive. NewDialerImpl installs it before creating the mail dialer.
raystack/frontier#2043: Addresses the same silent SMTP greeting issue by wrapping mail.NetDialTimeout to set a connection deadline.
Suggested reviewers:rohilsurana
Merge Risk: Minimal · up to 9c4c7
This change prevents email sends from hanging when an SMTP server accepts a connection but never sends its greeting. No merge-blocking risk was identified.
Pre-merge checks: 2
Check name
Status
Explanation
Linked Issues check
Issue #2039 asks that an SMTP greeting read not block forever when the server accepts a connection and stays silent. It offers two approaches: set a deadline right after the connection opens by wrappi…
Out of Scope Changes check
The reviewed changes touch only pkg/mailer/dialer.go and pkg/mailer/dialer_test.go. The dialer.go hunk adds the imports and the greeting-deadline wrapper that the issue's first proposed fix requires. …
Full details: Linked Issues check
Explanation
Issue #2039 asks that an SMTP greeting read not block forever when the server accepts a connection and stays silent. It offers two approaches: set a deadline right after the connection opens by wrapping mail.NetDialTimeout, or move to a context-aware SMTP library. At head, pkg/mailer/dialer.go implements the first approach. installGreetingDeadline wraps mail.NetDialTimeout once via sync.Once and calls conn.SetDeadline(time.Now().Add(timeout)) on each new connection before the library reads the greeting. NewDialerImpl calls installGreetingDeadline before mail.NewDialer. A timeout of zero or less keeps the unbounded behaviour, and a SetDeadline failure closes the connection and returns the error. The PR adds TestDialerImpl_DialAndSend_SilentServer and TestDialerImpl_DialAndSend_CooperativeServer in dialer_test.go. I did not read the test file contents, so test behaviour rests on the PR description and the code path. The code path itself matches the issue's requirement. The non-coding items (manual testing against a real server, full repo suite) do not apply to the verdict.
Full details: Out of Scope Changes check
Explanation
The reviewed changes touch only pkg/mailer/dialer.go and pkg/mailer/dialer_test.go. The dialer.go hunk adds the imports and the greeting-deadline wrapper that the issue's first proposed fix requires. The test additions verify that fix. The PR description's disclosure about #2043 and the closed-issue history are narrative, not code changes, and they do not add out-of-scope behaviour. No unrelated source changes were identified in the summary or the dialer.go file.
Finishing Touches
Fix failing CI checks
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Reviewing files that changed from the base of the PR and between 4e33f29 and 9c4c759.
Files selected for processing: 2
pkg/mailer/dialer.go
pkg/mailer/dialer_test.go
Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.
This branch has not been deployed
No deployments
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
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.
Summary
An email send hangs forever when the SMTP server accepts the connection but never sends its greeting. In
gopkg.in/mail.v2,Dialer.Dialreads the greeting (insidesmtpNewClient) before it sets the connection deadline fromDialer.Timeout, so that first read has no deadline. Every mail path goes through this dialer — login OTP, magic link, invitations, PAT alerts, org and user delete notices — and most run inside an HTTP request, so the request hangs with the send.Changes
pkg/mailer/dialer.go:NewDialerImplnow installs (once, viasync.Once) a wrapper around the library'smail.NetDialTimeouthook that sets a deadline on each new connection as soon as it opens, so the greeting read is covered. After the greeting, the library re-arms the same deadline as before, so the rest of the session is unchanged.pkg/mailer/dialer_test.go: new tests for a silent server and a cooperative server.Technical Details
timeoutargument the library passes to the hook for each dial, so every dialer keeps its ownTimeout, and a timeout of zero keeps the library's previous unbounded behaviour. If setting the deadline fails, the connection is closed and the error returned.NetDialTimeoutis a package-level hook, so the wrapper applies process-wide;pkg/maileris the only place in the repo that creates a mail dialer (grep NewDialer/NewDialerImplcall sites).Disclosure: @RyoKusnadi already implemented a fix for this in #2043 and closed it himself on 2026-11-08 ("happy to reopen if it's useful"), without any maintainer review or rejection. This PR covers the same ground, written independently from the current code — the main difference is that the deadline here comes from the per-dial timeout argument rather than a captured dialer timeout. Full credit to him for identifying the approach first. If he reopens #2043, I will close this PR in favour of his.
Test Plan
Manual testing completed (not done against a real mail server)
Build and type checking passes
TestDialerImpl_DialAndSend_SilentServer: a local listener accepts and stays silent; with a 300 ms dialer timeout the send must fail with a timeout error instead of blocking.FAIL — DialAndSend still blocked after 10s: no deadline on the greeting readTestDialerImpl_DialAndSend_CooperativeServer: a minimal fake SMTP server completes a full delivery; passes before and after (regression guard).go test ./pkg/mailer/— ok; alsogo test ./pkg/mailer/ -race -count=2— ok.go vet ./pkg/mailer/...andgofmt -l pkg/mailerclean. The full repo suite was not run.Fixes #2039