Skip to content

fix(mailer): set a connection deadline before the SMTP greeting is read - #2063

Open
0xABCD01 wants to merge 1 commit into
raystack:mainfrom
0xABCD01:fix/2039-smtp-greeting-deadline
Open

0xABCD01 wants to merge 1 commit into
raystack:mainfrom
0xABCD01:fix/2039-smtp-greeting-deadline

Conversation

@0xABCD01

Copy link
Copy Markdown

Summary

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.

Fixes #2039

@vercel

vercel Bot commented Nov 25, 2026

Copy link
Copy Markdown

@0xABCD01 is attempting to deploy a commit to the Raystack Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

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.
Verify SMTP greeting behavior
pkg/mailer/dialer_test.go
Tests check that delivery to a silent server times out and that delivery to a cooperative fake SMTP server succeeds.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

  • 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: Passed 2

Check name Status Explanation
Linked Issues check Passed 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 Passed 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.

Share

Recent review info

Run configuration

Commits

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
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.

An SMTP server that never sends its greeting makes an email send hang

1 participant