Truly independent web browser
LibIPC+LibWebView: Give helpers a deadline to answer the browser
Andreas Kling0b0be988d3d5
diff --git a/Libraries/LibIPC/Connection.h b/Libraries/LibIPC/Connection.hindex 17db5cf164f..3b3981ba68a 100644--- a/Libraries/LibIPC/Connection.h+++ b/Libraries/LibIPC/Connection.h@@ -113,6 +113,19 @@ public:return wait_for_specific_endpoint_message<typename RequestType::ResponseType>(MayCancel::Yes);}+ // NB: Responses have no ID, so the response to a request that missed its deadline would pass for the response to+ // the next request of that type. A caller that gets none while the connection is open must not use the+ // connection again.+ template<typename RequestType, typename... Args>+ OwnPtr<typename RequestType::ResponseType> send_sync_until(MonotonicTime deadline, Args&&... args)+ {+ if (post_sync_request(RequestType(forward<Args>(args)...)).is_error())+ return nullptr;+ if (auto message = wait_for_specific_endpoint_message_impl(RequestType::ResponseType::ENDPOINT_MAGIC, RequestType::ResponseType::static_message_id(), MayCancel::Yes, deadline))+ return message.template release_nonnull<typename RequestType::ResponseType>();+ return nullptr;+ }+// Waits for a message from the peer and handles none of the others meanwhile, which stay queued in order. None if// the deadline passes first, or the peer goes away.template<typename MessageType>diff --git a/Libraries/LibWebView/HelperProcess.cpp b/Libraries/LibWebView/HelperProcess.cppindex 656a125146c..9529a3a2957 100644--- a/Libraries/LibWebView/HelperProcess.cpp+++ b/Libraries/LibWebView/HelperProcess.cpp@@ -665,6 +665,28 @@ ErrorOr<RequestServerClientConnection> connect_new_request_server_client(Browsinreturn RequestServerManager::the().connect_new_client(*instance, session, site_binding);}+MonotonicTime deadline_for_helper_response()+{+ // NB: A helper that waits for a debugger does not answer until the user lets it go on.+ if (!Application::browser_options().debug_helper_processes.is_empty())+ return MonotonicTime::now() + AK::Duration::from_seconds(60 * 60 * 24 * 365);+ return MonotonicTime::now() + helper_response_timeout;+}++void end_unresponsive_helper(IPC::ConnectionBase& connection)+{+ Optional<u64> child_id;+ Application::process_manager().for_each_process([&](Process& process) {+ if (process.client<IPC::ConnectionBase>() == &connection)+ child_id = process.child_id();+ });+ if (child_id.has_value()) {+ warnln("Ending a helper process that did not respond in time");+ (void)Application::process_manager().terminate_child(*child_id, Core::Process::TerminationMode::Forceful);+ }+ connection.shutdown();+}+u64 allocate_audio_tab_id(){static u64 s_next_audio_tab_id { 1 };@@ -683,7 +705,7 @@ ErrorOr<IPC::TransportHandle> connect_new_audio_server_client(AudioServerControlif (!controller.is_open())return Error::from_string_literal("The AudioServer is gone");- auto response = controller.send_sync_but_allow_failure<Messages::AudioServerControl::ConnectNewClient>(audio_tab_id);+ auto response = send_sync_to_helper<Messages::AudioServerControl::ConnectNewClient>(controller, audio_tab_id);if (!response || !response->handle().has_value())return Error::from_string_literal("Failed to connect to AudioServer");return response->take_handle().release_value();@@ -702,7 +724,7 @@ static ErrorOr<IPC::TransportHandle> connect_new_video_decoder_server_client(Refif (!controller || !controller->is_open())controller = TRY(launch_video_decoder_server_process(owner_child_id, gpu_device_identity));- auto response = controller->send_sync_but_allow_failure<Messages::VideoDecoderServerControl::ConnectNewClient>();+ auto response = send_sync_to_helper<Messages::VideoDecoderServerControl::ConnectNewClient>(*controller);if (!response || !response->handle().has_value())return Error::from_string_literal("Failed to connect to VideoDecoderServer");return response->take_handle().release_value();@@ -725,7 +747,7 @@ ErrorOr<MediaServerClientConnection> connect_new_media_server_client(RefPtr<Medi};}- auto response = controller->send_sync_but_allow_failure<Messages::MediaServer::ConnectNewClient>();+ auto response = send_sync_to_helper<Messages::MediaServer::ConnectNewClient>(*controller);if (!response || !response->handle().has_value())return Error::from_string_literal("Failed to connect to MediaServer");return MediaServerClientConnection { response->take_handle().release_value(), response->client_id() };diff --git a/Libraries/LibWebView/HelperProcess.h b/Libraries/LibWebView/HelperProcess.hindex e23444dc66f..f1978f27a4d 100644--- a/Libraries/LibWebView/HelperProcess.h+++ b/Libraries/LibWebView/HelperProcess.h@@ -8,6 +8,7 @@#include <AK/Error.h>#include <AK/Optional.h>+#include <AK/Time.h>#include <LibGfx/GpuDeviceIdentity.h>#include <LibIPC/TransportHandle.h>#include <LibMediaClient/Client.h>@@ -22,6 +23,30 @@namespace WebView {+// How long a helper gets to answer a request that the browser waits for.+constexpr inline auto helper_response_timeout = AK::Duration::from_seconds(30);++// When a helper that is asked now has to have answered. A helper that the user debugs gets as long as it takes.+WEBVIEW_API MonotonicTime deadline_for_helper_response();++// Ends the helper at the other end of the connection, and closes the connection.+WEBVIEW_API void end_unresponsive_helper(IPC::ConnectionBase&);++// Asks a helper for something that the browser cannot go on without, such as a connection for another process. The+// browser's main thread waits for the response, so a helper that is running but never responds would stop the whole+// browser. Such a helper is ended instead, which is handled like a helper that crashed.+template<typename RequestType, typename ClientType, typename... Args>+OwnPtr<typename RequestType::ResponseType> send_sync_to_helper(ClientType& client, Args&&... args)+{+ auto deadline = deadline_for_helper_response();+ auto response = client.template send_sync_until<RequestType>(deadline, forward<Args>(args)...);+ // NB: A wait that ends before its deadline found the helper gone, which the connection handles by itself. A socket+ // stays open until then, so the connection being open does not tell the two apart.+ if (!response && client.is_open() && MonotonicTime::now() >= deadline)+ end_unresponsive_helper(client);+ return response;+}+WEBVIEW_API ErrorOr<NonnullRefPtr<WebView::WebContentClient>> launch_web_content_process(BrowsingSession&, Web::PageId initial_page_id, Web::HTML::CrossProcessId root_navigable_id);WEBVIEW_API ErrorOr<NonnullRefPtr<MediaClient::Client>> launch_media_server_process(u64 owner_child_id);diff --git a/Libraries/LibWebView/RequestServerManager.cpp b/Libraries/LibWebView/RequestServerManager.cppindex 847471b2c0b..d7ad92e0df5 100644--- a/Libraries/LibWebView/RequestServerManager.cpp+++ b/Libraries/LibWebView/RequestServerManager.cpp@@ -165,7 +165,7 @@ ErrorOr<RequestServerClientConnection> RequestServerManager::connect_new_client(// Random client IDs can collide across RequestServers. The UI identifies clients by ID alone, so retry collisions.for (size_t attempt = 0; attempt < 8; ++attempt) {- auto response = instance.control_client().send_sync_but_allow_failure<Messages::RequestServerControl::ConnectNewClient>(is_private, site_binding);+ auto response = send_sync_to_helper<Messages::RequestServerControl::ConnectNewClient>(instance.control_client(), is_private, site_binding);if (!response || response->client_id() < 0)return Error::from_string_literal("Failed to connect to RequestServer");diff --git a/Libraries/LibWebView/RequestServerSiteBindings.cpp b/Libraries/LibWebView/RequestServerSiteBindings.cppindex dec470f55f9..a19d37aa5cd 100644--- a/Libraries/LibWebView/RequestServerSiteBindings.cpp+++ b/Libraries/LibWebView/RequestServerSiteBindings.cpp@@ -216,7 +216,7 @@ void RequestServerSiteBindings::send(Site const& site) constif (!instance)return;- (void)instance->control_client().send_sync_but_allow_failure<Messages::RequestServerControl::BindClientToSite>(*client_id, site.top_level_site, site.frame_site);+ (void)send_sync_to_helper<Messages::RequestServerControl::BindClientToSite>(instance->control_client(), *client_id, site.top_level_site, site.frame_site);}}diff --git a/Libraries/LibWebView/WebContentClient.cpp b/Libraries/LibWebView/WebContentClient.cppindex 14433cac897..61fd4e91d23 100644--- a/Libraries/LibWebView/WebContentClient.cpp+++ b/Libraries/LibWebView/WebContentClient.cpp@@ -1239,7 +1239,7 @@ void WebContentClient::report_audio_capture_for_testing(Web::PageId labelled_pagErrorOr<void> WebContentClient::tell_audio_server_of_capture_permission(){- auto response = m_audio_server_client->send_sync_but_allow_failure<Messages::AudioServerControl::SetCapturePermission>(m_audio_tab_id, m_audio_capture_permitted);+ auto response = send_sync_to_helper<Messages::AudioServerControl::SetCapturePermission>(*m_audio_server_client, m_audio_tab_id, m_audio_capture_permitted);if (!response)return Error::from_string_literal("Failed to tell the AudioServer of the capture permission");return {};@@ -1300,7 +1300,7 @@ void WebContentClient::forget_audio_playback(Web::PageId page_id)ErrorOr<void> WebContentClient::tell_audio_server_of_playback_authorization(Audio::PlaybackGroupId group){- auto response = m_audio_server_client->send_sync_but_allow_failure<Messages::AudioServerControl::AuthorizePlaybackGroup>(m_audio_tab_id, group);+ auto response = send_sync_to_helper<Messages::AudioServerControl::AuthorizePlaybackGroup>(*m_audio_server_client, m_audio_tab_id, group);if (!response)return Error::from_string_literal("Failed to tell the AudioServer of a playback authorization");return {};