From 94a98f51918bb365c6a23f0754d93e357ae296e2 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Fri, 17 Apr 2026 16:32:00 +0100 Subject: [PATCH] LibWeb: Surface websocket startup failures to JS If establish_web_socket_connection() fails before a Requests::WebSocket object exists, the DOM WebSocket used to ignore that error and remain silently stuck with no message, error, or close events. Dispatch error and close in that setup-failure path so callers see a terminal websocket state instead of hanging until the harness timeout. --- Libraries/LibWeb/WebSockets/WebSocket.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Libraries/LibWeb/WebSockets/WebSocket.cpp b/Libraries/LibWeb/WebSockets/WebSocket.cpp index 8a587484ae..aca5437daf 100644 --- a/Libraries/LibWeb/WebSockets/WebSocket.cpp +++ b/Libraries/LibWeb/WebSockets/WebSocket.cpp @@ -106,8 +106,13 @@ WebIDL::ExceptionOr> WebSocket::construct_impl(JS::Realm& rea Platform::EventLoopPlugin::the().deferred_invoke(GC::create_function(vm.heap(), [web_socket, url_record, protocols_sequence = move(protocols_sequence)]() { auto& client = HTML::relevant_settings_object(*web_socket); - // 1. Establish a WebSocket connection given urlRecord, protocols, and client. [FETCH] - (void)web_socket->establish_web_socket_connection(*url_record, protocols_sequence, client); + // 1. Establish a WebSocket connection given urlRecord, protocols, and client. [FETCH] + // AD-HOC: We don't yet implement this method to spec, so it's possible for the connection to fail before we + // make a Requests::WebSocket. If so, we need to manually error and close it. + if (web_socket->establish_web_socket_connection(*url_record, protocols_sequence, client).is_error()) { + web_socket->on_error(); + web_socket->on_close(to_underlying(::WebSocket::CloseStatusCode::AbnormalClosure), String {}, false); + } })); return web_socket; @@ -194,6 +199,7 @@ bool WebSocket::must_survive_garbage_collection() const ErrorOr WebSocket::establish_web_socket_connection(URL::URL const& url_record, Vector const& protocols, HTML::EnvironmentSettingsObject& client) { // FIXME: Integrate properly with FETCH as per https://fetch.spec.whatwg.org/#websocket-opening-handshake + // That means following https://websockets.spec.whatwg.org/#concept-websocket-establish auto& window_or_worker = as(client.global_object()); auto origin_string = window_or_worker.origin().to_byte_string();