fix(memory): fsync appended message for consistent durability
addMsg now calls f.Sync() before f.Close(), matching the durability guarantee of writeMeta and rewriteJSONL (both use WriteFileAtomic with fsync). Without this, a power loss could leave the appended line in the kernel page cache only — lost on reboot.
This commit is contained in:
parent
6d894d6138
commit
f9f726c0c1
1 changed files with 10 additions and 2 deletions
|
|
@ -236,11 +236,19 @@ func (s *JSONLStore) addMsg(sessionKey string, msg providers.Message) error {
|
||||||
return fmt.Errorf("memory: open jsonl for append: %w", err)
|
return fmt.Errorf("memory: open jsonl for append: %w", err)
|
||||||
}
|
}
|
||||||
_, writeErr := f.Write(line)
|
_, writeErr := f.Write(line)
|
||||||
closeErr := f.Close()
|
|
||||||
if writeErr != nil {
|
if writeErr != nil {
|
||||||
|
f.Close()
|
||||||
return fmt.Errorf("memory: append message: %w", writeErr)
|
return fmt.Errorf("memory: append message: %w", writeErr)
|
||||||
}
|
}
|
||||||
if closeErr != nil {
|
// Flush to physical storage before closing. This matches the
|
||||||
|
// durability guarantee of writeMeta and rewriteJSONL (which use
|
||||||
|
// WriteFileAtomic with fsync). Without Sync, a power loss could
|
||||||
|
// leave the append in the kernel page cache only — lost on reboot.
|
||||||
|
if syncErr := f.Sync(); syncErr != nil {
|
||||||
|
f.Close()
|
||||||
|
return fmt.Errorf("memory: sync jsonl: %w", syncErr)
|
||||||
|
}
|
||||||
|
if closeErr := f.Close(); closeErr != nil {
|
||||||
return fmt.Errorf("memory: close jsonl: %w", closeErr)
|
return fmt.Errorf("memory: close jsonl: %w", closeErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue