From 768748ec2db4faee7420f6de3349f40f3051bbb5 Mon Sep 17 00:00:00 2001 From: Ciro Spaciari Date: Tue, 16 Sep 2025 15:39:18 -0700 Subject: [PATCH] fix(Server) check before downgrade (#22726) ### What does this PR do? This fix assertion in debug mode, remove flag `has_js_deinited` since js_value is tagged with finalized and is more reliable ### How did you verify your code works? run `bun-debug test test/js/bun/http/serve.test.ts` Screenshot 2025-09-16 at 14 51 40 --- src/bun.js/api/server.zig | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/bun.js/api/server.zig b/src/bun.js/api/server.zig index a29342e6a1..22e5e90dc3 100644 --- a/src/bun.js/api/server.zig +++ b/src/bun.js/api/server.zig @@ -548,10 +548,9 @@ pub fn NewServer(protocol_enum: enum { http, https }, development_kind: enum { d cached_hostname: bun.String = bun.String.empty, - flags: packed struct(u4) { + flags: packed struct(u3) { deinit_scheduled: bool = false, terminated: bool = false, - has_js_deinited: bool = false, has_handled_all_closed_promise: bool = false, } = .{}, @@ -1427,7 +1426,6 @@ pub fn NewServer(protocol_enum: enum { http, https }, development_kind: enum { d pub fn finalize(this: *ThisServer) void { httplog("finalize", .{}); this.js_value.finalize(); - this.flags.has_js_deinited = true; this.deinitIfWeCan(); } @@ -1460,7 +1458,7 @@ pub fn NewServer(protocol_enum: enum { http, https }, development_kind: enum { d if (this.hasActiveWebSockets()) "active" else "no", this.flags.has_handled_all_closed_promise, if (this.all_closed_promise.strong.has()) "has" else "no", - this.flags.has_js_deinited, + this.js_value == .finalized, }); const vm = this.globalThis.bunVM(); @@ -1510,7 +1508,7 @@ pub fn NewServer(protocol_enum: enum { http, https }, development_kind: enum { d } // Only free the memory if the JS reference has been freed too - if (this.flags.has_js_deinited) { + if (this.js_value == .finalized) { this.scheduleDeinit(); } } @@ -1539,8 +1537,9 @@ pub fn NewServer(protocol_enum: enum { http, https }, development_kind: enum { d } pub fn stop(this: *ThisServer, abrupt: bool) void { - this.js_value.downgrade(); - + if (this.js_value.isNotEmpty()) { + this.js_value.downgrade(); + } if (this.config.allow_hot and this.config.id.len > 0) { if (this.globalThis.bunVM().hotMap()) |hot| { hot.remove(this.config.id);