ipc-channel
ipc-channel copied to clipboard
Add mechanism to recover windows pipe handles
This PR resolves the following issues: #310 and #277, it also makes it possible to run servo under multiprocess mode on windows (without sandboxing).
This pull request has undergone multiple iterations, including implementations that utilized anonymous pipes, Advanced Local Procedure Call (ALPC), and a global server responsible for tracking pipe handles across all processes. Each of these approaches had several drawbacks compared to the final implementation presented here. The final implementation loosely follows a the advice todo from a previous contributor.
A generic named pipe creation interface function has been introduced to allow the sender and receiver to be instantiated in any order. Prior to this PR, the receiver needed to be created before the sender, which led to various issues when duplicating handles across processes. As a consequence of this change, both endpoints are now marked as duplex, even if bidirectional communication is not utilized. In the future, this functionality could be leveraged to enhance the efficiency of the Windows implementation. Specifically, a check could be performed to determine the existence of a handle, enabling the process to use it as either a sender or receiver as needed.
Additionally, a recover_handles method has been added to the OutOfBandMessage struct to prevent a panic when an out-of-band message is received from an incorrect handle. If the target process ID (PID) and the current PID differ, this method is invoked to duplicate the target handle into the current process. Since Windows does not allow direct handle transfer between processes, duplication is used as the standard approach. This method applies to channel handles, shared memory handles, and large data receiver handles. It is essential for IPC scenarios where handle transfers are required, ensuring that the current process always has a valid handle.
A set of tests are also included in this PR. More coverage could be added in the future.
Here is a showcase of servo running with this PR in Multiprocess mode:
https://github.com/user-attachments/assets/d2ff20ae-c38c-4df3-8904-7c6d91db0d09