plugin: a page-only plugin's options are set under plugins.json's lock too (#4666)

0d6162c8a put every plugins.json change under the file's lock, but
SetPageOptions writes a page-only plugin's options itself, under listMu
alone, so another magpie's save meanwhile was still lost there. It now
takes listFileMu before listMu, as SetOptions does.

Run (mote, darwin/arm64):
- go test ./internal/plugin -run Meanwhile -v: the three Meanwhile tests ok
- page.go reverted with the test kept: TestPluginPageOptionsKeepWhatAnotherMagpieSetMeanwhile
  red ("SetPageOptions went on while another magpie held plugins.json's
  lock"); restored, green
- full suite (go test -tags nogui ./... -timeout 45m, mote) with this
  stack at b566b9d3c on 0b8c4bfc3: every package ok but two reds of that
  base, both fixed on main since: internal/cli
  TestEveryRouteHasACommandOrIsNamed (4c86b0ab4) and internal/tui
  TestEveryWordHasZh "permission mode" (e9fcc6229)
- rebased on f1c9014cd: go vet (darwin, linux nogui, windows) and
  go test of internal/gateway, internal/plugin, internal/cli and
  internal/tui at the pushed head: ok (gateway 899s, plugin 158s, cli, tui)
- go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet
  ./..., darwin/linux/windows builds: ok at b566b9d3c

Refs #4666
This commit is contained in:
yetone
2026-11-15 06:40:19 +08:00
parent 5aa365349a
commit ab7cb55371
2 changed files with 70 additions and 0 deletions
+4
View File
@@ -366,8 +366,12 @@ func SetPageOptions(spec string, opts map[string]any) error {
if w, only := Window(t); w == nil || !only {
return SetOptions(spec, opts)
}
// under plugins.json's lock too, as SetOptions is: another magpie's
// change saved meanwhile stands (#4666)
listFileMu.Lock()
listMu.Lock()
defer listMu.Unlock()
defer listFileMu.Unlock()
l := Load()
i := indexOf(l, spec)
if i < 0 {
+66
View File
@@ -1,6 +1,9 @@
package plugin
import (
"encoding/json"
"os"
"path/filepath"
"testing"
"time"
@@ -110,3 +113,66 @@ func TestPluginPreferKeepsWhatAnotherMagpieSetMeanwhile(t *testing.T) {
t.Errorf("p-b off = %v, want what the other magpie saved kept", b)
}
}
// A plugin that is only a page has its options set by SetPageOptions
// itself, not through SetOptions: it is under plugins.json's lock too.
func TestPluginPageOptionsKeepWhatAnotherMagpieSetMeanwhile(t *testing.T) {
storeSandbox(t)
dir := filepath.Join(t.TempDir(), "page-only")
if err := os.MkdirAll(filepath.Join(dir, "page"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "page", "index.html"), []byte("<p>hi</p>"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "package.json"), []byte(`{"name":"page-only","magpie":{"page":{"title":"P","file":"page/index.html"}}}`), 0o644); err != nil {
t.Fatal(err)
}
if w, only := Window(dir); w == nil || !only {
t.Fatalf("the fixture is no page-only plugin: %+v only=%v", w, only)
}
spec, _ := json.Marshal(dir)
writeList(t, `{"plugins":[{"spec":`+string(spec)+`},{"spec":"p-b","options":{"x":1}}]}`)
unlock, held := filelock.Lock(listPath()+".lock", filelock.Wait)
if !held {
t.Skip("this file system holds no locks")
}
defer unlock()
done := make(chan error, 1)
go func() { done <- SetPageOptions(dir, map[string]any{"ours": true}) }()
time.Sleep(300 * time.Millisecond)
select {
case err := <-done:
t.Fatalf("SetPageOptions went on while another magpie held plugins.json's lock (err %v)", err)
default:
}
other := Load()
for i := range other.Plugins {
if other.Plugins[i].Spec == "p-b" {
other.Plugins[i].Options = map[string]any{"theirs": float64(1)}
}
}
if err := save(other); err != nil {
t.Fatal(err)
}
unlock()
if err := <-done; err != nil {
t.Fatal(err)
}
l := Load()
var a, b *Entry
for i := range l.Plugins {
switch l.Plugins[i].Spec {
case dir:
a = &l.Plugins[i]
case "p-b":
b = &l.Plugins[i]
}
}
if a == nil || a.Options["ours"] != true {
t.Errorf("the page plugin's options = %v, want ours kept", a)
}
if b == nil || b.Options["theirs"] != float64(1) {
t.Errorf("p-b's options = %v, want what the other magpie saved kept", b)
}
}