fix(diary): retire replaced daily writers and serialise ApplyGlobals

- install and Reset retire the channels they replace: a retired daily
  writer appends a late record and closes the file again, so no record is
  lost and no descriptor stays open; diary.Close keeps reopening the
  installed writers on the next record
- ApplyGlobals holds the setup lock for its whole run (lock-free
  installLocked and setVerbosityLocked), so concurrent calls rebuild the
  channels once (WR-06)
This commit is contained in:
Jakub Zych
2026-10-22 02:27:39 +02:00
parent c586c6f8a9
commit 39677f1819
3 changed files with 179 additions and 8 deletions

View File

@@ -40,6 +40,10 @@ type dailyWriter struct {
retryAt time.Time
// warnedDate is the last date a failure was reported for.
warnedDate string
// retired is set when a new setup replaced the writer's channels: a
// late record is still appended, but the file is closed again after
// every write, so the writer keeps no descriptor.
retired bool
}
func newDailyWriter(path string, days int, now func() time.Time, warn func(string, error)) *dailyWriter {
@@ -88,6 +92,10 @@ func (d *dailyWriter) Write(p []byte) (int, error) {
d.failedDate = ""
}
n, err := d.file.Write(p)
if d.retired {
_ = d.file.Close()
d.file, d.date = nil, ""
}
d.mu.Unlock()
return n, err
}
@@ -155,6 +163,16 @@ func (d *dailyWriter) prune(today string) {
}
}
// retire closes the open file for good: later writes, from goroutines that
// still hold the replaced channels, open the day's file, append and close
// it again.
func (d *dailyWriter) retire() error {
d.mu.Lock()
d.retired = true
d.mu.Unlock()
return d.Close()
}
// Close closes the open file. A later write opens it again.
func (d *dailyWriter) Close() error {
d.mu.Lock()