aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2023-08-24 01:32:22 -0700
committerGravatar GitHub <noreply@github.com> 2023-08-24 01:32:22 -0700
commit9c68abdb8d51951f83b4b253cf5dd3922c2c58b5 (patch)
tree66847ebf9ae02116de8be94312fb930e246ec5cf /src/bun.js
parentad326b77342dd3d8585a30b7da803d32f9c11fe2 (diff)
downloadbun-9c68abdb8d51951f83b4b253cf5dd3922c2c58b5.tar.gz
bun-9c68abdb8d51951f83b4b253cf5dd3922c2c58b5.tar.zst
bun-9c68abdb8d51951f83b4b253cf5dd3922c2c58b5.zip
wip (#4282)
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Diffstat (limited to 'src/bun.js')
-rw-r--r--src/bun.js/api/bun/dns_resolver.zig3
-rw-r--r--src/bun.js/api/bun/socket.zig13
-rw-r--r--src/bun.js/api/bun/subprocess.zig22
-rw-r--r--src/bun.js/api/server.zig11
-rw-r--r--src/bun.js/bindings/ZigGlobalObject.cpp11
-rw-r--r--src/bun.js/webcore/streams.zig3
6 files changed, 54 insertions, 9 deletions
diff --git a/src/bun.js/api/bun/dns_resolver.zig b/src/bun.js/api/bun/dns_resolver.zig
index ed76dc777..5c9be467f 100644
--- a/src/bun.js/api/bun/dns_resolver.zig
+++ b/src/bun.js/api/bun/dns_resolver.zig
@@ -1395,6 +1395,9 @@ pub const DNSResolver = struct {
this: *DNSResolver,
poll: *JSC.FilePoll,
) void {
+ var vm = this.vm;
+ defer vm.drainMicrotasks();
+
var channel = this.channel orelse {
_ = this.polls.orderedRemove(@as(i32, @intCast(poll.fd)));
poll.deinit();
diff --git a/src/bun.js/api/bun/socket.zig b/src/bun.js/api/bun/socket.zig
index 7d8025872..5da6c1f40 100644
--- a/src/bun.js/api/bun/socket.zig
+++ b/src/bun.js/api/bun/socket.zig
@@ -178,6 +178,8 @@ const Handlers = struct {
socket_context: *uws.SocketContext,
pub fn exit(this: *Scope, ssl: bool, wrapped: WrappedType) void {
+ var vm = this.handlers.vm;
+ defer vm.drainMicrotasks();
this.handlers.markInactive(ssl, this.socket_context, wrapped);
}
};
@@ -1144,6 +1146,8 @@ fn NewSocket(comptime ssl: bool) type {
const handlers = this.handlers;
const callback = handlers.onWritable;
if (callback == .zero) return;
+ var vm = handlers.vm;
+ defer vm.drainMicrotasks();
const globalObject = handlers.globalObject;
const this_value = this.getThisValue(globalObject);
@@ -1170,6 +1174,8 @@ fn NewSocket(comptime ssl: bool) type {
const callback = handlers.onTimeout;
if (callback == .zero) return;
+ var vm = handlers.vm;
+ defer vm.drainMicrotasks();
const globalObject = handlers.globalObject;
const this_value = this.getThisValue(globalObject);
@@ -1189,10 +1195,12 @@ fn NewSocket(comptime ssl: bool) type {
defer this.markInactive();
const handlers = this.handlers;
- this.poll_ref.unrefOnNextTick(handlers.vm);
+ var vm = handlers.vm;
+ this.poll_ref.unrefOnNextTick(vm);
const callback = handlers.onConnectError;
var globalObject = handlers.globalObject;
+ defer vm.drainMicrotasks();
const err = JSC.SystemError{
.errno = errno,
.message = bun.String.static("Failed to connect"),
@@ -1362,6 +1370,9 @@ fn NewSocket(comptime ssl: bool) type {
const callback = handlers.onEnd;
if (callback == .zero) return;
+ var vm = handlers.vm;
+ defer vm.drainMicrotasks();
+
// the handlers must be kept alive for the duration of the function call
// that way if we need to call the error handler, we can
var scope = handlers.enter(socket.context());
diff --git a/src/bun.js/api/bun/subprocess.zig b/src/bun.js/api/bun/subprocess.zig
index b8bf6939c..6cd6d9016 100644
--- a/src/bun.js/api/bun/subprocess.zig
+++ b/src/bun.js/api/bun/subprocess.zig
@@ -1392,6 +1392,8 @@ pub const Subprocess = struct {
JSValue.zero;
subprocess.this_jsvalue = out;
+ var send_exit_notification = false;
+
if (comptime !is_sync) {
var poll = JSC.FilePoll.init(jsc_vm, pidfd, .{}, Subprocess, subprocess);
subprocess.poll_ref = poll;
@@ -1406,13 +1408,20 @@ pub const Subprocess = struct {
@panic("This shouldn't happen");
}
- // process has already exited
- // https://cs.github.com/libuv/libuv/blob/b00d1bd225b602570baee82a6152eaa823a84fa6/src/unix/process.c#L1007
- subprocess.onExitNotification();
+ send_exit_notification = true;
+ lazy = false;
},
}
}
+ defer {
+ if (send_exit_notification) {
+ // process has already exited
+ // https://cs.github.com/libuv/libuv/blob/b00d1bd225b602570baee82a6152eaa823a84fa6/src/unix/process.c#L1007
+ subprocess.onExitNotification();
+ }
+ }
+
if (subprocess.stdin == .buffered_input) {
subprocess.stdin.buffered_input.remain = switch (subprocess.stdin.buffered_input.source) {
.blob => subprocess.stdin.buffered_input.source.blob.slice(),
@@ -1495,6 +1504,13 @@ pub const Subprocess = struct {
return sync_value;
}
+ pub fn onExitNotificationTask(this: *Subprocess) void {
+ var vm = this.globalThis.bunVM();
+ defer vm.drainMicrotasks();
+ std.debug.assert(!this.is_sync);
+ this.wait(false);
+ }
+
pub fn onExitNotification(
this: *Subprocess,
) void {
diff --git a/src/bun.js/api/server.zig b/src/bun.js/api/server.zig
index d6a9e1c6e..445b94617 100644
--- a/src/bun.js/api/server.zig
+++ b/src/bun.js/api/server.zig
@@ -3480,6 +3480,8 @@ pub const ServerWebSocket = struct {
const onMessageHandler = this.handler.onMessage;
if (onMessageHandler.isEmptyOrUndefinedOrNull()) return;
var globalObject = this.handler.globalObject;
+ // This is the start of a task.
+ defer globalObject.bunVM().drainMicrotasks();
const arguments = [_]JSValue{
this.getThisValue(),
@@ -3587,8 +3589,11 @@ pub const ServerWebSocket = struct {
var handler = this.handler;
var cb = handler.onPing;
if (cb.isEmptyOrUndefinedOrNull()) return;
-
var globalThis = handler.globalObject;
+
+ // This is the start of a task.
+ defer globalThis.bunVM().drainMicrotasks();
+
const result = cb.call(
globalThis,
&[_]JSC.JSValue{ this.this_value, if (this.binary_type == .Buffer)
@@ -3625,6 +3630,10 @@ pub const ServerWebSocket = struct {
if (cb.isEmptyOrUndefinedOrNull()) return;
var globalThis = handler.globalObject;
+
+ // This is the start of a task.
+ defer globalThis.bunVM().drainMicrotasks();
+
const result = cb.call(
globalThis,
&[_]JSC.JSValue{ this.this_value, if (this.binary_type == .Buffer)
diff --git a/src/bun.js/bindings/ZigGlobalObject.cpp b/src/bun.js/bindings/ZigGlobalObject.cpp
index d3bd623dd..5aee64451 100644
--- a/src/bun.js/bindings/ZigGlobalObject.cpp
+++ b/src/bun.js/bindings/ZigGlobalObject.cpp
@@ -3498,15 +3498,18 @@ EncodedJSValue GlobalObject::assignToStream(JSValue stream, JSValue controller)
this->m_assignToStream.set(vm, this, function);
}
- auto scope = DECLARE_CATCH_SCOPE(vm);
auto callData = JSC::getCallData(function);
JSC::MarkedArgumentBuffer arguments;
arguments.append(stream);
arguments.append(controller);
- auto result = call(this, function, callData, JSC::jsUndefined(), arguments);
- if (scope.exception())
- return JSC::JSValue::encode(scope.exception());
+ WTF::NakedPtr<JSC::Exception> returnedException = nullptr;
+
+ auto result = JSC::profiledCall(this, ProfilingReason::API, function, callData, JSC::jsUndefined(), arguments, returnedException);
+ if (returnedException.get()) {
+ auto* exception = WTFMove(returnedException.get());
+ return JSC::JSValue::encode(exception);
+ }
return JSC::JSValue::encode(result);
}
diff --git a/src/bun.js/webcore/streams.zig b/src/bun.js/webcore/streams.zig
index 771d34db0..1301a7280 100644
--- a/src/bun.js/webcore/streams.zig
+++ b/src/bun.js/webcore/streams.zig
@@ -3863,6 +3863,8 @@ pub const FIFO = struct {
return;
}
+ defer JSC.VirtualMachine.get().drainMicrotasks();
+
if (comptime Environment.isMac) {
if (sizeOrOffset == 0 and is_hup and this.drained) {
this.close();
@@ -4750,6 +4752,7 @@ pub fn NewReadyWatcher(
}
pub fn onPoll(this: *Context, sizeOrOffset: i64, _: u16) void {
+ defer JSC.VirtualMachine.get().drainMicrotasks();
ready(this, sizeOrOffset);
}