fix: write tracemalloc dump to file instead of logger

Podman's log buffer truncates the output. Write full traceback dump
to data/derp.malloc with per-allocation stack traces.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
user
2026-02-17 13:16:22 +01:00
parent 404800af94
commit a2a607baa2

View File

@@ -78,8 +78,8 @@ def _shutdown(bot: Bot) -> None:
asyncio.get_running_loop().create_task(bot.conn.close())
def _dump_tracemalloc(log: logging.Logger, limit: int = 25) -> None:
"""Log top memory allocations from tracemalloc snapshot."""
def _dump_tracemalloc(log: logging.Logger, path: str, limit: int = 25) -> None:
"""Dump top memory allocations to a file and log summary."""
import tracemalloc
snapshot = tracemalloc.take_snapshot()
@@ -90,9 +90,16 @@ def _dump_tracemalloc(log: logging.Logger, limit: int = 25) -> None:
])
stats = snapshot.statistics("traceback")
total = sum(s.size for s in stats)
log.info("tracemalloc top %d (total tracked: %.1f KiB)", limit, total / 1024)
lines = [f"tracemalloc top {limit} (total tracked: {total / 1024:.1f} KiB)\n"]
for i, stat in enumerate(stats[:limit], 1):
log.info("#%d %.1f KiB %s", i, stat.size / 1024, stat.traceback.format()[0])
frames = stat.traceback.format()
lines.append(f"#{i} {stat.size / 1024:.1f} KiB ({stat.count} blocks)")
for frame in frames:
lines.append(f" {frame}")
lines.append("")
with open(path, "w") as f:
f.write("\n".join(lines))
log.info("tracemalloc saved to %s (%.1f KiB tracked)", path, total / 1024)
def main(argv: list[str] | None = None) -> int:
@@ -134,7 +141,7 @@ def main(argv: list[str] | None = None) -> int:
_run(bot)
if args.tracemalloc:
_dump_tracemalloc(log)
_dump_tracemalloc(log, "data/derp.malloc")
return 0