plugin: Settle starts no refresh while it settles (#2449)

Settle's own Restart tells the OnChange hooks, and one that calls
Cached() started refreshProvidersInBackground after Settle's last
refreshing.Wait. callWithTimeout starts the host under WithoutCancel, so
it went on once Settle returned and spawned a host in the test's removed
HOME ("Module not found host-....js", uv_cwd) or in the next test's,
while that one signed in. provSettling keeps Cached and the loop's
continue branch from starting one while Settle runs.

TestSettleStartsNoRefresh: red without the fix ("Settle started 3
refreshes"), green with it, also -race -count=20. In go test -v
./internal/cli the stray host lines went from 7 to 0.

Run: internal/cli, internal/plugin, internal/provider whole ok; gofmt -l
clean; go vet -tags nogui for darwin, linux, windows ok.
Full suite: go test -tags nogui -timeout 45m ./... at 7fefbc614 with both #2449 commits (bd4e9da7b's and this one's changes) (sandbox HOME, macOS): exit 0, 66 ok, 18 no test files, 0 FAIL.
This commit is contained in:
yetone
2026-10-16 05:29:20 +08:00
parent 6641e1b0ac
commit 008571462b
2 changed files with 49 additions and 2 deletions
+18 -2
View File
@@ -208,6 +208,9 @@ var (
// provEpoch counts the invalidations (forgetProviders), so a refresh
// that began before one doesn't put what it read over the newer state.
provEpoch uint64
// provSettling is set while Settle runs: no refresh starts, nor goes
// on to a newer invalidation, to start a host in a folder going away.
provSettling bool
)
func providersPath() string { return filepath.Join(settings.Dir(), "plugin-providers.json") }
@@ -355,7 +358,7 @@ func refreshProvidersInBackground(ctx context.Context, epoch uint64) {
}
provMu.Lock()
provCancel()
if provEpoch != epoch && !provGood && !provTried {
if provEpoch != epoch && !provGood && !provTried && !provSettling {
epoch = provEpoch
ctx, provCancel = context.WithCancel(context.Background())
provTried = true
@@ -537,7 +540,20 @@ func Answered() bool {
// the next test starts with nothing of this one's running or seen. Each
// ask is bounded and the retries are few, so this waits a bounded time
// even for a host that never answers.
//
// No refresh starts meanwhile: one a hook started after the last wait
// went on once Settle had returned, and its host started in the test's
// folder as it went, or in the next test's, while that one signed in
// (#2449).
func Settle() {
provMu.Lock()
provSettling = true
provMu.Unlock()
defer func() {
provMu.Lock()
provSettling = false
provMu.Unlock()
}()
refreshing.Wait()
Restart()
// what the host said of a sign-in is written before the folder goes
@@ -617,7 +633,7 @@ func Cached() []Provider {
// Whether one is already in flight is read and set under one lock,
// so two callers asking at once start one refresh, not two.
provMu.Lock()
start := !provBusy && !provGood && !provTried
start := !provBusy && !provGood && !provTried && !provSettling
var ctx context.Context
if start {
ctx, provCancel = context.WithCancel(context.Background())
@@ -382,3 +382,34 @@ func TestRefreshInvalidationCancelsStaleCall(t *testing.T) {
t.Fatal(asks.Load(), Cached())
}
}
// settleLooks has a hook told of a change look at the providers, as
// catalog.Touched's SyncCatalog does, while it is set.
var settleLooks atomic.Bool
func init() {
OnChange(func() {
if settleLooks.Load() {
Cached()
}
})
}
// Settle starts no refresh, through a hook its own restarts tell: one
// started after its last wait went on once it had returned, and started
// a host in the test's folder as it was removed, or in the next test's
// while that one signed in (#2449).
func TestSettleStartsNoRefresh(t *testing.T) {
var asks atomic.Int64
refreshSandbox(t, func(context.Context, uint64) error {
asks.Add(1)
return nil
})
settleLooks.Store(true)
Settle()
settleLooks.Store(false)
refreshing.Wait()
if n := asks.Load(); n != 0 {
t.Fatalf("Settle started %d refreshes", n)
}
}