navidrome/utils/files_windows_test.go
Deluan e42418db77 fix(windows): avoid filepath.Rel hang on UNC share roots
On Windows, filepath.Rel never returns when the base is a UNC share root
(\\host\share) and the target is the same root with a trailing separator
(golang/go#79784, fixed only in Go 1.28 by CL 786100). A user on Windows
Server with MusicFolder='\\server\Music' (discussion #6232) sent CPU
profiles showing 96% of CPU time in filepath.Rel, called from
fromArtistFolder <- resolveArtistFolderPattern <- resolver.resolveArtist.
For an album directly under the library root, loadArtistFolder's
filepath.Dir returns '\\server\Music\', so the artwork worker spins
forever and, because Worker.drain waits for the whole batch, the artist
artwork pool stops.

Add utils.RelPath, which normalizes a UNC volume root followed only by
separators to the volume plus one separator before calling filepath.Rel.
Both sides then compare equal and Rel returns '.' before reaching the
loop. A bare '\\host\share' target under a deeper base now also gives
'..' instead of an error. Drive-letter paths are left unchanged, and on
non-Windows systems the helper behaves exactly like filepath.Rel.

Use it at every production filepath.Rel call site: artist folder
artwork, scanner library-relative paths, the watcher, playlist path
resolution and PlaylistsPath matching, and the cache spread FS.

Windows-only tests cover the UNC cases, with a timeout so a regression
fails instead of hanging CI. Run under Wine, 6 of them fail with plain
filepath.Rel (4 by timeout) and all pass with RelPath. The helper can be
removed after moving to Go 1.28.
2026-09-27 15:25:19 -04:00

40 lines
1.5 KiB
Go

package utils_test
import (
"time"
"github.com/navidrome/navidrome/utils"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("RelPath on Windows", func() {
type result struct {
rel string
err error
}
DescribeTable("handles UNC share roots without hanging",
func(base, target, expected string) {
done := make(chan result, 1)
go func() {
rel, err := utils.RelPath(base, target)
done <- result{rel, err}
}()
var res result
Eventually(done).WithTimeout(5 * time.Second).Should(Receive(&res))
Expect(res.err).ToNot(HaveOccurred())
Expect(res.rel).To(Equal(expected))
},
Entry("root and root with trailing separator", `\\server\Music`, `\\server\Music\`, "."),
Entry("root with trailing separator and root", `\\server\Music\`, `\\server\Music`, "."),
Entry("root and root with many separators", `\\server\Music`, `\\server\Music\\`, "."),
Entry("root with forward slashes", `//server/Music`, `//server/Music/`, "."),
Entry("root and child folder", `\\server\Music`, `\\server\Music\Artist`, "Artist"),
Entry("root with trailing separator and child folder", `\\server\Music\`, `\\server\Music\Artist\Album`, `Artist\Album`),
Entry("child folder and root", `\\server\Music\Artist`, `\\server\Music\`, ".."),
Entry("child folder and root without trailing separator", `\\server\Music\Artist`, `\\server\Music`, ".."),
Entry("root and root with different case", `\\server\Music`, `\\SERVER\music\`, "."),
Entry("drive root and child folder", `C:\`, `C:\Music`, "Music"),
)
})