Merge branch 'main' into jarred/dnsy

This commit is contained in:
Jarred Sumner
2024-05-01 01:59:58 -07:00
committed by GitHub
8 changed files with 63 additions and 27 deletions

View File

@@ -25,6 +25,8 @@
#include <libusockets.h>
#include <iostream>
extern "C" int bun_is_exiting();
namespace uWS {
struct Loop {
private:
@@ -89,14 +91,17 @@ private:
/* What to do with loops created with existingNativeLoop? */
struct LoopCleaner {
~LoopCleaner() {
if(loop && cleanMe) {
// There's no need to call this destructor if Bun is in the process of exiting.
// This is both a performance thing, and also to prevent freeing some things which are not meant to be freed
// such as uv_tty_t
if(loop && cleanMe && !bun_is_exiting()) {
loop->free();
}
}
Loop *loop = nullptr;
bool cleanMe = false;
};
static LoopCleaner &getLazyLoop() {
static thread_local LoopCleaner lazyLoop;
return lazyLoop;

View File

@@ -98,7 +98,17 @@ pub fn exit(code: u8) noreturn {
exitWide(@as(u32, code));
}
var is_exiting = std.atomic.Value(bool).init(false);
export fn bun_is_exiting() c_int {
return @intFromBool(isExiting());
}
pub fn isExiting() bool {
return is_exiting.load(.Monotonic);
}
pub fn exitWide(code: u32) noreturn {
is_exiting.store(true, .Monotonic);
if (comptime Environment.isMac) {
std.c.exit(@bitCast(code));
}

View File

@@ -176,7 +176,7 @@ pub const Subprocess = struct {
};
}
};
process: *Process = undefined,
process: *Process,
stdin: Writable,
stdout: Readable,
stderr: Readable,
@@ -208,6 +208,7 @@ pub const Subprocess = struct {
is_sync: bool = false,
killed: bool = false,
has_stdin_destructor_called: bool = false,
finalized: bool = false,
};
pub const SignalCode = bun.SignalCode;
@@ -672,7 +673,13 @@ pub const Subprocess = struct {
this.flags.has_stdin_destructor_called = true;
this.weak_file_sink_stdin_ptr = null;
this.updateHasPendingActivity();
if (this.flags.finalized) {
// if the process has already been garbage collected, we can free the memory now
bun.default_allocator.destroy(this);
} else {
// otherwise update the pending activity flag
this.updateHasPendingActivity();
}
}
pub fn doSend(this: *Subprocess, global: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSValue {
@@ -1514,7 +1521,12 @@ pub const Subprocess = struct {
this.process.detach();
this.process.deref();
bun.default_allocator.destroy(this);
this.flags.finalized = true;
if (this.weak_file_sink_stdin_ptr == null) {
// if no file sink exists we can free immediately
bun.default_allocator.destroy(this);
}
}
pub fn getExited(

View File

@@ -18,7 +18,7 @@ class MessagePortChannelProviderImpl;
class GlobalScope : public RefCounted<GlobalScope>, public EventTargetWithInlineData {
WTF_MAKE_ISO_ALLOCATED(GlobalScope);
uint32_t m_messageEventCount;
uint32_t m_messageEventCount{0};
static void onDidChangeListenerImpl(EventTarget&, const AtomString&, OnDidChangeListenerKind);

View File

@@ -226,20 +226,6 @@ pub inline fn cast(comptime To: type, value: anytype) To {
return @ptrCast(@alignCast(value));
}
extern fn strlen(ptr: [*c]const u8) usize;
pub fn indexOfSentinel(comptime Elem: type, comptime sentinel: Elem, ptr: [*:sentinel]const Elem) usize {
if (Elem == u8 and sentinel == 0) {
return strlen(ptr);
} else {
var i: usize = 0;
while (ptr[i] != sentinel) {
i += 1;
}
return i;
}
}
pub fn len(value: anytype) usize {
return switch (@typeInfo(@TypeOf(value))) {
.Array => |info| info.len,
@@ -260,11 +246,11 @@ pub fn len(value: anytype) usize {
@compileError("length of pointer with no sentinel");
const sentinel = @as(*align(1) const info.child, @ptrCast(sentinel_ptr)).*;
return indexOfSentinel(info.child, sentinel, value);
return std.mem.indexOfSentinel(info.child, sentinel, value);
},
.C => {
assert(value != null);
return indexOfSentinel(info.child, 0, value);
return std.mem.indexOfSentinel(info.child, 0, value);
},
.Slice => value.len,
},
@@ -1318,7 +1304,7 @@ fn lenSliceTo(ptr: anytype, comptime end: meta.Elem(@TypeOf(ptr))) usize {
if (array_info.sentinel) |sentinel_ptr| {
const sentinel = @as(*align(1) const array_info.child, @ptrCast(sentinel_ptr)).*;
if (sentinel == end) {
return indexOfSentinel(array_info.child, end, ptr);
return std.mem.indexOfSentinel(array_info.child, end, ptr);
}
}
return std.mem.indexOfScalar(array_info.child, ptr, end) orelse array_info.len;
@@ -1336,13 +1322,13 @@ fn lenSliceTo(ptr: anytype, comptime end: meta.Elem(@TypeOf(ptr))) usize {
},
.C => {
assert(ptr != null);
return indexOfSentinel(ptr_info.child, end, ptr);
return std.mem.indexOfSentinel(ptr_info.child, end, ptr);
},
.Slice => {
if (ptr_info.sentinel) |sentinel_ptr| {
const sentinel = @as(*align(1) const ptr_info.child, @ptrCast(sentinel_ptr)).*;
if (sentinel == end) {
return indexOfSentinel(ptr_info.child, sentinel, ptr);
return std.mem.indexOfSentinel(ptr_info.child, sentinel, ptr);
}
}
return std.mem.indexOfScalar(ptr_info.child, ptr, end) orelse ptr.len;

View File

@@ -468,3 +468,5 @@ pub extern "C" fn Bun__ttySetMode(fd: c_int, mode: c_int) c_int;
pub extern "C" fn bun_initialize_process() void;
pub extern "C" fn bun_restore_stdio() void;
pub extern "C" fn open_as_nonblocking_tty(i32, i32) i32;
pub extern fn strlen(ptr: [*c]const u8) usize;

View File

@@ -20,7 +20,6 @@ comptime {
extern fn bun_warn_avx_missing(url: [*:0]const u8) void;
pub extern "C" var _environ: ?*anyopaque;
pub extern "C" var environ: ?*anyopaque;
pub fn main() void {
bun.crash_handler.init();
@@ -51,3 +50,25 @@ pub fn main() void {
bun.CLI.Cli.start(bun.default_allocator);
bun.Global.exit(0);
}
pub const overrides = struct {
pub const mem = struct {
extern "C" fn wcslen(s: [*:0]const u16) usize;
pub fn indexOfSentinel(comptime T: type, comptime sentinel: T, p: [*:sentinel]const T) usize {
if (comptime T == u16 and sentinel == 0 and Environment.isWindows) {
return wcslen(p);
}
if (comptime T == u8 and sentinel == 0) {
return bun.C.strlen(p);
}
var i: usize = 0;
while (p[i] != sentinel) {
i += 1;
}
return i;
}
};
};