Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/validation/websocket-per-socket-fairness-20260921.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Per-socket WebSocket receive fairness — 21 September 2026

## Product boundary

The unchanged packaged Code OSS remote extension host sends its one-byte `Ready` message about 2.7 ms after process start, but WebScene can dispatch that byte hundreds of milliseconds later. The best retained run records the browser transport connected at `1789952238161`, Node sending `Ready` at `1789952238978`, and the browser receiving it at `1789952239128`. Explorer paints about 50 ms after `Ready`, making incoming socket dispatch the remaining workspace-startup owner.

The native transport previously placed events from every WebSocket in one global FIFO. A burst on a bulk or management connection could therefore delay a latency-sensitive protocol event on an independent socket.

## Focused change

The bounded receive queue now keeps FIFO order within each socket and rotates between sockets with pending events. Global byte and event limits, one empty-to-ready wake, terminal-event admission, and asynchronous JavaScript dispatch remain unchanged.

## Focused gates

- A native two-socket regression queues 128 4 KiB frames on one socket and a one-byte protocol marker on the other. The marker is dispatched as event two.
- Browser WebSocket lifecycle: 100/100 cycles; p95 0.192 ms; at most 19 competing MessagePort events before an echo.
- Product-shaped protocol lifecycle: 20/20 cycles; Ready p95 2.65 ms; Initialized p95 11.91 ms; socket dispatch p95 11 ms.
- FileReader ordering and task-source regression: pass; 64 timer backlog completes in 0.324 ms.
- Settled V8 heap is flat in the focused socket and protocol gates.

