Skip to content

Commit 0f414a0

Browse files
authored
fix: restore closing SMTP message on method exit (#14496)
1 parent a74273f commit 0f414a0

File tree

1 file changed

+10
-1
lines changed
  • coderd/notifications/dispatch

1 file changed

+10
-1
lines changed

coderd/notifications/dispatch/smtp.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,15 @@ func (s *SMTPHandler) dispatch(subject, htmlBody, plainBody, to string) Delivery
183183
if err != nil {
184184
return true, xerrors.Errorf("message transmission: %w", err)
185185
}
186+
closeOnce := sync.OnceValue(func() error {
187+
return message.Close()
188+
})
189+
// Close the message when this method exits in order to not leak resources. Even though we're calling this explicitly
190+
// further down, the method may exit before then.
191+
defer func() {
192+
// If we try close an already-closed writer, it'll send a subsequent request to the server which is invalid.
193+
_ = closeOnce()
194+
}()
186195

187196
// Create message headers.
188197
msg := &bytes.Buffer{}
@@ -250,7 +259,7 @@ func (s *SMTPHandler) dispatch(subject, htmlBody, plainBody, to string) Delivery
250259
return false, xerrors.Errorf("write body buffer: %w", err)
251260
}
252261

253-
if err = message.Close(); err != nil {
262+
if err = closeOnce(); err != nil {
254263
return true, xerrors.Errorf("delivery failure: %w", err)
255264
}
256265

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy