fix: remove premature cleanup from onebot, fix discord audio file leak

- Remove defer cleanup of LocalFiles in onebot channel handler, same
  async race condition as the other channels.
- Remove the now-unused LocalFiles field from parseMessageResult.
- Add downloaded audio files to mediaPaths in discord channel so they
  are tracked (previously only tracked via the now-removed localFiles).
This commit is contained in:
Together 2026-02-21 01:36:14 +08:00
parent e91c68ed34
commit 7836f21621
2 changed files with 9 additions and 23 deletions

View file

@ -220,6 +220,7 @@ func (c *DiscordChannel) handleMessage(s *discordgo.Session, m *discordgo.Messag
if isAudio { if isAudio {
localPath := c.downloadAttachment(attachment.URL, attachment.Filename) localPath := c.downloadAttachment(attachment.URL, attachment.Filename)
if localPath != "" { if localPath != "" {
mediaPaths = append(mediaPaths, localPath)
transcribedText := "" transcribedText := ""
if c.transcriber != nil && c.transcriber.IsAvailable() { if c.transcriber != nil && c.transcriber.IsAvailable() {
ctx, cancel := context.WithTimeout(c.getContext(), transcriptionTimeout) ctx, cancel := context.WithTimeout(c.getContext(), transcriptionTimeout)

View file

@ -4,7 +4,6 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"os"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -571,7 +570,6 @@ type parseMessageResult struct {
Text string Text string
IsBotMentioned bool IsBotMentioned bool
Media []string Media []string
LocalFiles []string
ReplyTo string ReplyTo string
} }
@ -603,7 +601,6 @@ func (c *OneBotChannel) parseMessageSegments(raw json.RawMessage, selfID int64)
mentioned := false mentioned := false
selfIDStr := strconv.FormatInt(selfID, 10) selfIDStr := strconv.FormatInt(selfID, 10)
var media []string var media []string
var localFiles []string
var replyTo string var replyTo string
for _, seg := range segments { for _, seg := range segments {
@ -642,7 +639,6 @@ func (c *OneBotChannel) parseMessageSegments(raw json.RawMessage, selfID int64)
}) })
if localPath != "" { if localPath != "" {
media = append(media, localPath) media = append(media, localPath)
localFiles = append(localFiles, localPath)
textParts = append(textParts, fmt.Sprintf("[%s]", segType)) textParts = append(textParts, fmt.Sprintf("[%s]", segType))
} }
} }
@ -656,7 +652,6 @@ func (c *OneBotChannel) parseMessageSegments(raw json.RawMessage, selfID int64)
LoggerPrefix: "onebot", LoggerPrefix: "onebot",
}) })
if localPath != "" { if localPath != "" {
localFiles = append(localFiles, localPath)
if c.transcriber != nil && c.transcriber.IsAvailable() { if c.transcriber != nil && c.transcriber.IsAvailable() {
tctx, tcancel := context.WithTimeout(c.ctx, 30*time.Second) tctx, tcancel := context.WithTimeout(c.ctx, 30*time.Second)
result, err := c.transcriber.Transcribe(tctx, localPath) result, err := c.transcriber.Transcribe(tctx, localPath)
@ -703,7 +698,6 @@ func (c *OneBotChannel) parseMessageSegments(raw json.RawMessage, selfID int64)
Text: strings.TrimSpace(strings.Join(textParts, "")), Text: strings.TrimSpace(strings.Join(textParts, "")),
IsBotMentioned: mentioned, IsBotMentioned: mentioned,
Media: media, Media: media,
LocalFiles: localFiles,
ReplyTo: replyTo, ReplyTo: replyTo,
} }
} }
@ -824,19 +818,10 @@ func (c *OneBotChannel) handleMessage(raw *oneBotRawEvent) {
} }
} }
// Clean up temp files when done // Note: media files in os.TempDir()/picoclaw_media/ are not cleaned up here
if len(parsed.LocalFiles) > 0 { // because HandleMessage publishes to an async message bus. The consumer
defer func() { // goroutine may still need these files after this function returns.
for _, f := range parsed.LocalFiles { // Temp files are managed by OS temp directory lifecycle.
if err := os.Remove(f); err != nil {
logger.DebugCF("onebot", "Failed to remove temp file", map[string]interface{}{
"path": f,
"error": err.Error(),
})
}
}
}()
}
if c.isDuplicate(messageID) { if c.isDuplicate(messageID) {
logger.DebugCF("onebot", "Duplicate message, skipping", map[string]interface{}{ logger.DebugCF("onebot", "Duplicate message, skipping", map[string]interface{}{