From 7693e2bfebd8602a67f7497f0571ecae5677c8d0 Mon Sep 17 00:00:00 2001 From: R Date: Sat, 5 Dec 2026 12:22:22 -0300 Subject: [PATCH] trystero: the room whose negotiation made a connection counts as the remote's advertisement, so that a page whose first frames Chrome dropped is bound again after the handshake times out The interop lane's "one room leaving" scenario failed on main (run 47519299767, 1 of 13 runs): the C++ peer, the lower id, offered in both rooms, and the page answered. Chrome drops what a page sends on an answering channel as it opens, and the page's presence and its @_hsready of the first room were lost. The C++ first room, bound to the connection by its own negotiation, never heard the page's readiness, timed the handshake out after 10 s and kicked the page from the room. Its rebinding over the live connection binds only a room the remote advertised, by its presence or by a kept frame of the room; a Trystero JS page advertises a room once per connection and, kicked from it, sends nothing more of it, so the first room never came back while the second went on. A connection now records the token of the room whose negotiation opened it as the remote's advertisement from the start: the remote answered or offered in that room's signaling. The app records it only on the connection it adopts, a held second connection keeps it for when it replaces the first, and the existing connection returned for another negotiation does not take it, since another remote may have answered that negotiation in the peer's name. After the kick, the rebinding (11 s) sends the room's presence, the page binds the room again, and both handshakes pass. The interop test gains the scenario "The page losing its first frames on the connection still joins both rooms": js.html's drop-early-sends drops what the page sends on the first channel it accepts for 300 ms, and the page joins room2 2 s later. It failed 7 runs of 7 before the change (room first never back in 30 s) and passed 20 runs of 20 after, in 21 s; the full browser interop passed 27 scenarios of 27, and "one room leaving" 10 runs of 10. Unit tests cover the advertisement on a started connection and on adoption, a held second, and its replacement. --- include/webcpp/trystero/detail/app_impl.hpp | 35 ++++--- include/webcpp/trystero/detail/connection.hpp | 16 +++- include/webcpp/trystero/room.hpp | 2 +- test/app_impl_test.cpp | 93 ++++++++++++------- test/connection_test.cpp | 21 ++++- test/oracle/interop/js.html | 29 ++++++ test/oracle/interop/run.mjs | 20 ++++ 7 files changed, 164 insertions(+), 52 deletions(-) diff --git a/include/webcpp/trystero/detail/app_impl.hpp b/include/webcpp/trystero/detail/app_impl.hpp index fb7f106..e7e4db0 100644 --- a/include/webcpp/trystero/detail/app_impl.hpp +++ b/include/webcpp/trystero/detail/app_impl.hpp @@ -304,10 +304,15 @@ class app_impl : public std::enable_shared_from_this { * clears it (dist/shared-peer.mjs:142-149): the rooms hear its end before the new * connection is adopted, and a room may adopt one to the peer from inside that call, which is * then the one shared, or ends in its turn if it is stale by then. One whose end is already - * under way, further up the stack, is let go: it ends no second time. */ + * under way, further up the stack, is let go: it ends no second time. + * + * `negotiated` is the token of the room whose negotiation opened the connection: an adopted + * connection records it as the remote's advertisement (connection::start), the existing one + * does not, since another remote may have answered the new negotiation in the peer's name. */ std::shared_ptr adopt(std::string_view peer_view, std::shared_ptr pc, - std::shared_ptr channel) { + std::shared_ptr channel, + std::string_view negotiated) { // `peer_view` may be a view into the stale connection, which its end frees. const std::pmr::string peer(peer_view, memory_); for (auto it = connections_.find(peer); it != connections_.end() && !it->second->live(); @@ -319,14 +324,16 @@ class app_impl : public std::enable_shared_from_this { connections_.erase(again); } } - return share_or_adopt(peer, std::move(pc), std::move(channel)); + return share_or_adopt(peer, std::move(pc), std::move(channel), negotiated); } private: /** A second connection to a peer, held unused until the peer shows which of the two it keeps: - * the frames its channel received meanwhile, in their order, and its deadline. */ + * the token of the room whose negotiation opened it, the frames its channel received + * meanwhile, in their order, and its deadline. */ struct second_connection { std::pmr::string peer; + std::pmr::string negotiated; std::shared_ptr pc; std::shared_ptr channel; std::pmr::deque frames; @@ -346,10 +353,11 @@ class app_impl : public std::enable_shared_from_this { // NOLINTBEGIN(performance-unnecessary-value-param): sinks, moved on std::shared_ptr share_or_adopt(std::string_view peer, std::shared_ptr pc, - std::shared_ptr channel) { + std::shared_ptr channel, + std::string_view negotiated) { // NOLINTEND(performance-unnecessary-value-param) if (auto existing = find(peer)) { - hold_or_close(peer, *existing, std::move(pc), std::move(channel)); + hold_or_close(peer, *existing, std::move(pc), std::move(channel), negotiated); return existing; } const auto weak = weak_from_this(); @@ -402,7 +410,7 @@ class app_impl : public std::enable_shared_from_this { for (const auto& [token, e] : rooms_) { tokens.emplace_back(token); } - c->start(tokens); + c->start(tokens, negotiated); return c; } @@ -412,10 +420,11 @@ class app_impl : public std::enable_shared_from_this { // NOLINTBEGIN(performance-unnecessary-value-param): sinks, moved on void hold_or_close(std::string_view peer, const connection& first, std::shared_ptr pc, - std::shared_ptr channel) { + std::shared_ptr channel, + std::string_view negotiated) { // NOLINTEND(performance-unnecessary-value-param) if (!first.used() && !seconds_.contains(peer)) { - hold_second(peer, std::move(pc), std::move(channel)); + hold_second(peer, std::move(pc), std::move(channel), negotiated); return; } channel->set_handlers({}); @@ -447,10 +456,11 @@ class app_impl : public std::enable_shared_from_this { * follows its reset at once: Chrome fails a send made in between and closes the channel with * that failure, which Trystero JS reports as a peer error. */ void hold_second(std::string_view peer, std::shared_ptr pc, - std::shared_ptr channel) { + std::shared_ptr channel, std::string_view negotiated) { auto second = std::allocate_shared( allocator(), second_connection{ .peer = std::pmr::string(peer, memory_), + .negotiated = std::pmr::string(negotiated, memory_), .pc = std::move(pc), .channel = std::move(channel), .frames = std::pmr::deque(memory_), @@ -539,7 +549,8 @@ class app_impl : public std::enable_shared_from_this { } log_("webrtc", "the second connection with ", peer, " replaces the first: the peer keeps it"); - const auto replacement = share_or_adopt(peer, second->pc, second->channel); + const auto replacement = + share_or_adopt(peer, second->pc, second->channel, second->negotiated); if (first) { first->end("the peer keeps the second connection"); } @@ -598,7 +609,7 @@ class app_impl : public std::enable_shared_from_this { second->deadline.reset(); frames = std::move(second->frames); log_("webrtc", "the second connection with ", peer, " replaces the first"); - replacement = share_or_adopt(peer, second->pc, second->channel); + replacement = share_or_adopt(peer, second->pc, second->channel, second->negotiated); } else { log_("webrtc", "the second connection with ", peer, " closed: the first ended"); close_second(*second); diff --git a/include/webcpp/trystero/detail/connection.hpp b/include/webcpp/trystero/detail/connection.hpp index 5258e7b..a5bd11c 100644 --- a/include/webcpp/trystero/detail/connection.hpp +++ b/include/webcpp/trystero/detail/connection.hpp @@ -73,7 +73,11 @@ class binding; // NOLINT(cppcoreguidelines-virtual-class-destructor) * connection, and its presence for a room comes once per connection, which a page can lose, since * Chrome drops what a page sends on an answering channel as it opens (its log says "Send failed * INVALID_STATE"). Without it the room would never bind the remote, whose frames of the room keep - * coming. + * coming. For the same loss, the room whose negotiation made the connection counts as advertised + * from the start (`start`): the remote answered or offered in that room's signaling, and a page + * whose presence and handshake frames were lost sends nothing more of that room once a kick ends + * its binding, so that the room's rebinding over the live connection would wait for an + * advertisement that never comes. * * Tip: it holds plain pointers to its bindings, which remove themselves before they go, so that it * keeps nothing of a room's resource after the room has ended. @@ -116,8 +120,9 @@ class connection : public std::enable_shared_from_this { connection& operator=(const connection&) = delete; /** Takes over the channel's and the connection's events, which replaces the handlers a room's - * negotiation set, and advertises `tokens`. */ - void start(std::span tokens) { + * negotiation set, records `negotiated`, the token of the room whose negotiation made this + * connection, as the remote's advertisement, and advertises `tokens`. */ + void start(std::span tokens, std::string_view negotiated) { const auto weak = weak_from_this(); const std::pmr::polymorphic_allocator<> allocator(memory_); channel_->set_handlers({ @@ -167,6 +172,7 @@ class connection : public std::enable_shared_from_this { }, }, }); + remote_tokens_.emplace(negotiated); for (const auto token : tokens) { send_presence(token, true); } @@ -185,8 +191,8 @@ class connection : public std::enable_shared_from_this { /** How many frames are kept for rooms not bound yet. */ [[nodiscard]] std::size_t kept_count() const { return kept_.size(); } - /** Whether the remote advertises the room of `token`: by its presence, or by a room frame of - * the token that the app kept. */ + /** Whether the remote advertises the room of `token`: by its presence, by a room frame of the + * token that the app kept, or by the negotiation of that room that made this connection. */ [[nodiscard]] bool advertised(std::string_view token) const { return remote_tokens_.contains(token); } diff --git a/include/webcpp/trystero/room.hpp b/include/webcpp/trystero/room.hpp index c83c85f..fd96560 100644 --- a/include/webcpp/trystero/room.hpp +++ b/include/webcpp/trystero/room.hpp @@ -1196,7 +1196,7 @@ class room_impl final : public std::enable_shared_from_this, public r return; } const std::pmr::string peer(peer_view, memory_); - const auto c = app_->adopt(peer, std::move(pc), std::move(channel)); + const auto c = app_->adopt(peer, std::move(pc), std::move(channel), token_); // Adoption logs synchronously: the sink may leave or destroy this room, or end the // connection. A farewell still counts as leaving and must not gain a new binding. if (left_) { diff --git a/test/app_impl_test.cpp b/test/app_impl_test.cpp index 257cb03..8d90ee9 100644 --- a/test/app_impl_test.cpp +++ b/test/app_impl_test.cpp @@ -334,9 +334,9 @@ void every_allocation_goes_through_the_apps_resource() { BOOST_TEST_EQ(app->password(), expected_password); app->register_room("r1", "a room token longer than the short string", room); app->add_user(); - app->adopt(peer, ends.local_pc, ends.local); + app->adopt(peer, ends.local_pc, ends.local, "a room token longer than the short string"); ends.local->close(); - app->adopt(peer, next.local_pc, next.local); + app->adopt(peer, next.local_pc, next.local, "a room token longer than the short string"); live = app->live_connections().size(); app->release_user(); } @@ -387,7 +387,7 @@ void adopt_starts_one_connection_per_peer() { app->register_room("r1", "T1", r1); app->register_room("r2", "T2", r2); const auto first = negotiate(io, net); - const auto c = app->adopt("B", first.local_pc, first.local); + const auto c = app->adopt("B", first.local_pc, first.local, "T1"); webcpp::test::require(BOOST_TEST(c != nullptr)); BOOST_TEST_EQ(c->peer(), "B"); BOOST_TEST(app->find("B") == c); @@ -397,7 +397,7 @@ void adopt_starts_one_connection_per_peer() { BOOST_TEST(*first.received == (std::vector>{presence("T1", true), presence("T2", true)})); const auto second = negotiate(io, net); - BOOST_TEST(app->adopt("B", second.local_pc, second.local) == c); + BOOST_TEST(app->adopt("B", second.local_pc, second.local, "T1") == c); settle(io); BOOST_TEST(second.local->is_open()); BOOST_TEST(c->live()); @@ -426,11 +426,11 @@ void another_connection_after_the_peers_first_frame_is_closed() { const auto app = make_app(io, net, log); app->register_room("r1", "T1", std::make_shared()); const auto first = negotiate(io, net); - const auto c = app->adopt("B", first.local_pc, first.local); + const auto c = app->adopt("B", first.local_pc, first.local, "T1"); first.remote->send(trystero::protocol::presence_frame("T1", true)); webcpp::test::require(BOOST_TEST(run_until(io, [&] { return c->used(); }))); const auto second = negotiate(io, net); - BOOST_TEST(app->adopt("B", second.local_pc, second.local) == c); + BOOST_TEST(app->adopt("B", second.local_pc, second.local, "T1") == c); BOOST_TEST(!second.local->is_open()); BOOST_TEST(log == (lines{ "webrtc: connection with B adopted", @@ -446,6 +446,33 @@ std::shared_ptr make_app_as(asio::io_context& io, return make_app(io, net, log, [self](trystero::app_config& c) { c.self_id = self; }); } +// The room whose negotiation opened a connection counts as advertised by the remote before any +// frame comes, as its presence would: a page can lose the presence it sends once per connection, +// and after a kick sends nothing more of that room, so the room's rebinding needs it. A held second +// connection keeps its own room's token for when it replaces the first; the first, returned for +// that second negotiation, does not take it, since the second may have another remote. +void the_negotiating_room_is_advertised() { + asio::io_context io; + trystero::test::memory_network net(io); + lines log; + const auto app = make_app_as(io, net, log, "A"); + app->register_room("r1", "T1", std::make_shared()); + app->register_room("r2", "T2", std::make_shared()); + const auto first = negotiate(io, net); + const auto c = app->adopt("B", first.local_pc, first.local, "T1"); + BOOST_TEST(c->advertised("T1")); + BOOST_TEST(!c->advertised("T2")); + const auto second = negotiate(io, net); + BOOST_TEST(app->adopt("B", second.local_pc, second.local, "T2") == c); + BOOST_TEST(!c->advertised("T2")); + first.remote->close(); + webcpp::test::require(BOOST_TEST(run_until(io, [&] { return app->find("B") != c; }))); + const auto replacement = app->find("B"); + webcpp::test::require(BOOST_TEST(replacement != nullptr)); + BOOST_TEST(replacement->advertised("T2")); + BOOST_TEST(!replacement->advertised("T1")); +} + // The lower id holds the second until the peer closes the first, as Trystero JS closes the other // connection of a crossed pair: the second then takes the first's place, after the rooms heard // the first's end, and hands them the frames it received meanwhile. @@ -457,9 +484,9 @@ void the_second_replaces_a_first_the_peer_closes() { const auto room = std::make_shared(); app->register_room("r1", "T1", room); const auto first = negotiate(io, net); - const auto c = app->adopt("B", first.local_pc, first.local); + const auto c = app->adopt("B", first.local_pc, first.local, "T1"); const auto second = negotiate(io, net); - app->adopt("B", second.local_pc, second.local); + app->adopt("B", second.local_pc, second.local, "T1"); second.remote->send(trystero::protocol::presence_frame("T1", true)); settle(io); BOOST_TEST(app->find("B") == c); // the lower id waits for the peer @@ -486,9 +513,9 @@ void the_higher_id_takes_the_second_the_peer_sends_on() { const auto room = std::make_shared(); app->register_room("r1", "T1", room); const auto first = negotiate(io, net); - const auto c = app->adopt("B", first.local_pc, first.local); + const auto c = app->adopt("B", first.local_pc, first.local, "T1"); const auto second = negotiate(io, net); - app->adopt("B", second.local_pc, second.local); + app->adopt("B", second.local_pc, second.local, "T1"); second.remote->send(trystero::protocol::presence_frame("T1", true)); webcpp::test::require(BOOST_TEST(run_until(io, [&] { return app->find("B") != c; }))); BOOST_TEST(!c->live()); @@ -510,9 +537,9 @@ void a_second_the_peer_leaves_alone_is_closed_in_time() { const auto app = make_app_as(io, net, log, self); app->register_room("r1", "T1", std::make_shared()); const auto first = negotiate(io, net); - const auto c = app->adopt("B", first.local_pc, first.local); + const auto c = app->adopt("B", first.local_pc, first.local, "T1"); const auto second = negotiate(io, net); - app->adopt("B", second.local_pc, second.local); + app->adopt("B", second.local_pc, second.local, "T1"); BOOST_TEST(net.advance(wait - 1ms)); settle(io); BOOST_TEST(second.local->is_open()); @@ -540,7 +567,7 @@ void the_connections_events_reach_the_rooms() { app->register_room("r2", "T2", r2); r1->on_end = [&] { app->unregister_room("T1", r1.get()); }; const auto ends = negotiate(io, net); - auto c = app->adopt("B", ends.local_pc, ends.local); + auto c = app->adopt("B", ends.local_pc, ends.local, "T1"); const std::weak_ptr weak = c; ends.remote->send(trystero::protocol::presence_frame("T1", true)); ends.remote->send(trystero::protocol::presence_frame("T9", true)); // no room of the app @@ -579,8 +606,8 @@ void the_last_user_ends_every_connection() { const auto c = negotiate(io, net); app->add_user(); app->add_user(); - const auto to_b = app->adopt("B", b.local_pc, b.local); - const auto to_c = app->adopt("C", c.local_pc, c.local); + const auto to_b = app->adopt("B", b.local_pc, b.local, "T1"); + const auto to_c = app->adopt("C", c.local_pc, c.local, "T1"); app->release_user(); BOOST_TEST(to_b->live() && to_c->live()); BOOST_TEST(room->events.empty()); @@ -594,7 +621,7 @@ void the_last_user_ends_every_connection() { app->release_user(); // no user left: nothing to release BOOST_TEST_EQ(room->events.size(), 2U); const auto d = negotiate(io, net); - const auto to_d = app->adopt("D", d.local_pc, d.local); + const auto to_d = app->adopt("D", d.local_pc, d.local, "T1"); app->add_user(); app->release_user(); BOOST_TEST(!to_d->live()); @@ -613,11 +640,11 @@ void a_stale_connection_ends_before_its_replacement() { app->register_room("r1", "T1", room); const auto first = negotiate(io, net); const auto second = negotiate(io, net); - const auto stale = app->adopt("B", first.local_pc, first.local); + const auto stale = app->adopt("B", first.local_pc, first.local, "T1"); first.local->close(); // closing: its close event is posted BOOST_TEST(app->find("B") == nullptr); BOOST_TEST(app->live_connections().empty()); - const auto fresh = app->adopt("B", second.local_pc, second.local); + const auto fresh = app->adopt("B", second.local_pc, second.local, "T1"); BOOST_TEST(fresh != stale); BOOST_TEST(room->events == (std::vector{"ended B: replaced by a new connection to the peer"})); @@ -641,15 +668,15 @@ void an_adoption_from_inside_a_stale_end_is_the_one_kept() { const auto first = negotiate(io, net); const auto second = negotiate(io, net); const auto third = negotiate(io, net); - app->adopt("B", first.local_pc, first.local); + app->adopt("B", first.local_pc, first.local, "T1"); std::shared_ptr nested; room->on_end = [&] { if (!nested) { - nested = app->adopt("B", third.local_pc, third.local); + nested = app->adopt("B", third.local_pc, third.local, "T1"); } }; first.local->close(); - const auto outer = app->adopt("B", second.local_pc, second.local); + const auto outer = app->adopt("B", second.local_pc, second.local, "T1"); webcpp::test::require(BOOST_TEST(nested != nullptr)); BOOST_TEST(outer == nested); BOOST_TEST(second.local->is_open()); @@ -678,16 +705,16 @@ void a_nested_adoption_gone_stale_ends_before_the_outer_one() { const auto first = negotiate(io, net); const auto second = negotiate(io, net); const auto third = negotiate(io, net); - app->adopt("B", first.local_pc, first.local); + app->adopt("B", first.local_pc, first.local, "T1"); std::shared_ptr nested; room->on_end = [&] { if (!nested) { - nested = app->adopt("B", third.local_pc, third.local); + nested = app->adopt("B", third.local_pc, third.local, "T1"); third.local->close(); // closing: its close event is posted } }; first.local->close(); - const auto outer = app->adopt("B", second.local_pc, second.local); + const auto outer = app->adopt("B", second.local_pc, second.local, "T1"); webcpp::test::require(BOOST_TEST(nested != nullptr)); BOOST_TEST(outer != nested); const std::vector ends{ @@ -714,7 +741,7 @@ void an_adoption_from_inside_an_end_under_way() { app->register_room("r1", "T1", room); const auto first = negotiate(io, net); const auto second = negotiate(io, net); - const auto c = app->adopt("B", first.local_pc, first.local); + const auto c = app->adopt("B", first.local_pc, first.local, "T1"); auto* m = std::pmr::get_default_resource(); const std::pmr::polymorphic_allocator<> alloc(m); const auto b = std::allocate_shared( @@ -731,7 +758,7 @@ void an_adoption_from_inside_an_end_under_way() { } std::shared_ptr nested; b->wait_drained({std::allocator_arg, alloc, - [&](bool) { nested = app->adopt("B", second.local_pc, second.local); }}); + [&](bool) { nested = app->adopt("B", second.local_pc, second.local, "T1"); }}); c->end("test"); webcpp::test::require(BOOST_TEST(nested != nullptr)); BOOST_TEST(nested != c); @@ -760,12 +787,12 @@ void an_end_takes_only_its_own_entry() { const auto c = negotiate(io, net); const auto again = negotiate(io, net); app->add_user(); - app->adopt("B", b.local_pc, b.local); - app->adopt("C", c.local_pc, c.local); + app->adopt("B", b.local_pc, b.local, "T1"); + app->adopt("C", c.local_pc, c.local, "T1"); std::shared_ptr nested; room->on_end = [&] { if (!nested) { - nested = app->adopt("C", again.local_pc, again.local); + nested = app->adopt("C", again.local_pc, again.local, "T1"); } }; app->release_user(); @@ -788,10 +815,11 @@ void a_view_into_the_stale_connection_names_its_replacement() { const auto first = negotiate(io, net); const auto second = negotiate(io, net); const std::string id(40, 'p'); // beyond the short string: the view is into the heap - const std::weak_ptr stale = app->adopt(id, first.local_pc, first.local); + const std::weak_ptr stale = + app->adopt(id, first.local_pc, first.local, "T1"); first.local->close(); const auto view = stale.lock()->peer(); - const auto fresh = app->adopt(view, second.local_pc, second.local); + const auto fresh = app->adopt(view, second.local_pc, second.local, "T1"); BOOST_TEST(stale.expired()); BOOST_TEST_EQ(fresh->peer(), id); BOOST_TEST(app->find(id) == fresh); @@ -807,7 +835,7 @@ void the_apps_destruction_is_quiet() { const auto room = std::make_shared(); app->register_room("r1", "T1", room); const auto ends = negotiate(io, net); - const auto c = app->adopt("B", ends.local_pc, ends.local); + const auto c = app->adopt("B", ends.local_pc, ends.local, "T1"); const auto logged = log.size(); app.reset(); settle(io); @@ -836,6 +864,7 @@ int main() { run_case(every_allocation_goes_through_the_apps_resource); run_case(the_registry_holds_rooms_by_a_weak_reference); run_case(adopt_starts_one_connection_per_peer); + run_case(the_negotiating_room_is_advertised); run_case(another_connection_after_the_peers_first_frame_is_closed); run_case(the_second_replaces_a_first_the_peer_closes); run_case(the_higher_id_takes_the_second_the_peer_sends_on); diff --git a/test/connection_test.cpp b/test/connection_test.cpp index 4409ad7..05958c9 100644 --- a/test/connection_test.cpp +++ b/test/connection_test.cpp @@ -171,8 +171,8 @@ link_pair::link_pair(std::span a_tokens, callbacks(a), detail::logger{}, m); b.c = std::allocate_shared(alloc, net, "A", answerer, answer_end, callbacks(b), detail::logger{}, m); - a.c->start(a_tokens); - b.c->start(b_tokens); + a.c->start(a_tokens, a_tokens.front()); + b.c->start(b_tokens, b_tokens.front()); } /** A binding of `token` on `c`, which records the action of every frame delivered to it. */ @@ -239,6 +239,22 @@ void start_advertises_every_token() { BOOST_TEST(p.a.c->advertised("T1") && !p.a.c->advertised("T2")); } +// The token a connection starts with as negotiated is the remote's advertisement from the start, +// before any frame of it, and is forgotten on the remote's withdrawal as any advertised token. +void the_negotiated_token_is_advertised_at_the_start() { + const std::array a_tokens{"T2", "T1"}; + const std::array b_tokens{"T2"}; + link_pair p(a_tokens, b_tokens); + BOOST_TEST(p.b.c->advertised("T2")); + BOOST_TEST(!p.b.c->advertised("T1")); + BOOST_TEST(p.b.presence.empty()); + p.a.c->send_presence("T2", false); + BOOST_TEST(run_until(p.io, [&] { return p.b.presence.size() == 3; })); + BOOST_TEST(p.b.presence == (std::vector{"+T2", "+T1", "-T2"})); + BOOST_TEST(!p.b.c->advertised("T2")); + BOOST_TEST(p.b.c->advertised("T1")); +} + void a_withdrawn_token_is_forgotten() { const std::array tokens{"T1"}; link_pair p(tokens, tokens); @@ -911,6 +927,7 @@ int main() { start_advertises_every_token(); the_first_frame_says_the_remote_uses_it(); replayed_frames_are_handled_in_order(); + the_negotiated_token_is_advertised_at_the_start(); a_withdrawn_token_is_forgotten(); a_bound_token_reaches_its_binding_and_another_is_kept(); a_withdrawn_token_drops_its_kept_frames(); diff --git a/test/oracle/interop/js.html b/test/oracle/interop/js.html index d912785..4bf80d4 100644 --- a/test/oracle/interop/js.html +++ b/test/oracle/interop/js.html @@ -205,6 +205,35 @@ } } +// With `drop-early-sends=`, every frame the page sends on the first data channel it accepts is dropped for that +// many milliseconds after the channel opens, as Chrome drops what a page sends on an answering channel as it opens (its +// log says "Send failed INVALID_STATE"): the remote never sees the page's first presence and handshake frames. +if (params.get('drop-early-sends')) { + const window_ms = Number(params.get('drop-early-sends')) + const opened = new WeakMap() + let accepted = null + const AcceptingConnection = window.RTCPeerConnection + window.RTCPeerConnection = class extends AcceptingConnection { + constructor(...args) { + super(...args) + this.addEventListener('datachannel', ({channel}) => { + if (accepted) return + accepted = channel + if (channel.readyState === 'open') opened.set(channel, performance.now()) + else channel.addEventListener('open', () => opened.set(channel, performance.now())) + }) + } + } + const channelSend = RTCDataChannel.prototype.send + RTCDataChannel.prototype.send = function (data) { + if (this === accepted && (!opened.has(this) || performance.now() - opened.get(this) < window_ms)) { + record('dropped-early', {}) + return + } + return channelSend.call(this, data) + } +} + const checksum = bytes => { let sum = 0 for (let i = 0; i < bytes.length; i++) sum = (sum + bytes[i] * (i + 1)) >>> 0 diff --git a/test/oracle/interop/run.mjs b/test/oracle/interop/run.mjs index 9864bdf..f7e8f06 100644 --- a/test/oracle/interop/run.mjs +++ b/test/oracle/interop/run.mjs @@ -1011,6 +1011,25 @@ const transientHandshakeScenario = (name, cppKind) => return null }) +// 10. The page's first frames lost, as in a CI run of scenario 5: the C++ peer, the lower id, offers in room; the page +// drops what it sends on the first channel it accepts for its first 300 ms (drop-early-sends), as Chrome does on an +// answering channel as it opens, so that its presence and its @_hsready of room never reach the C++ peer, whose room is +// bound by its own negotiation; the page joins room2 2 s later, over the live connection. The C++ peer times out the +// handshake of room and kicks the page from it; its rebinding, at one step (11 s) over the live connection, must bring +// the page back to room, and both rooms complete within 30 s. +const lostFirstFramesScenario = (name, cppKind) => + scenario(name, async open => { + const params = {room: room(), room2: room(), ...relayParams} + const cpp = await open(cppKind, {...params, self: '00000000000000000000'}) + const js = await open('js', {...params, 'drop-early-sends': 300, room2Delay: 2000}) + const ok = await until(() => twoRoomsComplete(cpp, js), 30_000) + if (!(await js.events()).some(e => e.kind === 'dropped-early')) return 'the page dropped no early frame' + const counts = await connectionCounts(cpp, cppKind, js) + const facts = `connections ${JSON.stringify(counts)}` + if (overBound(counts)) return `${facts}: past the bound of ${crossedBound}` + return ok ? null : `the two rooms did not both complete in 30 s; ${facts}` + }) + // Every scenario the options ask for, in turn. const scenarios = async () => { if (browserPeer) { @@ -1039,6 +1058,7 @@ const scenarios = async () => { await wrongPasswordScenario('A wrong password in one room, in both id orders, paced by the rebinding times', 'cpp') await roomFloodScenario('A flood in one room does not delay the pings of another', 'cpp') await transientHandshakeScenario('A transient handshake failure converges, in both id orders', 'cpp') + await lostFirstFramesScenario('The page losing its first frames on the connection still joins both rooms', 'cpp') } }