Fix memory leaks & blocking syscall in Bun Shell (#23636)

## Summary

Fixes two critical bugs in Bun Shell:

1. **Memory leaks & incorrect GC reporting**: Shell objects weren't
reporting their memory usage to JavaScriptCore's garbage collector,
causing memory to accumulate unchecked. Also fixes a leak where
`ShellArgs` wasn't being freed in `Interpreter.finalize()`.

2. **Blocking I/O on macOS**: Fixes a bug where writing large amounts of
data (>1MB) to pipes would block the main thread on macOS. The issue:
`sendto()` with `MSG_NOWAIT` flag blocks on macOS despite the flag, so
we now avoid the socket fast path unless the socket is already
non-blocking.

## Changes

- Adds `memoryCost()` and `estimatedSize()` implementations across shell
AST nodes, interpreter, and I/O structures
- Reports estimated memory size to JavaScriptCore GC via
`vm.heap.reportExtraMemoryAllocated()`
- Fixes missing `this.args.deinit()` call in interpreter finalization
- Fixes `BabyList.memoryCost()` to return bytes, not element count
- Conditionally uses socket fast path in IOWriter based on platform and
socket state

## Test plan

- [x] New test: `shell-leak-args.test.ts` - validates memory doesn't
leak during parsing/execution
- [x] New test: `shell-blocking-pipe.test.ts` - validates large pipe
writes don't block the main thread
- [x] Existing shell tests pass

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Claude Bot <claude-bot@bun.sh>
This commit is contained in:
Jarred Sumner
2025-10-19 22:17:19 -07:00
committed by GitHub
parent e63a897c66
commit 767c61d355
17 changed files with 471 additions and 16 deletions

View File

@@ -0,0 +1,30 @@
import { define } from "../../codegen/class-definitions";
export default [
define({
name: "ParsedShellScript",
construct: true,
noConstructor: true,
finalize: true,
hasPendingActivity: false,
configurable: false,
valuesArray: true,
memoryCost: true,
estimatedSize: true,
klass: {},
proto: {
setCwd: {
fn: "setCwd",
length: 1,
},
setEnv: {
fn: "setEnv",
length: 1,
},
setQuiet: {
fn: "setQuiet",
length: 1,
},
},
}),
];