Skip to content

Truly independent web browser

LibIPC: Discard queued messages when a connection shuts down

Shannon Booth7efd07cb422f
diff --git a/Libraries/LibIPC/Connection.cpp b/Libraries/LibIPC/Connection.cpp
index a02305c67e9..40585a32782 100644
--- a/Libraries/LibIPC/Connection.cpp
+++ b/Libraries/LibIPC/Connection.cpp
@@ -64,10 +64,18 @@ ErrorOr<void> ConnectionBase::post_sync_request(Message const& request)
void ConnectionBase::shutdown()
{
- m_transport->close();
+ VERIFY(m_owner_thread_id.is_current_thread());
if (m_died)
return;
m_died = true;
+ m_transport->close();
+
+ m_unprocessed_messages.clear();
+ for (auto batch : m_dispatching_message_batches) {
+ for (auto& message : batch)
+ message.clear();
+ }
+
die();
}
@@ -109,6 +117,8 @@ void ConnectionBase::dispatch_pending_messages()
void ConnectionBase::handle_messages()
{
VERIFY(m_owner_thread_id.is_current_thread());
+ if (m_died)
+ return;
auto messages = move(m_unprocessed_messages);
m_dispatching_message_batches.append(messages.span());
ScopeGuard remove_batch = [&] { m_dispatching_message_batches.take_last(); };
@@ -153,6 +163,8 @@ void ConnectionBase::wait_for_transport_to_become_readable()
ConnectionBase::PeerEOF ConnectionBase::drain_messages_from_peer()
{
VERIFY(m_owner_thread_id.is_current_thread());
+ if (m_died)
+ return PeerEOF::Yes;
bool parse_error = false;
auto schedule_shutdown = m_transport->read_as_many_messages_as_possible_without_blocking([&](auto&& raw_message) {
auto bytes = raw_message.bytes.bytes();
@@ -173,7 +185,6 @@ ConnectionBase::PeerEOF ConnectionBase::drain_messages_from_peer()
// dispatching that would run handlers on a connection that's closed under them. So, drop it all and shut down now,
// rather than after one last dispatch; see set_peer_owns_this_process().
if (schedule_shutdown == Transport::ShouldShutdown::Yes && m_peer_owns_this_process) {
- m_unprocessed_messages.clear();
shutdown();
return PeerEOF::Yes;
}
@@ -245,7 +256,7 @@ OwnPtr<IPC::Message> ConnectionBase::wait_for_specific_endpoint_message_impl(u32
}
if (peer_disconnected_during_wait) {
- // Don't dispatch any remaining queued messages here. wait_for_specific_endpoint_message_impl can be entered
+ // shutdown() discards any remaining queued messages. wait_for_specific_endpoint_message_impl can be entered
// from any sync IPC call — including from inside a constructor whose members are still being initialized. (See
// issue #9582. PageHost's constructor issues a sync IPC before ConnectionFromClient::m_page_host has been
// assigned.) Re-entering arbitrary handlers from here can hit uninitialized state and crash. shutdown() closes
diff --git a/Tests/LibIPC/TestConnection.cpp b/Tests/LibIPC/TestConnection.cpp
index e195e266449..f094f1992b2 100644
--- a/Tests/LibIPC/TestConnection.cpp
+++ b/Tests/LibIPC/TestConnection.cpp
@@ -240,6 +240,58 @@ TEST_CASE(taking_unprocessed_messages_preserves_nested_batch_order_and_unrelated
EXPECT(connection->take_unprocessed_messages(TEST_MAGIC, TARGET_MESSAGE_ID).is_empty());
}
+TEST_CASE(shutdown_stops_current_dispatch_batch)
+{
+ Core::EventLoop loop;
+ auto pair = TRY_OR_FAIL(IPC::Transport::create_paired());
+ CountingStub stub;
+ auto connection = TestConnection::construct(stub, move(pair.local));
+ stub.on_message = [&](IPC::Message const& message) {
+ if (message.message_id() == OTHER_MESSAGE_ID)
+ connection->shutdown();
+ };
+
+ connection->inject_unprocessed_message(make<TestMessage>(OTHER_MESSAGE_ID));
+ connection->inject_unprocessed_message(make<TestMessage>(TARGET_MESSAGE_ID));
+ connection->handle_messages();
+
+ EXPECT_EQ(stub.handle_count(), 1u);
+ EXPECT_EQ(connection->die_count(), 1u);
+ EXPECT(!connection->is_open());
+}
+
+TEST_CASE(shutdown_discards_pending_messages_from_all_batches)
+{
+ Core::EventLoop loop;
+ auto pair = TRY_OR_FAIL(IPC::Transport::create_paired());
+ CountingStub stub;
+ auto connection = TestConnection::construct(stub, move(pair.local));
+
+ constexpr int shutdown_message_id = 3;
+ constexpr u32 response_endpoint_magic = TEST_MAGIC + 1;
+ Vector<int> dispatched;
+ stub.on_message = [&](IPC::Message const& message) {
+ dispatched.append(message.message_id());
+ if (message.message_id() == OTHER_MESSAGE_ID) {
+ connection->inject_unprocessed_message(make<TestMessage>(shutdown_message_id));
+ connection->inject_unprocessed_message(make<TestMessage>(TARGET_MESSAGE_ID));
+ connection->handle_messages();
+ } else if (message.message_id() == shutdown_message_id) {
+ connection->inject_unprocessed_message(make<TestMessage>(TARGET_MESSAGE_ID));
+ connection->inject_unprocessed_message(make<TestMessage>(TARGET_MESSAGE_ID, 0, response_endpoint_magic));
+ connection->shutdown();
+ EXPECT(connection->take_unprocessed_messages(TEST_MAGIC, TARGET_MESSAGE_ID).is_empty());
+ EXPECT(!connection->call_wait_for_specific_endpoint_message_impl(response_endpoint_magic, TARGET_MESSAGE_ID));
+ }
+ };
+
+ connection->inject_unprocessed_message(make<TestMessage>(OTHER_MESSAGE_ID));
+ connection->inject_unprocessed_message(make<TestMessage>(TARGET_MESSAGE_ID));
+ connection->handle_messages();
+
+ EXPECT_EQ(dispatched, (Vector<int> { OTHER_MESSAGE_ID, shutdown_message_id }));
+}
+
TEST_CASE(anonymous_buffer_size_uses_64_bits)
{
constexpr size_t buffer_size = 4096;
@@ -313,6 +365,8 @@ TEST_CASE(sync_wait_failure_does_not_dispatch_queued_messages)
auto connection = TestConnection::construct(stub, move(pair.local));
connection->inject_unprocessed_message(make<TestMessage>(OTHER_MESSAGE_ID));
+ // Queue a deferred dispatch before the transport can observe EOF.
+ EXPECT_EQ(connection->drain_messages_from_peer(), TestConnection::PeerEOF::No);
// Hang up the peer before waiting for the response.
{
@@ -324,10 +378,11 @@ TEST_CASE(sync_wait_failure_does_not_dispatch_queued_messages)
EXPECT(!response);
EXPECT_EQ(stub.handle_count(), 0u);
- // Drain the deferred invocations queued by the EOF drain; the thread's event queue outlives this
- // test's scope, and the stub must not be dispatched to after it is destroyed.
+ // The wait may not shut down if EOF was seen before it started.
+ connection->shutdown();
while (loop.pump(Core::EventLoop::WaitMode::PollForEvents) != 0)
;
+ EXPECT_EQ(stub.handle_count(), 0u);
}
TEST_CASE(async_posts_from_many_threads_all_arrive)