WIP: Fix ref counting bugs in spawn API observed under rr (#20191)

Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
Co-authored-by: Jarred-Sumner <709451+Jarred-Sumner@users.noreply.github.com>
This commit is contained in:
190n
2025-06-14 07:59:29 -07:00
committed by GitHub
parent c44515eaaf
commit 28006d0ad4
2 changed files with 23 additions and 5 deletions

View File

@@ -41,6 +41,7 @@ pub fn kill(this: *ProcessAutoKiller) Result {
fn killProcesses(this: *ProcessAutoKiller) u32 {
var count: u32 = 0;
while (this.processes.pop()) |process| {
defer process.key.deref();
if (!process.key.hasExited()) {
log("process.kill {d}", .{process.key.pid});
count += @as(u32, @intFromBool(process.key.kill(@intFromEnum(bun.SignalCode.default)) == .result));
@@ -50,6 +51,10 @@ fn killProcesses(this: *ProcessAutoKiller) u32 {
}
pub fn clear(this: *ProcessAutoKiller) void {
for (this.processes.keys()) |process| {
process.deref();
}
if (this.processes.capacity() > 256) {
this.processes.clearAndFree(bun.default_allocator);
}
@@ -58,15 +63,23 @@ pub fn clear(this: *ProcessAutoKiller) void {
}
pub fn onSubprocessSpawn(this: *ProcessAutoKiller, process: *bun.spawn.Process) void {
if (this.enabled)
this.processes.put(bun.default_allocator, process, {}) catch {};
if (this.enabled) {
this.processes.put(bun.default_allocator, process, {}) catch return;
process.ref();
}
}
pub fn onSubprocessExit(this: *ProcessAutoKiller, process: *bun.spawn.Process) void {
if (this.ever_enabled)
_ = this.processes.swapRemove(process);
if (this.ever_enabled) {
if (this.processes.swapRemove(process)) {
process.deref();
}
}
}
pub fn deinit(this: *ProcessAutoKiller) void {
for (this.processes.keys()) |process| {
process.deref();
}
this.processes.deinit(bun.default_allocator);
}

View File

@@ -374,11 +374,16 @@ pub const Process = struct {
}
if (this.poller == .fd) {
return this.poller.fd.register(
const maybe = this.poller.fd.register(
this.event_loop.loop(),
.process,
true,
);
switch (maybe) {
.err => {},
.result => this.ref(),
}
return maybe;
} else {
@panic("Internal Bun error: poll_ref in Subprocess is null unexpectedly. Please file a bug report.");
}