Skip to content

Commit f7df1fd

Browse files
committed
fix: keep defer, but conditionalize
Signed-off-by: Danny Kopping <danny@coder.com>
1 parent c90be9b commit f7df1fd

File tree

1 file changed

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

1 file changed

+10
-0
lines changed

coderd/notifications/dispatch/smtp.go

Lines changed: 10 additions & 0 deletions
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+
closed := false
187+
// Close the message when this method exits in order to not leak resources. Even though we're calling this explicitly
188+
// further down, the method may exit before then.
189+
defer func() {
190+
// If we try close an already-closed writer, it'll send a subsequent request to the server which is invalid.
191+
if !closed {
192+
_ = message.Close()
193+
}
194+
}()
186195

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

262+
closed = true
253263
if err = message.Close(); err != nil {
254264
return true, xerrors.Errorf("delivery failure: %w", err)
255265
}

0 commit comments

Comments
 (0)