From a31403c547ab1cc929c49454ae70d98765acbc13 Mon Sep 17 00:00:00 2001 From: XZB-1248 <28593573+XZB-1248@users.noreply.github.com> Date: Thu, 19 Feb 2026 22:07:18 +0800 Subject: [PATCH] fix: potential handle leak when send attachments on slack --- pkg/channels/slack.go | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/pkg/channels/slack.go b/pkg/channels/slack.go index a27cd1b7d..83b866388 100644 --- a/pkg/channels/slack.go +++ b/pkg/channels/slack.go @@ -155,24 +155,28 @@ func (c *SlackChannel) Send(ctx context.Context, msg bus.OutboundMessage) error func (c *SlackChannel) sendWithAttachments(ctx context.Context, channelID, threadTS, content string, attachments []bus.Attachment) error { for _, attachment := range attachments { - file, err := os.Open(attachment.Path) - if err != nil { - return fmt.Errorf("failed to open attachment %s: %w", attachment.Path, err) - } + uploadErr := func() error { + file, err := os.Open(attachment.Path) + if err != nil { + return fmt.Errorf("failed to open attachment %s: %w", attachment.Path, err) + } + defer file.Close() - params := slack.UploadFileV2Parameters{ - Channel: channelID, - Filename: attachment.Filename, - Reader: file, - InitialComment: content, - ThreadTimestamp: threadTS, - } + params := slack.UploadFileV2Parameters{ + Channel: channelID, + Filename: attachment.Filename, + Reader: file, + InitialComment: content, + ThreadTimestamp: threadTS, + } - _, err = c.api.UploadFileV2Context(ctx, params) - defer file.Close() - - if err != nil { - return fmt.Errorf("failed to upload file %s: %w", attachment.Filename, err) + if _, err = c.api.UploadFileV2Context(ctx, params); err != nil { + return fmt.Errorf("failed to upload file %s: %w", attachment.Filename, err) + } + return nil + }() + if uploadErr != nil { + return uploadErr } // Only use content for first attachment to avoid duplicate comments