From e64aa70c34ba9744f00bbd52e852443a726aeaa8 Mon Sep 17 00:00:00 2001 From: Karl Seguin Date: Mon, 8 Jun 2026 15:24:27 +0800 Subject: [PATCH] Improve ArenaPool release debug reporting Cherry-picked from https://github.com/lightpanda-io/browser/pull/2623 since the main issue in that PR was directly solved by https://github.com/lightpanda-io/browser/pull/2625 but the ArenaPool change is still worth keeping. The change is: that we check for a double-release BEFORE resetting the arena, which would cause the invalid flow to appear in a harder-to-debug spot. --- src/ArenaPool.zig | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/ArenaPool.zig b/src/ArenaPool.zig index 29d2a0da..f6b5e85f 100644 --- a/src/ArenaPool.zig +++ b/src/ArenaPool.zig @@ -171,13 +171,9 @@ pub fn release(self: *ArenaPool, allocator: Allocator) void { const entry: *Entry = @fieldParentPtr("arena", arena); const bucket = entry.bucket; - // Reset the arena before acquiring the lock to minimize lock hold time - _ = arena.reset(.{ .retain_with_limit = bucket.retain_bytes }); - - self.mutex.lock(); - defer self.mutex.unlock(); - if (IS_DEBUG) { + self.mutex.lock(); + defer self.mutex.unlock(); if (self._leak_track.getPtr(entry.debug)) |count| { count.* -= 1; if (count.* < 0) { @@ -190,6 +186,11 @@ pub fn release(self: *ArenaPool, allocator: Allocator) void { } } + _ = arena.reset(.{ .retain_with_limit = bucket.retain_bytes }); + + self.mutex.lock(); + defer self.mutex.unlock(); + if ((comptime SAFETY) or bucket.free_list_len >= bucket.free_list_max) { // In Debug, we never pool. It can mask UAF bugs. arena.deinit();