fix: initialize log_written from file size to prevent 2x cap drift#16
fix: initialize log_written from file size to prevent 2x cap drift#16ricardovfreixo wants to merge 1 commit intomobydeck:mainfrom
Conversation
log_written tracked bytes since last rotation, not actual file size. After open_log(), the file could already hold log_max_size bytes but log_written started at 0, so another log_max_size was written before rotation triggered. Initialize log_written from the fd position after rotate_log() so the counter reflects reality.
010e671 to
a6f6c7f
Compare
|
thanks for submitting your PR! 👍 |
|
We can have both. If you don't set any cap, it assumes the 2x log size, but if you cap it, it respects the cap. I think using both we can cover all the possible cases |
Summary
The on-disk session log can grow to twice the configured
-Ccap before rotation kicks in.Root cause
log_writtentracks bytes written since the last rotation, not the actual file size. Afteropen_log(), the file can already contain up tolog_max_sizebytes (trimmed byrotate_log()), butlog_writtenis initialized to 0. Rotation only triggers whenlog_written >= log_max_size, so another fulllog_max_sizeof data is appended before the check fires.Fix
Initialize
log_writtenfrom the fd position afterrotate_log()inopen_log(), so the counter reflects the actual file size from the start:Single line change, no new syscall overhead (piggybacks on the position
rotate_log()already established via its finallseek(log_fd, 0, SEEK_END)).Test plan
atch new test -C 64k -- sh -c 'cat /dev/urandom | base64'ls -la ~/.cache/atch/test.log— should be at most ~64KB (plus one write batch), not ~128KBatch testshows recent output, not truncated mid-sequencemake clean && make— no new warnings