| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "log/slog" |
| 7 | |
| 8 | "reasonix/internal/agent" |
| 9 | ) |
| 10 | |
| 11 | // recoverShutdownSave answers a save lock that stayed held for the whole |
| 12 | // bounded wait during the shutdown snapshot. A schema-2 log takes the |
| 13 | // unsaved tail without the lock and the controller stays on its path; a |
| 14 | // schema-1 session still gets a distinct recovery copy. |
| 15 | func (c *Controller) recoverShutdownSave(s *agent.Session, path string, saveErr error, forceRewrite bool) (string, error) { |
| 16 | handled, err := s.AppendForShutdownWithoutLock(path, forceRewrite) |
| 17 | if err != nil { |
| 18 | return "", fmt.Errorf("append session tail at shutdown: %w", err) |
| 19 | } |
| 20 | if !handled { |
| 21 | return c.recoverShutdownSnapshot(path, saveErr) |
| 22 | } |
| 23 | appendSnapshotConflictDiagnostic(path, "shutdown", "appended_without_lock", saveErr, "", false) |
| 24 | slog.Warn("controller: shutdown snapshot lock timed out; appended the unsaved tail to the session log without it", "path", path) |
| 25 | return path, nil |
| 26 | } |
| 27 | |
| 28 | // listingDeferredAfterUnlockedAppend accepts a listing-sidecar refresh that |
| 29 | // failed on the very lock the shutdown append just worked around; the next |
| 30 | // locked save refreshes the sidecar along with the other derived files. |
| 31 | func listingDeferredAfterUnlockedAppend(s *agent.Session, path string, err error) bool { |
| 32 | return errors.Is(err, agent.ErrSessionFileLockHeld) && s.DerivedFilesPending(path) |
| 33 | } |
| 34 |