From f638fd93d4dbc15c7ff3db054d1180da0057662a Mon Sep 17 00:00:00 2001 From: Claude Bot Date: Sun, 26 Oct 2025 06:42:32 +0000 Subject: [PATCH] Fix: Set websocketServerContext field after server toJS() Move the websocketServerContextSetCached call from listen() to BunObject.serve(), right after toJS() creates the server JSValue. This ensures: 1. The server JSValue exists before we try to set fields on it 2. The websocket context is stored in the correct place alongside routeList 3. The GC can properly track the relationship --- src/bun.js/api/BunObject.zig | 3 +++ src/bun.js/api/server.zig | 8 -------- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/src/bun.js/api/BunObject.zig b/src/bun.js/api/BunObject.zig index 60a13a1e83..07d6c690f2 100644 --- a/src/bun.js/api/BunObject.zig +++ b/src/bun.js/api/BunObject.zig @@ -1086,6 +1086,9 @@ pub fn serve(globalObject: *jsc.JSGlobalObject, callframe: *jsc.CallFrame) bun.J if (route_list_object != .zero) { ServerType.js.routeListSetCached(obj, globalObject, route_list_object); } + if (config.websocket_js_context != .zero) { + ServerType.js.websocketServerContextSetCached(obj, globalObject, config.websocket_js_context); + } server.js_value.setStrong(obj, globalObject); if (config.allow_hot) { diff --git a/src/bun.js/api/server.zig b/src/bun.js/api/server.zig index 183d1bab3e..1a4266616d 100644 --- a/src/bun.js/api/server.zig +++ b/src/bun.js/api/server.zig @@ -2726,14 +2726,6 @@ pub fn NewServer(protocol_enum: enum { http, https }, development_kind: enum { d httplog("listen", .{}); var app: *App = undefined; const globalThis = this.globalThis; - - // Set the websocketServerContext on the server JSValue to keep it alive through GC - if (this.config.websocket_js_context != .zero) { - const server_value = this.jsValueAssertAlive(); - const js = bun.JSC.Codegen.JSHTTPServerClass(comptime protocol, comptime development); - js.websocketServerContextSetCachedValue(server_value, globalThis, this.config.websocket_js_context); - } - var route_list_value = jsc.JSValue.zero; if (ssl_enabled) { bun.BoringSSL.load();