diff --git a/internal/filememo/filememo.go b/internal/filememo/filememo.go index 94531e462..c3aeb5ee8 100644 --- a/internal/filememo/filememo.go +++ b/internal/filememo/filememo.go @@ -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 } diff --git a/internal/filememo/filememo_test.go b/internal/filememo/filememo_test.go index 021eb3ab5..aa1bc89d7 100644 --- a/internal/filememo/filememo_test.go +++ b/internal/filememo/filememo_test.go @@ -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) + } + } +}