perf: keep file I/O off Service.mu and untie StartOnly from the app context
Phase 7 of the whole-project review (findings 3.2 and 3.3). Service.mu is the lock the Fyne main thread takes on every Jobs() and Runtime() call, so anything blocking inside it makes a UI refresh wait on the disk. Three things did: - Every SaveJobs/SaveConfig was a marshal, fsync, and rename under mu. Writes are now prepared under the lock (Store.PrepareSaveJobs / PrepareSaveConfig snapshot the payload and target path) and run after it is released. deferSaveLocked takes saveMu while mu is still held, so writes still reach the file in the order their snapshots were taken and an older snapshot can never land on top of a newer one. - executeRun ran runner.CleanupLogs under mu after every run. It needs only the values already snapshotted into runEnv, so it now runs after the unlock — including when the job is gone, since the run still wrote a log file that retention covers. - adoptJobsLocked ran runner.SeedStats under mu, reached from UpdateSettings on the UI thread. Seeding moved out into applySeededStatsLocked; UpdateSettings now reads the new jobs file and seeds its statistics before taking the lock, and re-checks the "no jobs-file switch while running" guard once it has it. SeedStats also opened every log file twice — once to find the job, again to read the result. readLogSummary reads job_id, state, and duration in one pass, so each log is opened once. StartOnly runs were built with exec.CommandContext on the app's lifecycle context. os/exec keeps a watcher goroutine alive until Wait returns or the context is done, and StartOnly never calls Wait, so one goroutine leaked per run and would then try to kill a process whose handle startJobOnly had already released. The invocation now uses context.Background(), whose nil Done channel means no watcher is started at all. Regression tests: TestRunJobStartOnlyLeavesNoContextWatcher (fails with 5 leaked goroutines on the old code), TestConcurrentJobOperationsLeaveTheFileMatchingMemory, and TestUpdateSettingsSeedsAdoptedJobsFromLogs. STANDARDS gains the no-I/O-under-mu rule and the "a StartOnly process outlives GoSentry" entry. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
+36
-8
@@ -65,19 +65,47 @@ func OpenStore() (*Store, []domain.Job, error) {
|
||||
return store, jobs, nil
|
||||
}
|
||||
|
||||
func (s *Store) SaveConfig() error {
|
||||
// PrepareSaveConfig re-resolves the derived paths from the current config and
|
||||
// snapshots everything the write needs, returning the write itself as a closure.
|
||||
// It exists so a caller that guards the Store with its own lock can do the file
|
||||
// I/O — a marshal, an fsync, and a rename — after releasing that lock: the
|
||||
// snapshot cannot change under the closure, so running it unlocked is safe.
|
||||
// Prepared writes must be run in the order they were prepared, or an older
|
||||
// snapshot can land on top of a newer one.
|
||||
func (s *Store) PrepareSaveConfig() func() error {
|
||||
s.applyConfigPaths()
|
||||
if err := os.MkdirAll(s.Paths.AppDir, 0o755); err != nil {
|
||||
return err
|
||||
dir := s.Paths.AppDir
|
||||
path := s.Paths.ConfigPath
|
||||
config := s.Config
|
||||
return func() error {
|
||||
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
return writeJSON(path, config)
|
||||
}
|
||||
return writeJSON(s.Paths.ConfigPath, s.Config)
|
||||
}
|
||||
|
||||
// PrepareSaveJobs is PrepareSaveConfig for the jobs file. The jobs slice is
|
||||
// copied, so the caller may keep mutating its own slice as soon as this returns.
|
||||
func (s *Store) PrepareSaveJobs(jobs []domain.Job) func() error {
|
||||
dir := s.Paths.JobsDir
|
||||
path := s.Paths.JobsPath
|
||||
snapshot := make([]domain.Job, len(jobs))
|
||||
copy(snapshot, jobs)
|
||||
return func() error {
|
||||
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
return writeJSON(path, domain.JobsFile{Jobs: snapshot})
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Store) SaveConfig() error {
|
||||
return s.PrepareSaveConfig()()
|
||||
}
|
||||
|
||||
func (s *Store) SaveJobs(jobs []domain.Job) error {
|
||||
if err := os.MkdirAll(s.Paths.JobsDir, 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
return writeJSON(s.Paths.JobsPath, domain.JobsFile{Jobs: jobs})
|
||||
return s.PrepareSaveJobs(jobs)()
|
||||
}
|
||||
|
||||
func loadOrCreateConfig(paths Paths) (domain.Config, error) {
|
||||
|
||||
Reference in New Issue
Block a user