scripts/statsdump: rotation failure must not leave a closed handle (auditor ringer Q2)

The disk-full fix in c9aa677 guarded the WRITE path but not the ROTATION path:
rotate() closes the handle first, so if the generation shift or the reopen
itself raises (ENOSPC bites when the directory must grow), out stays closed --
and the next poll's write raises ValueError, which the OSError handler does
not catch. The poller dies with a traceback and no exit summary: the exact
terminal state the size cap exists to prevent.

Fix: rotate() reopens best-effort before re-raising (append-open needs no
disk space, so it succeeds even on a full disk), and the exit close is
guarded so a failing flush cannot eat the summary line.

Can-fail verified by injecting ENOSPC into os.replace at the second shift --
the auditor's geometry, rotation failing with the handle already closed:
pre-fix dies 'ValueError: I/O operation on closed file' with 0 summary lines;
post-fix completes the run, rc=0, summary printed.

Authored at: Memoria Qwen 3.8 Flash-Next 125B MoE (level not visible) via Open WebUI
This commit is contained in:
Memoria (Noblistic) authored and StillDeadcode committed 2026-10-21 09:27:48 +02:00
1 parent 8cfcd3c6e5
commit 87becd5812
1 file changed
+24 -9
+24 -9
View File
@@ -62,14 +62,26 @@ def main():
out.close() # close() flushes; on a full disk the dirty buffer fails to land
except OSError:
pass # those rows are already counted lost -- the fresh handle is the point
for i in range(4, 0, -1):
src = "%s.%d" % (a.out, i)
if os.path.exists(src):
os.replace(src, "%s.%d" % (a.out, i + 1))
os.replace(a.out, a.out + ".1")
out = open(a.out, "a")
rotated += 1
sys.stderr.write("statsdump: rotated %s (generation %d)\n" % (a.out, rotated))
try:
for i in range(4, 0, -1):
src = "%s.%d" % (a.out, i)
if os.path.exists(src):
os.replace(src, "%s.%d" % (a.out, i + 1))
os.replace(a.out, a.out + ".1")
out = open(a.out, "a")
rotated += 1
sys.stderr.write("statsdump: rotated %s (generation %d)\n" % (a.out, rotated))
except OSError:
# The shift or the reopen failed (ENOSPC can bite when the directory must
# grow). out is CLOSED here -- and a closed handle raises ValueError on the
# next write, which the OSError handler does not catch (auditor ringer Q2).
# Reopen best-effort before re-raising: append-open needs no disk space, so
# this succeeds even on a full disk, and the poller keeps counting losses.
try:
out = open(a.out, "a")
except OSError:
pass
raise
sys.stderr.write("statsdump: %s/stats every %.1fs -> %s\n" % (base, a.every, a.out))
try:
while True:
@@ -131,7 +143,10 @@ def main():
except KeyboardInterrupt:
sys.stderr.write("statsdump: interrupted\n")
finally:
out.close()
try:
out.close()
except OSError:
pass # a flush failing at exit must not eat the summary line below
sys.stderr.write("statsdump: %d rows written, %d polls lost, %d rotations, %s\n"
% (ok, lost, rotated, a.out))
# An empty file is a failure, not a quiet run: say so with the exit status, not only in prose.