From a2a607baa29da298d284a9fc323398337d7c66bd Mon Sep 17 00:00:00 2001 From: user Date: Tue, 17 Feb 2026 13:16:22 +0100 Subject: [PATCH] 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 --- src/derp/cli.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/derp/cli.py b/src/derp/cli.py index 77cd753..4b2dbd6 100644 --- a/src/derp/cli.py +++ b/src/derp/cli.py @@ -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