@@ -4196,6 +4196,8 @@ pub const Context = struct {
41964196 /// The collector only enqueues registries; callbacks run later from the
41974197 /// normal interpreter checkpoint, outside the collector.
41984198 finalization_cleanup_jobs: std.ArrayListUnmanaged(*value.Object) = .empty,
4199+ /// Shared by nested/peer drains under realm_lock; consumed prefixes are not roots.
4200+ finalization_cleanup_head: usize = 0,
41994201 /// Monotonic proof bit for zig-gc's optional weak-processing gate. Runtime
42004202 /// constructors publish true before any WeakMap/WeakSet/WeakRef/
42014203 /// FinalizationRegistry state can become observable. It never returns to
@@ -5698,6 +5700,7 @@ pub const Context = struct {
56985700 .timer_keepalive = keepaliveTimers,
56995701 .timer_abandon = abandonTimers,
57005702 .finalization_cleanup_jobs = &self.finalization_cleanup_jobs,
5703+ .finalization_cleanup_head = &self.finalization_cleanup_head,
57015704 .stop_flag = self.stop_flag orelse &self.teardown_stop,
57025705 .watchdog_check_flag = self.watchdog_check_flag,
57035706 .watchdog_deadline_ns = self.watchdog_deadline_ns,
@@ -5999,7 +6002,7 @@ pub const Context = struct {
59996002 self.unhandled_rejections.isEmpty() and
60006003 self.handled_rejections.isEmpty() and
60016004 self.timers.items.len == 0 and
6002- self.finalization_cleanup_jobs.items.len == 0 and
6005+ self.finalization_cleanup_head == self. finalization_cleanup_jobs.items.len and
60036006 self.async_waiters.items.len == 0;
60046007 }
60056008
@@ -6255,6 +6258,7 @@ pub const Context = struct {
62556258 self.unhandled_rejections.clear();
62566259 self.handled_rejections.clear();
62576260 self.finalization_cleanup_jobs.clearRetainingCapacity();
6261+ self.finalization_cleanup_head = 0;
62586262 for (self.protected_values.items) |handle| self.gpa.destroy(handle);
62596263 self.protected_values.clearRetainingCapacity();
62606264 self.private_strong_roots.clearRetainingCapacity();
@@ -8891,7 +8895,7 @@ pub const Context = struct {
88918895 // (a no-op outside parallel_js).
88928896 self.realmLock();
88938897 defer self.realmUnlock();
8894- for (self.finalization_cleanup_jobs.items) |queued| {
8898+ for (self.finalization_cleanup_jobs.items[self.finalization_cleanup_head..] ) |queued| {
88958899 if (queued == registry) return;
88968900 }
88978901 self.reserveFinalizationCleanupJobsLocked(1) catch return;
@@ -8903,8 +8907,21 @@ pub const Context = struct {
89038907 }
89048908
89058909 fn reserveFinalizationCleanupJobsLocked(self: *Context, additional: usize) error{OutOfMemory}!void {
8906- const spare = self.finalization_cleanup_jobs.capacity - self.finalization_cleanup_jobs.items.len;
8910+ var spare = self.finalization_cleanup_jobs.capacity - self.finalization_cleanup_jobs.items.len;
89078911 if (spare >= additional) return;
8912+ // Reuse a consumed prefix before growing, amortizing compaction across
8913+ // at least as many retired entries. Compacting one slot on each append
8914+ // would make a long queue quadratic under repeated callback publication.
8915+ if (self.finalization_cleanup_head != 0 and
8916+ self.finalization_cleanup_head >= self.finalization_cleanup_jobs.items.len / 2)
8917+ {
8918+ const pending = self.finalization_cleanup_jobs.items[self.finalization_cleanup_head..];
8919+ std.mem.copyForwards(*value.Object, self.finalization_cleanup_jobs.items[0..pending.len], pending);
8920+ self.finalization_cleanup_jobs.items.len = pending.len;
8921+ self.finalization_cleanup_head = 0;
8922+ spare = self.finalization_cleanup_jobs.capacity - pending.len;
8923+ if (spare >= additional) return;
8924+ }
89088925 const extra = @max(additional, finalization_cleanup_queue_reserve_granularity);
89098926 try self.finalization_cleanup_jobs.ensureTotalCapacity(self.gpa, self.finalization_cleanup_jobs.items.len + extra);
89108927 }
@@ -43034,3 +43051,274 @@ test "Thread identity remains realm scoped across alternating hosts and Context
4303443051 try std.testing.expect(first.main_js_thread.?.prop_async_head == null);
4303543052 }
4303643053}
43054+
43055+ test "finalization cleanup reentrant drains retain newly queued registries" {
43056+ const Host = struct {
43057+ context: *Context,
43058+ calls: [4]u8 = @splat(0),
43059+ count: usize = 0,
43060+ fn cleanup(raw: *anyopaque, _: Value, args: []const Value) value.HostError!Value {
43061+ const machine: *interp.Interpreter = @ptrCast(@alignCast(raw));
43062+ const host: *@This() = @ptrCast(@alignCast(machine.active_native.?.private_data.?));
43063+ const id: u8 = @intFromFloat(args[0].asNum());
43064+ if (host.count >= host.calls.len) return error.OutOfMemory;
43065+ host.calls[host.count] = id;
43066+ host.count += 1;
43067+ if (id == 1) {
43068+ const current = host.context.global_object.getOwn("cleanupA").?.asObj();
43069+ const token = host.context.global_object.getOwn("cleanupToken").?.asObj();
43070+ if (!current.finRecordUnregister(@ptrCast(token))) return error.OutOfMemory;
43071+ try machine.drainFinalizationCleanupJobs();
43072+ // The nested drain exhausts A and B. Work published afterward
43073+ // must survive the suspended outer drain's queue cursor.
43074+ const third = host.context.global_object.getOwn("cleanupC").?.asObj();
43075+ try third.finRecordAppend(machine.arena, .{ .held = Value.num(3), .ready = true });
43076+ host.context.queueFinalizationRegistryCleanup(third);
43077+ const first = host.context.global_object.getOwn("cleanupA").?.asObj();
43078+ try first.finRecordAppend(machine.arena, .{ .held = Value.num(4), .ready = true });
43079+ host.context.queueFinalizationRegistryCleanup(first);
43080+ }
43081+ return Value.undef();
43082+ }
43083+ };
43084+ for ([_]bool{ false, true }) |gc_enabled| {
43085+ const ctx = try Context.createWith(std.testing.allocator, .{ .enable_gc = gc_enabled, .enable_jit = false });
43086+ defer ctx.destroy();
43087+ var host = Host{ .context = ctx };
43088+ const saved = gc_mod.setActiveContext(ctx);
43089+ defer gc_mod.restoreActiveContext(saved);
43090+ try interp.setNative(ctx.arena(), ctx.root_shape, ctx.global_object, "cleanupReenter", 1, Host.cleanup);
43091+ ctx.global_object.getOwn("cleanupReenter").?.asObj().private_data = &host;
43092+ _ = try ctx.evaluate(
43093+ \\var cleanupToken = {};
43094+ \\var cleanupA = new FinalizationRegistry(cleanupReenter);
43095+ \\var cleanupB = new FinalizationRegistry(cleanupReenter);
43096+ \\var cleanupC = new FinalizationRegistry(cleanupReenter);
43097+ );
43098+ for ([_][]const u8{ "cleanupA", "cleanupB" }, 1..) |name, id| {
43099+ const registry = ctx.global_object.getOwn(name).?.asObj();
43100+ try registry.finRecordAppend(ctx.arena(), .{ .held = Value.num(@floatFromInt(id)), .ready = true });
43101+ ctx.queueFinalizationRegistryCleanup(registry);
43102+ }
43103+ const registry = ctx.global_object.getOwn("cleanupA").?.asObj();
43104+ try registry.finRecordAppend(ctx.arena(), .{
43105+ .held = Value.num(99),
43106+ .ready = true,
43107+ .token = @ptrCast(ctx.global_object.getOwn("cleanupToken").?.asObj()),
43108+ });
43109+ _ = try ctx.evaluate("0;");
43110+ try std.testing.expectEqual(@as(usize, 4), host.count);
43111+ try std.testing.expectEqualSlices(u8, &.{ 1, 2, 4, 3 }, &host.calls);
43112+ try std.testing.expectEqual(@as(usize, 0), ctx.finalization_cleanup_jobs.items.len);
43113+ }
43114+ }
43115+
43116+ test "finalization cleanup reloads registry callback and held value after moving GC" {
43117+ const Host = struct {
43118+ context: *Context,
43119+ calls: usize = 0,
43120+ moved: bool = false,
43121+ nested: bool,
43122+ fn move(host: *@This(), machine: *interp.Interpreter) void {
43123+ const before_registry = host.context.global_object.getOwn("movingCleanupRegistry").?.asObj();
43124+ const before_callback = machine.active_native.?;
43125+ const before_held = host.context.global_object.getOwn("movingCleanupHeld").?.asObj();
43126+ host.context.gc_scan_native_stack = false;
43127+ const result = host.context.collectYoungAfterRootValidation(host.context.gc.?);
43128+ host.moved = result.status == .compacted and before_registry != host.context.global_object.getOwn("movingCleanupRegistry").?.asObj() and before_callback != machine.active_native.? and before_held != host.context.global_object.getOwn("movingCleanupHeld").?.asObj();
43129+ }
43130+ fn cleanup(raw: *anyopaque, _: Value, args: []const Value) value.HostError!Value {
43131+ const machine: *interp.Interpreter = @ptrCast(@alignCast(raw));
43132+ const host: *@This() = @ptrCast(@alignCast(machine.active_native.?.private_data.?));
43133+ host.calls += 1;
43134+ if (host.calls == 1) {
43135+ if (host.nested) try machine.drainFinalizationCleanupJobs() else host.move(machine);
43136+ // In the nested case the queue has already been cleared. The
43137+ // suspended outer cleanup still owns its rewritten argument.
43138+ const expected = host.context.global_object.getOwn("movingCleanupHeld").?.asObj();
43139+ if (args[0].asObj() != expected) return error.OutOfMemory;
43140+ } else {
43141+ if (args[0].asNum() != 2) return error.OutOfMemory;
43142+ if (host.nested) host.move(machine);
43143+ }
43144+ return Value.undef();
43145+ }
43146+ };
43147+ for ([_]bool{ false, true }) |nested| {
43148+ const ctx = try Context.createWithTestingOptions(std.testing.allocator, .{
43149+ .enable_gc = true,
43150+ .enable_jit = false,
43151+ .heap_limit_bytes = 16 * 1024 * 1024,
43152+ });
43153+ defer ctx.destroy();
43154+ var host = Host{ .context = ctx, .nested = nested };
43155+ const saved = gc_mod.setActiveContext(ctx);
43156+ defer gc_mod.restoreActiveContext(saved);
43157+ try interp.setNative(ctx.arena(), ctx.root_shape, ctx.global_object, "movingCleanup", 1, Host.cleanup);
43158+ ctx.global_object.getOwn("movingCleanup").?.asObj().private_data = &host;
43159+ _ = try ctx.evaluate(
43160+ \\var movingCleanupRegistry = new FinalizationRegistry(movingCleanup);
43161+ \\var movingCleanupHeld = { alive: 906 };
43162+ );
43163+ const registry = ctx.global_object.getOwn("movingCleanupRegistry").?.asObj();
43164+ try registry.finRecordAppend(ctx.arena(), .{ .held = ctx.global_object.getOwn("movingCleanupHeld").?, .ready = true });
43165+ try registry.finRecordAppend(ctx.arena(), .{ .held = Value.num(2), .ready = true });
43166+ ctx.queueFinalizationRegistryCleanup(registry);
43167+ _ = try ctx.evaluate("0;");
43168+ try std.testing.expect(host.moved);
43169+ try std.testing.expectEqual(@as(usize, 2), host.calls);
43170+ try std.testing.expectEqual(@as(usize, 0), ctx.finalization_cleanup_jobs.items.len);
43171+ }
43172+ }
43173+
43174+ test "finalization cleanup requeues processed registries with bounded queue storage" {
43175+ const Host = struct {
43176+ context: *Context,
43177+ calls: usize = 0,
43178+ capacity: usize = 0,
43179+ fn cleanup(raw: *anyopaque, _: Value, _: []const Value) value.HostError!Value {
43180+ const machine: *interp.Interpreter = @ptrCast(@alignCast(raw));
43181+ const host: *@This() = @ptrCast(@alignCast(machine.active_native.?.private_data.?));
43182+ host.calls += 1;
43183+ if (host.calls < 64) {
43184+ const name: []const u8 = if (host.calls % 2 == 1) "churnCleanupB" else "churnCleanupA";
43185+ const registry = host.context.global_object.getOwn(name).?.asObj();
43186+ try registry.finRecordAppend(machine.arena, .{ .held = Value.undef(), .ready = true });
43187+ host.context.queueFinalizationRegistryCleanup(registry);
43188+ host.capacity = @max(host.capacity, host.context.finalization_cleanup_jobs.capacity);
43189+ }
43190+ return Value.undef();
43191+ }
43192+ };
43193+ const ctx = try Context.create(std.testing.allocator);
43194+ defer ctx.destroy();
43195+ var host = Host{ .context = ctx };
43196+ const saved = gc_mod.setActiveContext(ctx);
43197+ defer gc_mod.restoreActiveContext(saved);
43198+ try interp.setNative(ctx.arena(), ctx.root_shape, ctx.global_object, "churnCleanup", 1, Host.cleanup);
43199+ ctx.global_object.getOwn("churnCleanup").?.asObj().private_data = &host;
43200+ _ = try ctx.evaluate("var churnCleanupA = new FinalizationRegistry(churnCleanup), churnCleanupB = new FinalizationRegistry(churnCleanup);");
43201+ const first = ctx.global_object.getOwn("churnCleanupA").?.asObj();
43202+ try first.finRecordAppend(ctx.arena(), .{ .held = Value.undef(), .ready = true });
43203+ ctx.queueFinalizationRegistryCleanup(first);
43204+ const initial_capacity = ctx.finalization_cleanup_jobs.capacity;
43205+ _ = try ctx.evaluate("0;");
43206+ try std.testing.expectEqual(@as(usize, 64), host.calls);
43207+ try std.testing.expectEqual(initial_capacity, host.capacity);
43208+ try std.testing.expectEqual(@as(usize, 0), ctx.finalization_cleanup_jobs.items.len);
43209+ }
43210+
43211+ test "finalization cleanup never replays a cell after an abrupt Proxy apply getter" {
43212+ const Host = struct {
43213+ calls: usize = 0,
43214+ unavailable: std.testing.FailingAllocator,
43215+ fn failOnce(raw: *anyopaque, _: Value, _: []const Value) value.HostError!Value {
43216+ const machine: *interp.Interpreter = @ptrCast(@alignCast(raw));
43217+ const host: *@This() = @ptrCast(@alignCast(machine.active_native.?.private_data.?));
43218+ host.calls += 1;
43219+ if (host.calls == 1) {
43220+ const memory = try host.unavailable.allocator().alloc(u8, 1);
43221+ host.unavailable.allocator().free(memory);
43222+ }
43223+ return Value.undef();
43224+ }
43225+ };
43226+ for ([_]interp.BytecodeExecutionMode{ .tree_walker, .required }) |mode| {
43227+ const ctx = try Context.createWithTestingOptions(std.testing.allocator, .{ .enable_jit = false, .bytecode_execution_mode = mode });
43228+ defer ctx.destroy();
43229+ var host = Host{ .unavailable = .init(ctx.arena(), .{ .fail_index = 0, .resize_fail_index = 0 }) };
43230+ const saved = gc_mod.setActiveContext(ctx);
43231+ defer gc_mod.restoreActiveContext(saved);
43232+ try interp.setNative(ctx.arena(), ctx.root_shape, ctx.global_object, "failCleanupAdmissionOnce", 0, Host.failOnce);
43233+ ctx.global_object.getOwn("failCleanupAdmissionOnce").?.asObj().private_data = &host;
43234+ _ = try ctx.evaluate(
43235+ \\var cleanupGetterCalls = 0, cleanupDelivered = 0;
43236+ \\var abruptCleanupRegistry = new FinalizationRegistry(new Proxy(function(value) { cleanupDelivered += value; }, {
43237+ \\ get apply() { cleanupGetterCalls++; failCleanupAdmissionOnce(); return undefined; }
43238+ \\}));
43239+ );
43240+ const registry = ctx.global_object.getOwn("abruptCleanupRegistry").?.asObj();
43241+ try registry.finRecordAppend(ctx.arena(), .{ .held = Value.num(1), .ready = true });
43242+ try registry.finRecordAppend(ctx.arena(), .{ .held = Value.num(2), .ready = true });
43243+ ctx.queueFinalizationRegistryCleanup(registry);
43244+ try std.testing.expectError(error.OutOfMemory, ctx.evaluate("0;"));
43245+ try std.testing.expect(host.unavailable.has_induced_failure);
43246+ try std.testing.expectEqual(@as(f64, 1), ctx.global_object.getOwn("cleanupGetterCalls").?.asNum());
43247+ _ = try ctx.evaluate("0;");
43248+ try std.testing.expectEqual(@as(f64, 2), ctx.global_object.getOwn("cleanupGetterCalls").?.asNum());
43249+ try std.testing.expectEqual(@as(f64, 2), ctx.global_object.getOwn("cleanupDelivered").?.asNum());
43250+ try std.testing.expectEqual(@as(usize, 0), ctx.finalization_cleanup_jobs.items.len);
43251+ }
43252+ }
43253+
43254+ test "finalization cleanup races ready publication without losing or duplicating records" {
43255+ if (builtin.single_threaded) return error.SkipZigTest;
43256+ const ctx = try Context.createWithTestingOptions(std.testing.allocator, .{
43257+ .enable_gc = true,
43258+ .parallel_gc = true,
43259+ .enable_threads = true,
43260+ .parallel_js = true,
43261+ .enable_jit = false,
43262+ });
43263+ defer ctx.destroy();
43264+ const saved = gc_mod.setActiveContext(ctx);
43265+ defer gc_mod.restoreActiveContext(saved);
43266+ const Host = struct {
43267+ context: *Context,
43268+ registry: *value.Object,
43269+ allocator: std.mem.Allocator,
43270+ finished: std.atomic.Value(bool) = .init(false),
43271+ failed: std.atomic.Value(bool) = .init(false),
43272+ calls: usize = 0,
43273+ ordered: bool = true,
43274+ fn publish(host: *@This()) void {
43275+ const previous = gc_mod.setActiveContext(host.context);
43276+ defer gc_mod.restoreActiveContext(previous);
43277+ defer host.finished.store(true, .release);
43278+ for (1..129) |id| {
43279+ host.registry.finRecordAppend(host.allocator, .{ .held = Value.num(@floatFromInt(id)), .ready = true }) catch {
43280+ host.failed.store(true, .release);
43281+ return;
43282+ };
43283+ host.context.queueFinalizationRegistryCleanup(host.registry);
43284+ std.Thread.yield() catch {};
43285+ }
43286+ }
43287+ fn cleanup(raw: *anyopaque, _: Value, args: []const Value) value.HostError!Value {
43288+ const machine: *interp.Interpreter = @ptrCast(@alignCast(raw));
43289+ const host: *@This() = @ptrCast(@alignCast(machine.active_native.?.private_data.?));
43290+ host.calls += 1;
43291+ host.ordered = host.ordered and args[0].asNum() == @as(f64, @floatFromInt(host.calls));
43292+ std.Thread.yield() catch {};
43293+ return Value.undef();
43294+ }
43295+ };
43296+ try interp.setNative(ctx.arena(), ctx.root_shape, ctx.global_object, "parallelCleanup", 1, Host.cleanup);
43297+ const registry = (try ctx.evaluate("var parallelCleanupRegistry = new FinalizationRegistry(parallelCleanup); parallelCleanupRegistry;")).asObj();
43298+ var host = Host{ .context = ctx, .registry = registry, .allocator = ctx.arena() };
43299+ ctx.global_object.getOwn("parallelCleanup").?.asObj().private_data = &host;
43300+ // Preallocate backing so the witness isolates the publication/retirement
43301+ // race, independent of concurrent allocator recovery.
43302+ for (0..128) |_| try registry.finRecordAppend(ctx.arena(), .{ .held = Value.undef(), .ready = true });
43303+ while (registry.finRecordTakeReady() != null) {}
43304+ ctx.queueFinalizationRegistryCleanup(registry);
43305+ var machine = ctx.interpreter();
43306+ var unavailable = std.testing.FailingAllocator.init(machine.arena, .{ .fail_index = 0, .resize_fail_index = 0 });
43307+ machine.arena = unavailable.allocator();
43308+ try machine.drainFinalizationCleanupJobs();
43309+ const thread = try std.Thread.spawn(.{}, Host.publish, .{&host});
43310+ var joined = false;
43311+ defer if (!joined) thread.join();
43312+ while (!host.finished.load(.acquire)) {
43313+ try machine.drainFinalizationCleanupJobs();
43314+ std.Thread.yield() catch {};
43315+ }
43316+ thread.join();
43317+ joined = true;
43318+ try machine.drainFinalizationCleanupJobs();
43319+ try std.testing.expect(!host.failed.load(.acquire));
43320+ try std.testing.expect(!unavailable.has_induced_failure);
43321+ try std.testing.expect(host.ordered);
43322+ try std.testing.expectEqual(@as(usize, 128), host.calls);
43323+ try std.testing.expectEqual(@as(usize, 0), ctx.finalization_cleanup_jobs.items.len);
43324+ }
0 commit comments