filememo: a write in a synctest bubble no longer crashes the next test (#3632)

091153bf (#3185) has Forget close the channel Written gives and make the
next one, and de49433f's wake, for another process's write that Watch
sees, does the same. A test that saves inside a synctest bubble writes
through Forget too, and a channel made in a bubble is that bubble's:
the next write from outside it, in another bubble or in none, closing
it is a fatal error. TestUnreadLoginsWriteErrorsKeepCredentials saves
in a bubble per case, so internal/provider's test binary dies in its
second bubble, on CI's ubuntu and macOS runs at 8299409e and 81a58d2f
and still at c158b043 (run 40265917236; a local -run prints the same
frames):

    fatal error: close of synctest channel from outside bubble

    goroutine 6032 [running, synctest bubble 2]:
    github.com/yetone/magpie/internal/filememo.Forget()
    	internal/filememo/filememo.go:91
    github.com/yetone/magpie/internal/provider.Changed()
    	internal/provider/hold.go:146
    github.com/yetone/magpie/internal/provider.writeLogins(...)
    	internal/provider/logins.go:272

Now Written makes the channel when there is none, and Forget and wake
close it only when one was taken, and make none (endWaits). Nothing
changes for the loops: the channel a loop takes is closed by the next
write, magpie's or another process's, and two loops taking it between
writes share one. A write with nothing taken since the last one had no
wait to end. Only the loops that follow magpie's files take it
(middleware's observer, OTel's session watch, pluginrt.KeepFresh,
awake's loop and the agent's Codex daemon check), so every channel is
made outside a bubble, and a bubble's write may close it.

A loop that took Written inside a bubble would make the channel that
bubble's in the same way, and it would outlive the bubble. None does. A
bubble lives only inside a synctest.Test call, and those are all in the
tests of internal/filememo, provider, plugin, otlp and gateway
(TestConcurrentClassification*, its only ones). With a probe in Forget,
wake and Written over those (a bubble's clock starts at 2000-01-01
00:00 UTC), provider's tests write twice in bubbles, filememo's new
test once in each of its four, and nothing takes Written in one.
provider's, plugin's and filememo's test binaries link none of those
five loops, so none of them can; otlp's and gateway's bubbles run an
exporter's send and classify. Written's comment says so.
internal/gateway's claudeRoom closes and remakes its freed channel the
same way, and no bubble reaches it either: #3690.

What was run (tree 8d629438, on origin/main c158b043, go1.26.8,
linux/amd64):
- TestWrittenAcrossBubbles (new), for Forget and for wake, each in
  bubbles of its own: two waits taken outside, two bubbles that each
  write, then a write outside; both waits are closed by the bubbles'
  writes, and the next one by the write after. Broken by hand, each put
  back after: c158b043's filememo.go under it, "fatal error: close of
  synctest channel from outside bubble"; Forget making the next channel
  again, the same; wake as on c158b043 (close, then make), the same, in
  wake's half; Written making a new channel on each call (two loops no
  longer share one), "Forget in a bubble left a wait taken outside it
  open"; endWaits without its nil check, "panic: close of nil channel".
  With the change ok, and -race -shuffle=on -count=20 ok. TestWritten
  and TestWatchWakesOnOthersWrites, unchanged, ok.
- TestUnreadLoginsWriteErrorsKeepCredentials: on c158b043 the fatal
  error in 4 of 4 runs; with the change it and the tests beside it
  (-run '^(TestUnreadLogins|TestReadableLoginsNotKeptAside)') ok at
  -count=20, and internal/provider whole ok with Bun 1.4.2.
- The probe above, not committed (with -v, since go test hides a passing
  package's output), over the tests with bubbles: Forget in a bubble 4,
  two in provider and two in filememo's new test; wake 2, filememo's;
  Written 0. Over the loops' packages (middleware, pluginrt, awake,
  agent), which have no synctest tests: nothing. In that run
  internal/plugin's TestPiPackage was red at its plugins' start
  ("starting plugins: context deadline exceeded"), #2865, and
  internal/agent with tests of #3141, #3398 and #3641; internal/plugin
  was ok in the suite below.
- gofmt -l clean; GOOS=linux, windows and darwin go vet -tags nogui
  ./..., GOOS=windows go vet ./..., GOOS=linux and darwin go build -tags
  nogui ./... and GOOS=windows go build ./...: exit 0 each. go vet and
  go build ./... without a tag on linux stop at pkg-config here (no
  GTK, WebKitGTK or fontconfig headers on this box, exit 1).
- Full suite, outside graftcp (the proxy this box wraps commands in),
  through systemd-run, umask 077, Bun 1.4.2 first on PATH as on CI,
  -timeout 45m, log kept: 66 ok, provider among them, and 6 red, none
  in what this changes:
  - agent TestInstalledVersion,
    TestCopilotFolderFromAnIDEPluginIsNotTheCLI,
    TestQoderSettingsFromAnIDEPluginIsNotTheCLI,
    TestOmpDetectedWithPiDir and TestZedAppPath: #3141;
    TestCodexDriftReached: #3398;
    TestReasonixCLIRequiresSupportedNativeVersion and
    TestReasonixCLIRecoversAfterProbeTimeout: #3641.
  - davsync TestSync: #3420.
  - edit TestWriteAtomicReadOnlyHardLink and testenv TestProgram: this
    box's umask 077, #3640.
  - gateway TestPluginToldWhereTheAgentWorks: this box's /tmp/.git,
    #3675.
  - gui TestGatewayHeldByAContainer and TestGatewayFixQuitsOnlyAMagpie:
    #3649.

Not tried: macOS and Windows, vet and build only here (nothing in the
change depends on the OS; CI's macOS job runs every package's tests, its
Windows job compiles them); an idle `magpie serve`'s wakeups as 091153bf
measured them (the loops take and are woken by the channel at the same
points), and Watch with a second process writing:
TestWatchWakesOnOthersWrites writes the files from the test.

Merged from JoeyWu-tech's fix/3632-filememo-synctest (0576b5893, ggbdpq's
pick on the issue), cherry-picked onto origin/main a1222c539. Run on mote
at this tree:
- go test -race -shuffle=on -count=10 ./internal/filememo/: ok.
- TestWrittenAcrossBubbles with a1222c539's filememo.go put back: fatal
  error: close of synctest channel from outside bubble. With the fix: ok.
