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
This commit is contained in:
Claude Bot
2025-10-26 06:42:32 +00:00
parent edb7ba6332
commit f638fd93d4
2 changed files with 3 additions and 8 deletions

View File

@@ -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) {

View File

@@ -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();