diff --git a/src/bun.js.zig b/src/bun.js.zig index c5a9a27d6f..e17e20471f 100644 --- a/src/bun.js.zig +++ b/src/bun.js.zig @@ -36,7 +36,6 @@ pub const Run = struct { .args = ctx.args, .graph = graph_ptr, .is_main_thread = true, - .destruct_main_thread_on_exit = bun.getRuntimeFeatureFlag(.BUN_DESTRUCT_VM_ON_EXIT), }), .arena = arena, .ctx = ctx, @@ -174,7 +173,6 @@ pub const Run = struct { .debugger = ctx.runtime_options.debugger, .dns_result_order = DNSResolver.Order.fromStringOrDie(ctx.runtime_options.dns_result_order), .is_main_thread = true, - .destruct_main_thread_on_exit = bun.getRuntimeFeatureFlag(.BUN_DESTRUCT_VM_ON_EXIT), }, ), .arena = arena, diff --git a/src/bun.js/VirtualMachine.zig b/src/bun.js/VirtualMachine.zig index 7e91bcbf28..1e352ff587 100644 --- a/src/bun.js/VirtualMachine.zig +++ b/src/bun.js/VirtualMachine.zig @@ -176,12 +176,6 @@ channel_ref_overridden: bool = false, // if one disconnect event listener should be ignored channel_ref_should_ignore_one_disconnect_event_listener: bool = false, -/// Whether this VM should be destroyed after it exits, even if it is the main thread's VM. -/// Worker VMs are always destroyed on exit, regardless of this setting. Setting this to -/// true may expose bugs that would otherwise only occur using Workers. Controlled by -/// Options.destruct_main_thread_on_exit. -destruct_main_thread_on_exit: bool, - /// A set of extensions that exist in the require.extensions map. Keys /// contain the leading '.'. Value is either a loader for built in /// functions, or an index into JSCommonJSExtensions. @@ -218,6 +212,13 @@ pub fn initRequestBodyValue(this: *VirtualMachine, body: jsc.WebCore.Body.Value) return .init(body, &this.body_value_hive_allocator); } +/// Whether this VM should be destroyed after it exits, even if it is the main thread's VM. +/// Worker VMs are always destroyed on exit, regardless of this setting. Setting this to +/// true may expose bugs that would otherwise only occur using Workers. Controlled by +pub fn shouldDestructMainThreadOnExit(_: *const VirtualMachine) bool { + return bun.getRuntimeFeatureFlag(.BUN_DESTRUCT_VM_ON_EXIT); +} + pub threadlocal var is_bundler_thread_for_bytecode_cache: bool = false; pub fn uwsLoop(this: *const VirtualMachine) *uws.Loop { @@ -836,7 +837,7 @@ pub fn onExit(this: *VirtualMachine) void { extern fn Zig__GlobalObject__destructOnExit(*JSGlobalObject) void; pub fn globalExit(this: *VirtualMachine) noreturn { - if (this.destruct_main_thread_on_exit and this.is_main_thread) { + if (this.shouldDestructMainThreadOnExit()) { Zig__GlobalObject__destructOnExit(this.global); this.deinit(); } @@ -989,7 +990,7 @@ pub fn initWithModuleGraph( .ref_strings_mutex = .{}, .standalone_module_graph = opts.graph.?, .debug_thread_id = if (Environment.allow_assert) std.Thread.getCurrentId(), - .destruct_main_thread_on_exit = opts.destruct_main_thread_on_exit, + .initial_script_execution_context_identifier = if (opts.is_main_thread) 1 else std.math.maxInt(i32), }; vm.source_mappings.init(&vm.saved_source_map_table); @@ -1111,7 +1112,7 @@ pub fn init(opts: Options) !*VirtualMachine { .ref_strings = jsc.RefString.Map.init(allocator), .ref_strings_mutex = .{}, .debug_thread_id = if (Environment.allow_assert) std.Thread.getCurrentId(), - .destruct_main_thread_on_exit = opts.destruct_main_thread_on_exit, + .initial_script_execution_context_identifier = if (opts.is_main_thread) 1 else std.math.maxInt(i32), }; vm.source_mappings.init(&vm.saved_source_map_table); @@ -1270,8 +1271,6 @@ pub fn initWorker( .standalone_module_graph = worker.parent.standalone_module_graph, .worker = worker, .debug_thread_id = if (Environment.allow_assert) std.Thread.getCurrentId(), - // This option is irrelevant for Workers - .destruct_main_thread_on_exit = false, .initial_script_execution_context_identifier = @as(i32, @intCast(worker.execution_context_id)), }; vm.source_mappings.init(&vm.saved_source_map_table); @@ -1363,7 +1362,7 @@ pub fn initBake(opts: Options) anyerror!*VirtualMachine { .ref_strings = jsc.RefString.Map.init(allocator), .ref_strings_mutex = .{}, .debug_thread_id = if (Environment.allow_assert) std.Thread.getCurrentId(), - .destruct_main_thread_on_exit = opts.destruct_main_thread_on_exit, + .initial_script_execution_context_identifier = if (opts.is_main_thread) 1 else std.math.maxInt(i32), }; vm.source_mappings.init(&vm.saved_source_map_table); diff --git a/src/cli/test_command.zig b/src/cli/test_command.zig index 570ecc7b19..33011c5169 100644 --- a/src/cli/test_command.zig +++ b/src/cli/test_command.zig @@ -1373,7 +1373,6 @@ pub const TestCommand = struct { .smol = ctx.runtime_options.smol, .debugger = ctx.runtime_options.debugger, .is_main_thread = true, - .destruct_main_thread_on_exit = bun.getRuntimeFeatureFlag(.BUN_DESTRUCT_VM_ON_EXIT), }, ); vm.argv = ctx.passthrough;