Files
navidrome/plugins/manager_loader_load_test.go
Deluan Quintão 205c85da55 fix(plugins): surface host service failures when loading plugins (#5756)
* fix(plugins): surface host service failures when loading plugins

When a host service failed to initialize during plugin load (e.g. the
taskqueue database could not be created because the data folder is not
writable), the error was logged and swallowed, and its host functions were
silently omitted. Instantiation then failed with a misleading error such as
'"task_createqueue" is not exported in module "extism:host/user"', which
reads as a plugin/host API mismatch and gets wrongly blamed on plugin
authors (see kgarner7/navidrome-listenbrainz-daily-playlist#26).

Host service factories now return an error, and loadPluginWithConfig fails
fast with the actual cause (e.g. 'creating Task service: creating plugin
data directory: ...'), which is also stored in the plugin's last_error.
Closers accumulated before a load failure are now closed (via a deferred
guard covering all failure paths), so partially-created services no longer
leak goroutines or database handles.

Also fix a panic in 'navidrome plugin enable': CLI commands use the plugin
Manager without calling Start, so manager.ctx was nil and
newTaskQueueService panicked in context.WithCancel. Long-lived host
services (taskqueue, kvstore, websocket) now receive their lifecycle
context explicitly in the constructor, sourced from serviceContext.baseCtx(),
which falls back to context.Background() for the unstarted-manager case.

* docs(plugins): correct websocket readLoop lifecycle comment

The comment claimed the read loop's context is always cancelled during
application shutdown, which is not true when the manager was never started
(one-shot CLI runs, where baseCtx falls back to context.Background()).
Clarify that connection closure via Close() on plugin unload is what ends
the read loop, with context cancellation as a server-shutdown backstop.

Addresses review feedback on #5756.

* test(plugins): exclude plugin-loading specs from Windows builds

The new loadPluginWithConfig specs reference test suite helpers
(testdataDir, noopMetricsRecorder) defined in plugins_suite_test.go, which
is excluded on Windows, breaking test compilation there. Move the specs to
their own file with the same build constraint, keeping the pure-function
specs in manager_loader_test.go running on Windows as before.
2026-07-10 19:15:49 -04:00

79 lines
2.4 KiB
Go

//go:build !windows
package plugins
import (
"crypto/sha256"
"encoding/hex"
"net/http"
"os"
"path/filepath"
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/conf/configtest"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/tests"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("loadPluginWithConfig", func() {
var manager *Manager
var dataDir string
BeforeEach(func() {
pluginsDir := GinkgoT().TempDir()
dataDir = GinkgoT().TempDir()
src := filepath.Join(testdataDir, "test-taskqueue"+PackageExtension)
data, err := os.ReadFile(src)
Expect(err).ToNot(HaveOccurred())
dest := filepath.Join(pluginsDir, "test-taskqueue"+PackageExtension)
Expect(os.WriteFile(dest, data, 0600)).To(Succeed())
hash := sha256.Sum256(data)
DeferCleanup(configtest.SetupConfig())
conf.Server.Plugins.Enabled = true
conf.Server.Plugins.Folder = conf.NewDir(pluginsDir)
conf.Server.Plugins.AutoReload = false
conf.Server.DataFolder = conf.NewDir(dataDir)
repo := tests.CreateMockPluginRepo()
repo.Permitted = true
repo.SetData(model.Plugins{{
ID: "test-taskqueue",
Path: dest,
SHA256: hex.EncodeToString(hash[:]),
Enabled: false,
}})
manager = &Manager{
plugins: make(map[string]*plugin),
ds: &tests.MockDataStore{MockedPlugin: repo},
metrics: noopMetricsRecorder{},
subsonicRouter: http.NotFoundHandler(),
}
})
Describe("host service creation failures", func() {
It("reports the Task service creation error instead of a missing host function", func() {
Expect(manager.Start(GinkgoT().Context())).To(Succeed())
DeferCleanup(func() { _ = manager.Stop() })
// Block the taskqueue data dir by creating a file where the directory should be
Expect(os.WriteFile(filepath.Join(dataDir, "plugins"), nil, 0600)).To(Succeed())
err := manager.EnablePlugin(GinkgoT().Context(), "test-taskqueue")
Expect(err).To(MatchError(ContainSubstring("creating Task service")))
Expect(err).ToNot(MatchError(ContainSubstring("not exported")))
})
})
Describe("unstarted manager", func() {
It("enables a taskqueue plugin on a manager that was never started", func() {
// CLI commands (navidrome plugin enable) use the manager without calling Start
Expect(manager.EnablePlugin(GinkgoT().Context(), "test-taskqueue")).To(Succeed())
DeferCleanup(func() { _ = manager.unloadPlugin("test-taskqueue") })
})
})
})