Truly independent web browser
LibIPC: Let a connection react to a sync request that lost its peer
Andreas Kling0f94a427e510
diff --git a/Libraries/LibIPC/Connection.cpp b/Libraries/LibIPC/Connection.cppindex 40585a32782..14f3ff4e445 100644--- a/Libraries/LibIPC/Connection.cpp+++ b/Libraries/LibIPC/Connection.cpp@@ -7,6 +7,7 @@*/#include <AK/ScopeGuard.h>+#include <AK/TemporaryChange.h>#include <AK/Vector.h>#include <LibIPC/Connection.h>#include <LibIPC/Message.h>@@ -58,7 +59,7 @@ ErrorOr<void> ConnectionBase::post_sync_request(Message const& request)// rather than hand the failure to a caller that may have no way to carry on — as WebKit's sendSyncMessage() does// (thru didFailToSendSyncMessage) for a connection with setShouldExitOnSyncMessageSendFailure().if (result.is_error() && m_peer_owns_this_process && !is_open())- shutdown();+ did_lose_peer_during_sync_request();return result;}@@ -185,7 +186,11 @@ 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) {- shutdown();+ // NB: A wait for a sync response acts on this once it sees that no response came, and shuts down then.+ if (m_is_waiting_for_sync_response)+ m_unprocessed_messages.clear();+ else+ shutdown();return PeerEOF::Yes;}@@ -208,6 +213,7 @@ ConnectionBase::PeerEOF ConnectionBase::drain_messages_from_peer()OwnPtr<IPC::Message> ConnectionBase::wait_for_specific_endpoint_message_impl(u32 endpoint_magic, int message_id){VERIFY(m_owner_thread_id.is_current_thread());+ TemporaryChange waiting_for_sync_response { m_is_waiting_for_sync_response, true };bool peer_disconnected_during_wait = false;auto take_matching_message = [&]() -> OwnPtr<IPC::Message> {@@ -261,11 +267,11 @@ OwnPtr<IPC::Message> ConnectionBase::wait_for_specific_endpoint_message_impl(u32// 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// the transport and calls die(). That exits processes cleanly — the same as queued close_server message would.- shutdown();+ did_lose_peer_during_sync_request();} else if (m_peer_owns_this_process && !is_open()) {// NB: A peer that owns this process may also have been found gone before the wait began — e.g. by the// transport's IO thread, right after the request was posted; see set_peer_owns_this_process().- shutdown();+ did_lose_peer_during_sync_request();}return {};diff --git a/Libraries/LibIPC/Connection.h b/Libraries/LibIPC/Connection.hindex e1dc76edf6a..173ae79a0ba 100644--- a/Libraries/LibIPC/Connection.h+++ b/Libraries/LibIPC/Connection.h@@ -33,10 +33,11 @@ public:virtual void die() { }// Tells the connection that its peer owns this process — i.e. that the process exists only to serve that peer, and- // that die() ends it. Such a process has nothing left to do once that peer has closed the connection. So, the- // connection then shuts down rather than be used any further: It drops whatever the peer sent before closing,- // rather than dispatch that to handlers that'd find the connection closed under them. And it shuts down on a sync- // request, rather than fail one whose caller may have no way to carry on without the response.+ // that die() ends it, right away or once its event loop unwinds. Such a process has nothing left to do once that+ // peer has closed the connection. So, the connection then shuts down rather than be used any further: It drops+ // whatever the peer sent before closing, rather than dispatch that to handlers that'd find the connection closed+ // under them. And it shuts down on a sync request, rather than fail one whose caller may have no way to carry on+ // without the response.// Gecko/WebKit/Blink do the same for a child process's connection to its parent, each exiting the process right// there: Gecko SetAbortOnError, Blink TerminateSelfOnDisconnect, WebKit// setDidCloseOnConnectionWorkQueueCallback/setShouldExitOnSyncMessageSendFailure.@@ -51,6 +52,10 @@ public:// its answer this way, without dispatching anything else out of turn.Vector<NonnullOwnPtr<Message>> take_unprocessed_messages(u32 endpoint_magic, i32 message_id);+ // A sync request cannot get a response, as a peer that owns this process has closed the connection. The caller of+ // send_sync() has no way to carry on, so a connection whose die() returns has to end the process here.+ virtual void did_lose_peer_during_sync_request() { shutdown(); }+// Dispatch every message the peer has already sent on this connection, without waiting for new ones.void dispatch_pending_messages();@@ -108,6 +113,7 @@ protected:u32 m_local_endpoint_magic { 0 };bool m_peer_owns_this_process { false };+ bool m_is_waiting_for_sync_response { false };bool m_died { false };};