The exact installed SDK and unchanged Code OSS package remain the product promotion gate for WebScene #289, #280, and #252.
Original file line number Diff line number Diff line change
Expand Up @@ -48,34 +48,56 @@ struct socket_record final {
struct native_websocket_transport::state final {
mutable std::mutex mutex;
std::unordered_map<uint64_t, std::shared_ptr<socket_record>> sockets;
std::deque<event> events;
// Preserve FIFO ordering within each WebSocket while rotating between
// sockets. A single global FIFO lets a bulk-transfer connection delay a
// latency-sensitive protocol byte on an otherwise independent socket.
std::unordered_map<uint64_t, std::deque<event>> events_by_socket;
std::deque<uint64_t> ready_sockets;
size_t queued_events{0};
size_t queued_bytes{0};
uint64_t next_socket_id{1};
bool shutting_down{false};
std::function<void()> event_available;

void evict_next_event()
{
if (ready_sockets.empty()) return;
const auto socket_id = ready_sockets.front();
ready_sockets.pop_front();
const auto found = events_by_socket.find(socket_id);
if (found == events_by_socket.end() || found->second.empty()) return;
auto& socket_events = found->second;
queued_bytes -= socket_events.front().payload.size();
socket_events.pop_front();
--queued_events;
if (socket_events.empty()) events_by_socket.erase(found);
else ready_sockets.push_back(socket_id);
}

bool enqueue(event value, bool priority = false)
{
std::function<void()> notify;
{
std::lock_guard lock(mutex);
if (shutting_down) return false;
if (priority) {
while (!events.empty()
&& (events.size() >= maximum_queued_websocket_events
while (queued_events != 0U
&& (queued_events >= maximum_queued_websocket_events
|| value.payload.size()
> maximum_queued_websocket_bytes - queued_bytes)) {
queued_bytes -= events.front().payload.size();
events.pop_front();
evict_next_event();
}
}
if (events.size() >= maximum_queued_websocket_events
if (queued_events >= maximum_queued_websocket_events
|| value.payload.size() > maximum_queued_websocket_bytes - queued_bytes) {
return false;
}
const auto was_empty = events.empty();
const auto was_empty = queued_events == 0U;
auto& socket_events = events_by_socket[value.socket_id];
if (socket_events.empty()) ready_sockets.push_back(value.socket_id);
queued_bytes += value.payload.size();
events.push_back(std::move(value));
++queued_events;
socket_events.push_back(std::move(value));
if (was_empty) notify = event_available;
}
if (notify) notify();
Expand Down Expand Up @@ -263,17 +285,25 @@ void native_websocket_transport::set_event_available_callback(
bool native_websocket_transport::try_pop(event& value)
{
std::lock_guard lock(state_->mutex);
if (state_->events.empty()) return false;
value = std::move(state_->events.front());
if (state_->ready_sockets.empty()) return false;
const auto socket_id = state_->ready_sockets.front();
state_->ready_sockets.pop_front();
const auto found = state_->events_by_socket.find(socket_id);
if (found == state_->events_by_socket.end() || found->second.empty()) return false;
auto& socket_events = found->second;
value = std::move(socket_events.front());
state_->queued_bytes -= value.payload.size();
state_->events.pop_front();
socket_events.pop_front();
--state_->queued_events;
if (socket_events.empty()) state_->events_by_socket.erase(found);
else state_->ready_sockets.push_back(socket_id);
return true;
}

bool native_websocket_transport::has_pending_events() const noexcept
{
std::lock_guard lock(state_->mutex);
return !state_->events.empty();
return state_->queued_events != 0U;
}

void native_websocket_transport::release(uint64_t socket_id)
Expand All @@ -300,7 +330,9 @@ void native_websocket_transport::shutdown()
state_->shutting_down = true;
state_->event_available = {};
sockets.swap(state_->sockets);
state_->events.clear();
state_->events_by_socket.clear();
state_->ready_sockets.clear();
state_->queued_events = 0;
state_->queued_bytes = 0;
}
for (auto& [socket_id, record] : sockets) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "webscene_native_engine.h"
#include "webscene_native_dom.h"
#include "webscene_embed_fallback.h"
#include "webscene_native_websocket.h"

#include <ixwebsocket/IXGetFreePort.h>
#include <ixwebsocket/IXNetSystem.h>
Expand Down Expand Up @@ -766,6 +767,7 @@ int main()
return 0;
}
if (selected == "websocket-file-reader") {
test_native_websocket_cross_socket_fairness();
test_native_websocket_browser_api();
test_native_websocket_protocol_handshake_timing();
test_native_file_reader_task_source_fairness();
Expand Down Expand Up @@ -1480,6 +1482,7 @@ int main()
"engine rejected an asynchronous low-memory request");
test_engine_memory_metrics_are_worker_snapshots(engine);
test_hidden_engine_reclamation_is_debounced_and_cancelable(engine);
test_native_websocket_cross_socket_fairness();
test_native_websocket_browser_api();
test_native_websocket_protocol_handshake_timing();
test_native_file_reader_task_source_fairness();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,130 @@ size_t load_websocket_cookie_document_v4(
return required;
}

void test_native_websocket_cross_socket_fairness()
{
require(ix::initNetSystem(), "cross-socket network initialization failed");
const auto port = ix::getFreePort();
require(port > 0, "cross-socket server could not reserve a port");

std::mutex server_mutex;
std::condition_variable server_changed;
ix::WebSocket* bulk_peer = nullptr;
bool bulk_registered = false;
bool marker_sent = false;
constexpr auto bulk_frames = 128U;
ix::WebSocketServer server(port, "127.0.0.1");
server.setOnClientMessageCallback(
[&](std::shared_ptr<ix::ConnectionState>,
ix::WebSocket& socket,
const ix::WebSocketMessagePtr& message) {
if (message->type != ix::WebSocketMessageType::Message) return;
if (message->str == "register-bulk") {
{
std::lock_guard lock(server_mutex);
bulk_peer = &socket;
bulk_registered = true;
}
server_changed.notify_all();
return;
}
if (message->str != "send-marker") return;
ix::WebSocket* target = nullptr;
{
std::lock_guard lock(server_mutex);
target = bulk_peer;
}
require(target != nullptr, "bulk socket was not registered");
const std::string bulk_payload(4096U, 'b');
for (auto index = 0U; index < bulk_frames; ++index) {
target->sendBinary(bulk_payload);
}
// Let the independent receive thread queue the bulk burst before
// the latency-sensitive byte arrives on this socket.
std::this_thread::sleep_for(std::chrono::milliseconds(20));
socket.sendBinary("R");
{
std::lock_guard lock(server_mutex);
marker_sent = true;
}
server_changed.notify_all();
});
const auto listening = server.listen();
require(listening.first, "cross-socket server could not listen");
server.start();

webscene_native::native_websocket_transport transport;
const auto address = std::string("ws://127.0.0.1:") + std::to_string(port);
const auto bulk_socket = transport.open(address + "/bulk", {}, {}, {});
const auto latency_socket = transport.open(address + "/latency", {}, {}, {});
require(bulk_socket != 0U && latency_socket != 0U,
"cross-socket clients could not open");

auto opened = 0U;
const auto open_deadline = std::chrono::steady_clock::now()
+ std::chrono::seconds(5);
while (opened < 2U && std::chrono::steady_clock::now() < open_deadline) {
webscene_native::native_websocket_transport::event event;
if (!transport.try_pop(event)) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
continue;
}
if (event.type
== webscene_native::native_websocket_transport::event_type::opened) {
++opened;
}
}
require(opened == 2U, "cross-socket clients did not open");
static constexpr std::string_view register_bulk{"register-bulk"};
require(transport.send(
bulk_socket,
reinterpret_cast<const uint8_t*>(register_bulk.data()),
register_bulk.size(),
false), "bulk registration send failed");
{
std::unique_lock lock(server_mutex);
require(server_changed.wait_for(lock, std::chrono::seconds(5), [&] {
return bulk_registered;
}), "bulk registration did not arrive");
}
static constexpr std::string_view send_marker{"send-marker"};
require(transport.send(
latency_socket,
reinterpret_cast<const uint8_t*>(send_marker.data()),
send_marker.size(),
false), "marker request send failed");
{
std::unique_lock lock(server_mutex);
require(server_changed.wait_for(lock, std::chrono::seconds(5), [&] {
return marker_sent;
}), "latency marker was not sent");
}
std::this_thread::sleep_for(std::chrono::milliseconds(20));

auto events_through_marker = 0U;
auto saw_marker = false;
webscene_native::native_websocket_transport::event event;
while (transport.try_pop(event)) {
++events_through_marker;
if (event.socket_id == latency_socket
&& event.type
== webscene_native::native_websocket_transport::event_type::message
&& event.payload.size() == 1U && event.payload.front() == 'R') {
saw_marker = true;
break;
}
}
require(saw_marker, "latency marker was not dispatched");
require(events_through_marker <= 2U,
"bulk WebSocket traffic delayed an independent socket");

transport.shutdown();
server.stop();
require(ix::uninitNetSystem(), "cross-socket network cleanup failed");
std::cout << "WebSocket cross-socket fairness gate: bulkFrames="
<< bulk_frames << " eventsThroughMarker=" << events_through_marker << "\n";
}

void test_native_websocket_browser_api()
{
require(ix::initNetSystem(), "IXWebSocket network initialization failed");
Expand Down
Loading