- go test ./internal/provider/: no fatal; only #3854's three TestFetch*
  reds are left, the same as on main. internal/awake, middleware, otlp,
  pluginrt (the other Written users): ok. internal/agent -run CodexDaemon: ok.
- GOOS=linux and GOOS=windows go vet ./internal/filememo/ ./internal/agent/: ok.
skygunner's c9e34bbd has the same mechanism and wasn't taken.
This commit is contained in:
JoeyWu-tech
2026-10-28 06:32:43 +08:00
committed by yetone
parent a1222c539f
commit 4842f69be5
2 changed files with 59 additions and 7 deletions
+24 -7
View File
@@ -88,8 +88,7 @@ func Forget() {
mu.Lock()
gen++
gens.Store(gen)
close(written)
written = make(chan struct{})
endWaits()
mu.Unlock()
}
@@ -98,23 +97,41 @@ func Forget() {
// disk tells.
func wake() {
mu.Lock()
close(written)
written = make(chan struct{})
endWaits()
mu.Unlock()
}
// written is closed, and made anew, by every Forget.
var written = make(chan struct{})
// endWaits closes the channel Written gave since the last write, if it
// gave one, mu held. It makes none: a test that saves inside a synctest
// bubble writes through Forget too, a channel made there is the
// bubble's, and the next write from outside the bubble closing it is a
// fatal error, which took internal/provider's tests down (#3632).
func endWaits() {
if written != nil {
close(written)
written = nil
}
}
// written is what Written gave since the last write; nil until a loop
// waits again.
var written chan struct{}
// Written is closed by the next write magpie makes (Forget), or by
// another process's that Watch is told of: a loop that follows magpie's
// files waits on it, and looks at them only every so often (OthersEvery)
// for what another process wrote, rather than polling each second for its
// own writes too (#3185: those polls woke an idle magpie ~12 times a
// second).
// second). No loop may take it inside a synctest bubble: a channel made
// there is the bubble's and outlives it, and its next use from outside
// the bubble (a write's close, a loop's select) is fatal, in whichever
// test comes next.
func Written() <-chan struct{} {
mu.Lock()
defer mu.Unlock()
if written == nil {
written = make(chan struct{})
}
return written
}
+35
View File
@@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
"testing"
"testing/synctest"
"time"
)
@@ -173,3 +174,37 @@ func TestWritten(t *testing.T) {
default:
}
}
// A test that saves inside a synctest bubble writes through Forget too,
// and neither the channel it closes nor the next one may be the bubble's:
// the next write from outside the bubble, another bubble's or none's,
// closing it is a fatal error that took internal/provider's tests down
// (#3632). Another process's write (wake) is held to the same, each in
// bubbles of its own, so neither cleans up after the other. Loops
// waiting outside are still told of a write made in a bubble, two of them
// on the one wait they share.
func TestWrittenAcrossBubbles(t *testing.T) {
for _, write := range []struct {
name string
do func()
}{{"Forget", Forget}, {"wake", wake}} {
w, also := Written(), Written() // two loops waiting
for range 2 {
synctest.Test(t, func(t *testing.T) { write.do() })
}
for _, c := range []<-chan struct{}{w, also} {
select {
case <-c:
default:
t.Fatalf("%s in a bubble left a wait taken outside it open", write.name)
}
}
w = Written()
write.do()
select {
case <-w:
default:
t.Fatalf("not closed by the %s after the bubbles'", write.name)
}
}
}