LibIPC+LibWeb: Flush MessagePort messages before closing

The spec isn't super clear on what disentagling a MessagePort means. But
we are required to send all pending messages before closing the port.

This is a bit tricky because the transport socket performs writes on a
background thread. From the main thread, where the disentanglement will
occur, we don't really know the state of the write thread. So what we do
here is stop the background thread then flush all remaining data from
the main thread.
This commit is contained in:
Timothy Flynn 2025-05-20 16:21:17 -04:00 committed by Tim Flynn
commit 36da270dbe
Notes: github-actions[bot] 2025-05-21 10:56:21 +00:00
3 changed files with 85 additions and 36 deletions

View file

@ -73,7 +73,9 @@ public:
void set_up_read_hook(Function<void()>);
bool is_open() const;
void close();
void close_after_sending_all_pending_messages();
void wait_until_readable();
@ -95,8 +97,16 @@ public:
ErrorOr<IPC::File> clone_for_transfer();
private:
enum class TransferState {
Continue,
SocketClosed,
};
[[nodiscard]] TransferState transfer_data(ReadonlyBytes& bytes, Vector<int>& fds);
static ErrorOr<void> send_message(Core::LocalSocket&, ReadonlyBytes& bytes, Vector<int>& unowned_fds);
void stop_send_thread();
NonnullOwnPtr<Core::LocalSocket> m_socket;
mutable Threading::RWLock m_socket_rw_lock;
ByteBuffer m_unprocessed_bytes;