Repository navigation
Fix memory leaks & blocking syscall in Bun Shell #23636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -72,6 +72,21 @@ pub const EnvStr = packed struct(u128) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub fn memoryCost(this: EnvStr) usize { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const divisor: usize = brk: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (this.asRefCounted()) |refc| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| break :brk refc.refcount; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| break :brk 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (divisor == 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @branchHint(.unlikely); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return this.len / divisor; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+75
to
+88
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct memoryCost for refcounted vs borrowed slices
Compute by tag: use -pub fn memoryCost(this: EnvStr) usize {
- const divisor: usize = brk: {
- if (this.asRefCounted()) |refc| {
- break :brk refc.refcount;
- }
- break :brk 1;
- };
- if (divisor == 0) {
- @branchHint(.unlikely);
- return 0;
- }
-
- return this.len / divisor;
-}
+pub fn memoryCost(this: EnvStr) usize {
+ return switch (this.tag) {
+ .empty => 0,
+ .slice => 0, // memory owned elsewhere
+ .refcounted => brk: {
+ const refc = this.castRefCounted();
+ const rc = refc.refcount;
+ if (rc == 0) break :brk 0;
+ break :brk refc.byteSlice().len / rc;
+ },
+ };
+}📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub fn ref(this: EnvStr) void { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (this.asRefCounted()) |refc| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| refc.ref(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,13 @@ pub fn refSelf(this: *IOReader) *IOReader { | |
| return this; | ||
| } | ||
|
|
||
| pub fn memoryCost(this: *const IOReader) usize { | ||
| var size: usize = @sizeOf(IOReader); | ||
| size += this.buf.allocatedSlice().len; | ||
| size += this.readers.memoryCost(); | ||
| return size; | ||
| } | ||
|
|
||
| pub fn eventLoop(this: *IOReader) jsc.EventLoopHandle { | ||
| return this.evtloop; | ||
| } | ||
|
|
@@ -224,6 +231,14 @@ pub const IOReaderChildPtr = struct { | |
| }; | ||
| } | ||
|
|
||
| pub fn memoryCost(this: IOReaderChildPtr) usize { | ||
| if (this.ptr.is(Interpreter.Builtin.Cat)) { | ||
| // TODO: | ||
| return @sizeOf(Interpreter.Builtin.Cat); | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
|
Comment on lines
+234
to
+241
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick | 🔵 Trivial Avoid hardcoding Cat size; delegate to child memoryCost if available Returning Consider exposing a 🤖 Prompt for AI Agents |
||
| /// Return true if the child should be deleted | ||
| pub fn onReadChunk(this: IOReaderChildPtr, chunk: []const u8, remove: *bool) Yield { | ||
| return this.ptr.call("onIOReaderChunk", .{ chunk, remove }, Yield); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
Verify the extern function is implemented.
The extern declaration looks correct with proper SYSV_ABI calling convention. Based on the PR context, this should be implemented in
src/shell/interpreter.zig.🏁 Script executed:
Length of output: 0
🏁 Script executed:
Length of output: 802
Add missing implementation for ShellInterpreter__estimatedSize
No matching definition was found in the Zig sources (e.g.,
src/shell/interpreter.zig). Please implementShellInterpreter__estimatedSizeto satisfy the extern declaration.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's generated code!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Jarred-Sumner, you're absolutely right—my apologies for the confusion! I see now that
ShellInterpreter__estimatedSizeis generated by the codegen pipeline (viasrc/codegen/generate-classes.ts) from theestimatedSizemethod defined in the Zig code. The extern declaration is correctly placed in the generated header. Thank you for clarifying! 🐰/_ `---' _
| |
🧠 Learnings used