mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-24 18:02:20 +00:00
LibIPC: Move send thread from IPC connection to the transport layer
By doing this we also make MessagePort, that relies on IPC transport, to send messages from separate thread, which solves the problem when WebWorker and WebContent could deadlock if both were trying to post messages at the same time. Fixes https://github.com/LadybirdBrowser/ladybird/issues/4254
This commit is contained in:
parent
af2dae63d1
commit
14bac7b287
Notes:
github-actions[bot]
2025-04-08 19:10:29 +00:00
Author: https://github.com/kalenikaliaksandr
Commit: 14bac7b287
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4276
Reviewed-by: https://github.com/ADKaster
6 changed files with 118 additions and 111 deletions
|
@ -8,11 +8,40 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/Queue.h>
|
||||
#include <LibCore/Socket.h>
|
||||
#include <LibIPC/UnprocessedFileDescriptors.h>
|
||||
#include <LibThreading/ConditionVariable.h>
|
||||
#include <LibThreading/MutexProtected.h>
|
||||
#include <LibThreading/Thread.h>
|
||||
|
||||
namespace IPC {
|
||||
|
||||
class AutoCloseFileDescriptor : public RefCounted<AutoCloseFileDescriptor> {
|
||||
public:
|
||||
AutoCloseFileDescriptor(int fd)
|
||||
: m_fd(fd)
|
||||
{
|
||||
}
|
||||
|
||||
~AutoCloseFileDescriptor()
|
||||
{
|
||||
if (m_fd != -1)
|
||||
(void)Core::System::close(m_fd);
|
||||
}
|
||||
|
||||
int value() const { return m_fd; }
|
||||
|
||||
int take_fd()
|
||||
{
|
||||
int fd = m_fd;
|
||||
m_fd = -1;
|
||||
return fd;
|
||||
}
|
||||
|
||||
private:
|
||||
int m_fd;
|
||||
};
|
||||
|
||||
class TransportSocket {
|
||||
AK_MAKE_NONCOPYABLE(TransportSocket);
|
||||
AK_MAKE_DEFAULT_MOVABLE(TransportSocket);
|
||||
|
@ -29,7 +58,7 @@ public:
|
|||
|
||||
void wait_until_readable();
|
||||
|
||||
ErrorOr<void> transfer_message(ReadonlyBytes, Vector<int, 1> const& unowned_fds);
|
||||
void post_message(Vector<u8> const&, Vector<NonnullRefPtr<AutoCloseFileDescriptor>> const&) const;
|
||||
|
||||
enum class ShouldShutdown {
|
||||
No,
|
||||
|
@ -47,17 +76,30 @@ public:
|
|||
ErrorOr<IPC::File> clone_for_transfer();
|
||||
|
||||
private:
|
||||
ErrorOr<void> transfer(ReadonlyBytes, Vector<int, 1> const& unowned_fds);
|
||||
static ErrorOr<void> send_message(Core::LocalSocket&, ReadonlyBytes&&, Vector<int, 1> const& unowned_fds);
|
||||
|
||||
NonnullOwnPtr<Core::LocalSocket> m_socket;
|
||||
NonnullOwnPtr<Threading::Mutex> m_socket_write_mutex;
|
||||
ByteBuffer m_unprocessed_bytes;
|
||||
UnprocessedFileDescriptors m_unprocessed_fds;
|
||||
|
||||
// After file descriptor is sent, it is moved to the wait queue until an acknowledgement is received from the peer.
|
||||
// This is necessary to handle a specific behavior of the macOS kernel, which may prematurely garbage-collect the file
|
||||
// descriptor contained in the message before the peer receives it. https://openradar.me/9477351
|
||||
NonnullOwnPtr<Threading::MutexProtected<Queue<File>>> m_fds_retained_until_received_by_peer;
|
||||
NonnullOwnPtr<Queue<NonnullRefPtr<AutoCloseFileDescriptor>>> m_fds_retained_until_received_by_peer;
|
||||
|
||||
struct MessageToSend {
|
||||
Vector<u8> bytes;
|
||||
Vector<int, 1> fds;
|
||||
};
|
||||
struct SendQueue : public AtomicRefCounted<SendQueue> {
|
||||
AK::SinglyLinkedList<MessageToSend> messages;
|
||||
Threading::Mutex mutex;
|
||||
Threading::ConditionVariable condition { mutex };
|
||||
bool running { true };
|
||||
};
|
||||
RefPtr<Threading::Thread> m_send_thread;
|
||||
RefPtr<SendQueue> m_send_queue;
|
||||
void queue_message_on_send_thread(MessageToSend&&) const;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue