Skip to content

🎧 Your Personal Streaming Service

Close stream when downloading files, fix fd leak

Deluanb8c5e49dd3b1
diff --git a/server/subsonic/stream.go b/server/subsonic/stream.go
index 1cb5a240b..1b01a2025 100644
--- a/server/subsonic/stream.go
+++ b/server/subsonic/stream.go
@@ -127,11 +127,17 @@ func (api *Router) Download(w http.ResponseWriter, r *http.Request) (*responses.
switch v := entity.(type) {
case *model.MediaFile:
stream, err := api.streamer.NewStream(ctx, id, format, maxBitRate)
-
if err != nil {
return nil, err
}
+ // Make sure the stream will be closed at the end, to avoid leakage
+ defer func() {
+ if err := stream.Close(); err != nil && log.CurrentLevel() >= log.LevelDebug {
+ log.Error("Error closing stream", "id", id, "file", stream.Name(), err)
+ }
+ }()
+
disposition := fmt.Sprintf("attachment; filename=\"%s\"", stream.Name())
w.Header().Set("Content-Disposition", disposition)