Skip to content

Commit

Permalink
Bun.$ should keep the Bun process alive while a command is running
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner committed Jun 18, 2024
1 parent 3a17a2c commit 0765e76
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/shell/interpreter.zig
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,7 @@ pub const Interpreter = struct {
async_commands_executing: u32 = 0,

globalThis: *JSC.JSGlobalObject,
ref: bun.Async.KeepAlive = .{},

flags: packed struct(u8) {
done: bool = false,
Expand Down Expand Up @@ -1235,7 +1236,7 @@ pub const Interpreter = struct {
interpreter.this_jsvalue = JSC.Codegen.JSShellInterpreter.toJS(interpreter, globalThis);
JSC.Codegen.JSShellInterpreter.resolveSetCached(interpreter.this_jsvalue, globalThis, resolve);
JSC.Codegen.JSShellInterpreter.rejectSetCached(interpreter.this_jsvalue, globalThis, reject);

interpreter.ref.activate(JSC.VirtualMachine.get().uwsLoop());
bun.Analytics.Features.shell += 1;
return interpreter.this_jsvalue;
}
Expand Down Expand Up @@ -1656,6 +1657,8 @@ pub const Interpreter = struct {
defer decrPendingActivityFlag(&this.has_pending_activity);

if (this.event_loop == .js) {
this.ref.disable();

defer this.deinitAfterJSRun();
this.exit_code = exit_code;
if (this.this_jsvalue != .zero) {
Expand All @@ -1676,6 +1679,8 @@ pub const Interpreter = struct {
defer decrPendingActivityFlag(&this.has_pending_activity);

if (this.event_loop == .js) {
this.ref.disable();

if (this.this_jsvalue != .zero) {
if (JSC.Codegen.JSShellInterpreter.rejectGetCached(this.this_jsvalue)) |reject| {
reject.call(this.globalThis, &[_]JSValue{ JSValue.jsNumberFromChar(1), this.getBufferedStdout(), this.getBufferedStderr() });
Expand All @@ -1692,6 +1697,7 @@ pub const Interpreter = struct {
for (this.jsobjs) |jsobj| {
jsobj.unprotect();
}
this.ref.disable();
this.root_io.deref();
this.root_shell.deinitImpl(false, false);
this.this_jsvalue = .zero;
Expand All @@ -1705,6 +1711,7 @@ pub const Interpreter = struct {
this.root_shell._buffered_stdout.owned.deinitWithAllocator(bun.default_allocator);
}
this.this_jsvalue = .zero;
this.ref.disable();
this.allocator.destroy(this);
}

Expand Down
6 changes: 6 additions & 0 deletions test/js/bun/shell/shell-keepalive-fixture-1.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions test/js/bun/shell/shell-keepalive-fixture-2.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions test/js/bun/shell/shell-keepalive.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { expect, test } from "bun:test";
import { join } from "path";
import "harness";

test("shell should stay alive while a builtin command is in progress", async () => {
expect([join(import.meta.dir, "shell-keepalive-fixture-1.js")]).toRun();
});

test("shell should stay alive while a non-builtin command is in progress", async () => {
expect([join(import.meta.dir, "shell-keepalive-fixture-2.js")]).toRun();

Check failure on line 10 in test/js/bun/shell/shell-keepalive.test.ts

View workflow job for this annotation

GitHub Actions / Test linux-x64-baseline / Tests

error:

Command /home/runner/work/bun/bun/test/js/bun/shell/shell-keepalive-fixture-2.js failed: hi at /home/runner/work/bun/bun/test/js/bun/shell/shell-keepalive.test.ts:10:67 at /home/runner/work/bun/bun/test/js/bun/shell/shell-keepalive.test.ts:9:76

Check failure on line 10 in test/js/bun/shell/shell-keepalive.test.ts

View workflow job for this annotation

GitHub Actions / Test linux-x64 / Tests

error:

Command /home/runner/work/bun/bun/test/js/bun/shell/shell-keepalive-fixture-2.js failed: hi at /home/runner/work/bun/bun/test/js/bun/shell/shell-keepalive.test.ts:10:67 at /home/runner/work/bun/bun/test/js/bun/shell/shell-keepalive.test.ts:9:76

Check failure on line 10 in test/js/bun/shell/shell-keepalive.test.ts

View workflow job for this annotation

GitHub Actions / Test linux-aarch64 / Tests

error:

Command /home/runner/work/bun/bun/test/js/bun/shell/shell-keepalive-fixture-2.js failed: hi at /home/runner/work/bun/bun/test/js/bun/shell/shell-keepalive.test.ts:10:67 at /home/runner/work/bun/bun/test/js/bun/shell/shell-keepalive.test.ts:9:76
});

0 comments on commit 0765e76

Please sign in to comment.