Repository navigation
Conversation
|
@0xABCD01 is attempting to deploy a commit to the Raystack Team on Vercel. A member of the Team first needs to authorize it. |
|
No actionable comments were generated in the recent review. Summary
Merge Risk:
|
rohilsurana
left a comment
There was a problem hiding this comment.
Thanks for the fix. It does what issue #2039 asks, and I found no security problems. Only pkg/mailer/dialer.go and the new test file change. The wrapper passes the same network, address and timeout to the original dial. It adds no new network calls and does not touch credentials or TLS settings. The tests only listen on loopback.
I ran the same tests before and after the fix, scoped to pkg/mailer. For "before", I put the dialer.go from main back and kept the tests. I also added two tests of my own: a silent server with the default 10s timeout through the public API, and a silent server with SSL = true (the port 465 path).
| Test | main | this PR |
|---|---|---|
TestDialerImpl_DialAndSend_SilentServer |
FAIL, still blocked after 10s | PASS, 0.30s |
TestDialerImpl_DialAndSend_CooperativeServer |
PASS | PASS |
| My silent server, default 10s timeout | FAIL, still blocked after 25s | PASS, i/o timeout after 10.0s |
| My silent server, SSL on, 500ms timeout | FAIL, still blocked after 15s | PASS, i/o timeout after 0.5s |
go test -race on the new tests passes. golangci-lint run ./pkg/mailer/... reports 0 issues. gofmt is clean. The Go CI workflows have not run on this PR yet.
The inline comments are small. The main ask is a test for the SSL path, since port 465 is a common setup.
|
|
||
| addr := listener.Addr().(*net.TCPAddr) | ||
| impl := NewDialerImpl(addr.IP.String(), addr.Port, "", "", true, nil, mail.NoStartTLS) | ||
| impl.dialer.Timeout = 300 * time.Millisecond |
There was a problem hiding this comment.
Could you add a second silent server test with impl.dialer.SSL = true? There the deadline also has to cover the TLS handshake. I checked it locally and it passes with your change and hangs on main, but nothing in the PR guards it. A case with Timeout = 0 against the cooperative server would also be nice, to show the zero timeout path still works.
| dial := mail.NetDialTimeout | ||
| mail.NetDialTimeout = func(network, addr string, timeout time.Duration) (net.Conn, error) { | ||
| conn, err := dial(network, addr, timeout) | ||
| if err != nil || timeout <= 0 { |
There was a problem hiding this comment.
Small guard idea: if err != nil || conn == nil || timeout <= 0. net.DialTimeout never returns a nil conn with a nil error, but a future hook might, and then SetDeadline would panic.
| if err != nil || timeout <= 0 { | ||
| return conn, err | ||
| } | ||
| if err := conn.SetDeadline(time.Now().Add(timeout)); err != nil { |
There was a problem hiding this comment.
When the greeting read times out, mail.v2 returns from Dial without closing the conn (smtp.go:91-94). So each timed out send now leaves one socket open until the garbage collector closes it. That is still far better than hanging forever. It may be worth a follow up, either upstream or by returning a wrapped conn here.
|
|
||
| func NewDialerImpl(SMTPHost string, SMTPPort int, SMTPUser string, SMTPPass string, | ||
| SMTPInsecure bool, headers map[string]string, tlsPolicy mail.StartTLSPolicy) *DialerImpl { | ||
| installGreetingDeadline() |
There was a problem hiding this comment.
The constructor changes a package level hook in the library. sync.Once stops a double install, but the write is not synced with a Dial that may be running in another goroutine. That is fine today, since the only caller is cmd/serve.go at startup. Installing it in a package init() would remove the question for good. Not a blocker.
Add the conn == nil check rohilsurana suggested before SetDeadline, plus tests for the SSL silent-server path and the Timeout = 0 path against a cooperative server.
|
Pushed the three changes in 18c8309:
Results: On the two non-blockers, agreed on both. The socket left open when the greeting read times out sits inside mail.v2's |
|
|
||
| var greetingDeadlineOnce sync.Once | ||
|
|
||
| // installGreetingDeadline wraps the mail library's dial hook so that a |
There was a problem hiding this comment.
We can get rid of this comment. It explains how we got to this fix, which belongs in the PR description. The function name already says what it does.
| return msg | ||
| } | ||
|
|
||
| // A server that accepts the connection but never sends its greeting must |
There was a problem hiding this comment.
We can get rid of this comment. The test name can say what it checks, and the library detail belongs in the PR description.
| } | ||
| } | ||
|
|
||
| // Same silent server, but with SSL on: the deadline set at dial time also |
There was a problem hiding this comment.
Same here, we can drop this comment and let the test name say it.
| } | ||
| } | ||
|
|
||
| // A timeout of zero keeps the library's unbounded behaviour: no deadline |
There was a problem hiding this comment.
Same here, we can drop this comment and let the test name say it.
| } | ||
| } | ||
|
|
||
| // A server that answers normally must still complete the send: the |
There was a problem hiding this comment.
Same here, we can drop this comment and let the test name say it.
| } | ||
| } | ||
|
|
||
| // serveFakeSMTP speaks just enough SMTP for one successful delivery. |
There was a problem hiding this comment.
We can drop this one as well. The helper name already says what it does.
|
Done in d0dcfb7 — removed the six comments (dialer.go installGreetingDeadline header, plus the five test/helper comments in dialer_test.go). Comment-only change, no code touched. gofmt clean, For reference, the silent-server SSL test, the Timeout=0 case, and the nil-conn guard from your earlier review are already in the PR since the previous revision (18c8309), so nothing from that round was missed. The socket-leak follow-up and the init() note I left as follow-ups, not part of this change. |
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