Commit Graph
1 Commits
Author SHA1 Message Date
forever 0dd821c130 provider: a forget, a TUI group edit or a failed group save no longer changes the record or groups every other reader holds (#1124)
* provider: forgetting a moved subscription's account no longer changes the record every other reader holds, so a forget whose write fails can't leave moving back an account with no key

setMigration copied only the outer map of migrations.json as filememo
had parsed it. The record handed to its callback still shared its
Accounts and Backup with that parse, which every MigrationOf reader gets
too. forgetPluginAccounts' callback deletes from both lists in place
(slices.DeleteFunc), which shifts the entries and zeroes the tail of the
shared arrays. putBack and moveBack read those lists without
migrationsMu, so that was a data race. When the write then failed, the
file was unchanged and filememo kept serving the changed parse: the same
length, the forgotten account replaced by the next and a last entry with
no key, which a move back would hand back. setMigration now gives the
callback its own copy of both lists (and of each account's Was).

Other readers of shared parses were checked (migrations, logins, credits,
quota history, settings, catalog and plugin caches): none changes what
it reads. TUI group editing changes heldOf("file")'s groups in place
while a Hold is open; that is a different cache and goes in its own
change.

Tested: TestFailedMigrationWriteLeavesItAsRead fails without the fix
(read as accounts ["zed:b" ""], backup ["b@example.com" ""]) and passes
with it. TestMigrationReadWhileForgotten fails with DATA RACE in 9 of 10
separate -race runs without the fix and passes 20 of 20 with it.
go test -race -tags nogui ./internal/provider, go vet ./..., go build,
GOOS=linux go build -tags nogui and GOOS=windows go build pass.

* provider: a group edit the TUI makes, or a save that fails, no longer changes the groups every other reader holds

While any goroutine holds provider.Hold() (SyncCatalog after every store,
the in-process gateway's /models and model routes), groupsIn hands every
caller the same providers.json, and each Group copied out of it still
shared its Members, Match, Matched, Off, Levels, Fast and Rules (with each
rule's Agents and Time) with that file; withMatches cloned Members only
for a group with patterns. The TUI's saveGroup changes the group it read
in place before saving it (slices.Delete/DeleteFunc/Insert on Rules and
Members, swaps, RenameMember), and SaveGroup itself wrote Rules[i].Use and
appended to Fast in the caller's lists. That raced with SyncCatalog's and
the gateway's group reads, and a save that failed ("every model in X is
switched off") left the held groups shifted with an empty member or rule
at the end for up to a second, where SyncCatalog could write agents'
model files from them.

groupsIn now gives each group lists of its own (Group.own), and SaveGroup
cleans its own copy, so the caller's group is as it gave it, saved or not.
RenameGroup and SetAutoGroups read through groupsIn and are covered too.
The CLI's in-place edits (groups_cli.go, groups_rules_cli.go) run with no
hold and read a fresh file; they are covered all the same.

Found by the sibling sweep for #1124.

Verified: TestHeldGroupsAreTheCallers (internal/provider) and
TestGroupTakeOutRefusedWhileHeld (internal/tui) hold the catalog, take a
model out of a group whose other one is off, as the TUI's d does, and
read the groups again. Without the change both fail: members
[b/big ""] / [b/gpt-5.5 ""] and a zeroed rule in the held groups. With
only SaveGroup's copy removed, the first fails with "SaveGroup changed the
caller's rule". go test -tags nogui -race ./internal/provider
./internal/tui, go test -tags nogui ./..., go vet ./..., go build,
GOOS=linux go build -tags nogui and GOOS=windows go build pass.
2026-10-13 09:50:57 +08:00