Archived
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:
1 parent
8cfcd3c6e5
commit
87becd5812
1 file changed
+24
-9
+24
-9
@@ -62,14 +62,26 @@ def main():
|
|||||||
out.close() # close() flushes; on a full disk the dirty buffer fails to land
|
out.close() # close() flushes; on a full disk the dirty buffer fails to land
|
||||||
except OSError:
|
except OSError:
|
||||||
pass # those rows are already counted lost -- the fresh handle is the point
|
pass # those rows are already counted lost -- the fresh handle is the point
|
||||||
for i in range(4, 0, -1):
|
try:
|
||||||
src = "%s.%d" % (a.out, i)
|
for i in range(4, 0, -1):
|
||||||
if os.path.exists(src):
|
src = "%s.%d" % (a.out, i)
|
||||||
os.replace(src, "%s.%d" % (a.out, i + 1))
|
if os.path.exists(src):
|
||||||
os.replace(a.out, a.out + ".1")
|
os.replace(src, "%s.%d" % (a.out, i + 1))
|
||||||
out = open(a.out, "a")
|
os.replace(a.out, a.out + ".1")
|
||||||
rotated += 1
|
out = open(a.out, "a")
|
||||||
sys.stderr.write("statsdump: rotated %s (generation %d)\n" % (a.out, rotated))
|
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))
|
sys.stderr.write("statsdump: %s/stats every %.1fs -> %s\n" % (base, a.every, a.out))
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
@@ -131,7 +143,10 @@ def main():
|
|||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
sys.stderr.write("statsdump: interrupted\n")
|
sys.stderr.write("statsdump: interrupted\n")
|
||||||
finally:
|
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"
|
sys.stderr.write("statsdump: %d rows written, %d polls lost, %d rotations, %s\n"
|
||||||
% (ok, lost, rotated, a.out))
|
% (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.
|
# An empty file is a failure, not a quiet run: say so with the exit status, not only in prose.
|
||||||
|
|||||||
Reference in new issue
Block a user