Skip to content

Truly independent web browser

LibWebView: Drop the spare WebContent process once it is lost

Luke Wilde044558e74cda
diff --git a/Libraries/LibWebView/Application.cpp b/Libraries/LibWebView/Application.cpp
index 80f54de0838..0fe7f7331bc 100644
--- a/Libraries/LibWebView/Application.cpp
+++ b/Libraries/LibWebView/Application.cpp
@@ -1533,6 +1533,9 @@ bool Application::same_origin_tabs_may_share_a_process() const
ErrorOr<NonnullRefPtr<WebContentPage>> Application::open_page_for_new_tab(IsPrivate is_private)
{
+ if (m_spare_web_content_process && !m_spare_web_content_process->is_open())
+ m_spare_web_content_process = nullptr;
+
RefPtr<WebContentClient> web_content_client;
if (is_private == IsPrivate::Yes) {
web_content_client = TRY(create_web_content_client(IsPrivate::Yes, allocate_page_id()));
@@ -2219,6 +2222,8 @@ void Application::process_did_exit(Process&& process, Optional<int>)
break;
case ProcessType::WebContent:
if (auto client = process.client<WebContentClient>()) {
+ if (m_spare_web_content_process == client)
+ m_spare_web_content_process = nullptr;
client->did_lose_process();
if (!report_name.is_empty())
client->did_save_crash_report(report_name);
diff --git a/Tests/LibWebView/CMakeLists.txt b/Tests/LibWebView/CMakeLists.txt
index 1d276e28329..c58247ed37d 100644
--- a/Tests/LibWebView/CMakeLists.txt
+++ b/Tests/LibWebView/CMakeLists.txt
@@ -147,6 +147,7 @@ if (NOT WIN32)
TestRequestServerDiskCaches.cpp
TestShadowTreeTooltipsAndLinks.cpp
TestSharedWorkerHost.cpp
+ TestSpareWebContentProcess.cpp
TestTitleElementChanges.cpp
TestUserActivation.cpp
TestViewSource.cpp
diff --git a/Tests/LibWebView/TestSpareWebContentProcess.cpp b/Tests/LibWebView/TestSpareWebContentProcess.cpp
new file mode 100644
index 00000000000..8fcf73480fe
--- /dev/null
+++ b/Tests/LibWebView/TestSpareWebContentProcess.cpp
@@ -0,0 +1,132 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include "HeadlessTest.h"
+#include <AK/HashTable.h>
+#include <AK/LexicalPath.h>
+#include <LibCore/EventLoop.h>
+#include <LibCore/System.h>
+#include <LibGfx/SystemTheme.h>
+#include <LibTest/TestCase.h>
+#include <LibWebView/Application.h>
+#include <LibWebView/HeadlessWebView.h>
+#include <LibWebView/ProcessManager.h>
+#include <LibWebView/Utilities.h>
+#include <LibWebView/WebContentClient.h>
+#include <LibWebView/WebContentPage.h>
+#include <signal.h>
+
+namespace {
+
+class TestApplication : public Test::HeadlessApplication {
+ WEB_VIEW_APPLICATION(TestApplication)
+
+public:
+ explicit TestApplication(Optional<ByteString> ladybird_binary_path)
+ : Test::HeadlessApplication(move(ladybird_binary_path))
+ {
+ }
+
+ bool has_spare_web_content_process() const
+ {
+ return WebView::Application::has_spare_web_content_process();
+ }
+};
+
+Core::AnonymousBuffer load_theme()
+{
+ auto theme_path = LexicalPath::join(WebView::s_ladybird_resource_root, "themes"sv, "Default.ini"sv);
+ return MUST(Gfx::load_system_theme(theme_path.string()));
+}
+
+pid_t wait_for_spare_web_content_process(TestApplication const& app)
+{
+ Core::EventLoop::current().spin_until([&] { return app.has_spare_web_content_process(); });
+
+ Optional<pid_t> spare_pid;
+ WebView::Application::process_manager().for_each_process([&](WebView::Process& process) {
+ if (process.type() == WebView::ProcessType::WebContent && process.title() == "(spare)"_utf16)
+ spare_pid = process.pid();
+ });
+ VERIFY(spare_pid.has_value());
+ return *spare_pid;
+}
+
+bool connection_to_web_content_process_is_open(pid_t pid)
+{
+ bool is_open = false;
+ WebView::WebContentClient::for_each_client([&](WebView::WebContentClient& client) {
+ if (client.pid() != pid)
+ return IterationDecision::Continue;
+ is_open = client.is_open();
+ return IterationDecision::Break;
+ });
+ return is_open;
+}
+
+}
+
+TEST_CASE(spare_is_dropped_when_its_process_exits)
+{
+ Test::HeadlessTestEnvironment environment { "TestSpareWebContentProcess"sv };
+ HashTable<pid_t> exited_pids;
+ auto app = TRY_OR_FAIL(environment.create_application<TestApplication>());
+ auto theme = load_theme();
+
+ auto& process_manager = WebView::Application::process_manager();
+ process_manager.on_process_exited = [&, handle_exit = move(process_manager.on_process_exited)](WebView::Process&& process, Optional<int> exit_status) {
+ auto pid = process.pid();
+ handle_exit(move(process), exit_status);
+ exited_pids.set(pid);
+ };
+
+ auto first_tab = WebView::HeadlessWebView::create(theme, { 800, 600 });
+ auto spare_pid = wait_for_spare_web_content_process(*app);
+
+ MUST(Core::System::kill(spare_pid, SIGKILL));
+ Core::EventLoop::current().spin_until([&] { return exited_pids.contains(spare_pid); });
+ EXPECT(!app->has_spare_web_content_process());
+
+ auto second_tab = WebView::HeadlessWebView::create(theme, { 800, 600 });
+ EXPECT(second_tab->page().is_open());
+ EXPECT_NE(second_tab->web_content_pid(), spare_pid);
+}
+
+TEST_CASE(spare_is_not_taken_once_its_connection_closed)
+{
+ Test::HeadlessTestEnvironment environment { "TestSpareWebContentProcess"sv };
+ Optional<WebView::Process> held_spare_exit;
+ Optional<int> held_spare_exit_status;
+ auto app = TRY_OR_FAIL(environment.create_application<TestApplication>());
+ auto theme = load_theme();
+
+ auto first_tab = WebView::HeadlessWebView::create(theme, { 800, 600 });
+ auto spare_pid = wait_for_spare_web_content_process(*app);
+
+ // The application handles the spare's exit only once the test lets it.
+ auto& process_manager = WebView::Application::process_manager();
+ auto handle_exit = move(process_manager.on_process_exited);
+ process_manager.on_process_exited = [&](WebView::Process&& process, Optional<int> exit_status) {
+ if (process.pid() != spare_pid) {
+ handle_exit(move(process), exit_status);
+ return;
+ }
+ held_spare_exit = move(process);
+ held_spare_exit_status = exit_status;
+ };
+
+ MUST(Core::System::kill(spare_pid, SIGKILL));
+ Core::EventLoop::current().spin_until([&] {
+ return held_spare_exit.has_value() && !connection_to_web_content_process_is_open(spare_pid);
+ });
+
+ auto second_tab = WebView::HeadlessWebView::create(theme, { 800, 600 });
+ EXPECT_NE(second_tab->web_content_pid(), spare_pid);
+
+ handle_exit(held_spare_exit.release_value(), held_spare_exit_status);
+ process_manager.on_process_exited = move(handle_exit);
+ EXPECT(second_tab->page().is_open());
+}