Delta is a GPUI app: it draws with Vulkan, and without linux-dmabuf Mesa's WSI presents through wl_shm, making one wl_shm_pool per swapchain image. libwayland flushes those create_pool requests with all their fds in one sendmsg (vkcube on lavapipe sends 4), but both hosts kept only the first SCM_RIGHTS fd and closed the rest. Every pool but the first had no memory, so only one swapchain image ever reached the screen: stale, frozen or flickering windows. - recvmsg keeps every fd (up to 32), in order: natively through recvmsg_fds, and in the wasm host through a new chandra:host/sys take-fd import. - A commit consumes the attached buffer. A bare commit (GPUI commits after ack_configure) used to re-show and re-release the old buffer, which the client may already be drawing its next frame into. - ARGB8888 client buffers are blended as premultiplied alpha, as wl_shm defines, in both rasterisers (translucent edges were darkened). - wl_seat.name and wl_output.scale/done are only sent to clients that bound version 2 or later (vkcube binds v1 and aborted on them). Regression test: a client flushes three pools with their fds in one message, then commits a bare frame; it fails on the old code. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DPL5hm8wb1UKxQxf8o6gpz
This commit is contained in:
14 files changed
+309
-41
No files matched your search
@@ -30,6 +30,16 @@ extern "C" fn c_recvmsg_fd(
|
||||
out_fd : FixedArray[Int],
|
||||
) -> Int = "mp_recvmsg_fd"
|
||||
|
||||
///|
|
||||
#borrow(buf, fds)
|
||||
extern "C" fn c_sendmsg_fds(
|
||||
fd : Int,
|
||||
buf : Bytes,
|
||||
len : Int,
|
||||
fds : FixedArray[Int],
|
||||
n_fds : Int,
|
||||
) -> Int = "mp_sendmsg_fds"
|
||||
|
||||
///|
|
||||
#borrow(buf)
|
||||
extern "C" fn c_sendmsg_fd(
|
||||
@@ -123,12 +133,26 @@ pub fn poll_read(fd : Int, timeout_ms : Int) -> Int {
|
||||
c_poll_read(fd, timeout_ms)
|
||||
}
|
||||
|
||||
///| Receive bytes and optional SCM_RIGHTS fd. Returns (bytes_read, received_fd).
|
||||
/// bytes_read == -1 means EAGAIN; -2 means error; 0 means EOF.
|
||||
pub fn recvmsg_fd(fd : Int, buf : FixedArray[Byte], max_len : Int) -> (Int, Int) {
|
||||
let out_fd : FixedArray[Int] = FixedArray::make(1, -1)
|
||||
///| Receive bytes and every SCM_RIGHTS fd that came with them, in order. Returns
|
||||
/// (bytes_read, fds); bytes_read == -1 means EAGAIN, -2 an error, 0 EOF.
|
||||
pub fn recvmsg_fds(fd : Int, buf : FixedArray[Byte], max_len : Int) -> (Int, Array[Int]) {
|
||||
let out_fd : FixedArray[Int] = FixedArray::make(33, -1)
|
||||
let n = c_recvmsg_fd(fd, buf, max_len, out_fd)
|
||||
(n, out_fd[0])
|
||||
let fds : Array[Int] = []
|
||||
for i = 0; i < out_fd[0]; i = i + 1 {
|
||||
fds.push(out_fd[1 + i])
|
||||
}
|
||||
(n, fds)
|
||||
}
|
||||
|
||||
///| Receive bytes and an optional SCM_RIGHTS fd. Returns (bytes_read, received_fd); any further
|
||||
/// fds are closed. bytes_read == -1 means EAGAIN; -2 means error; 0 means EOF.
|
||||
pub fn recvmsg_fd(fd : Int, buf : FixedArray[Byte], max_len : Int) -> (Int, Int) {
|
||||
let (n, fds) = recvmsg_fds(fd, buf, max_len)
|
||||
for i = 1; i < fds.length(); i = i + 1 {
|
||||
close_fd(fds[i])
|
||||
}
|
||||
(n, if fds.length() > 0 { fds[0] } else { -1 })
|
||||
}
|
||||
|
||||
///|
|
||||
@@ -136,6 +160,11 @@ pub fn sendmsg_fd(fd : Int, buf : Bytes, send_fd : Int) -> Int {
|
||||
c_sendmsg_fd(fd, buf, buf.length(), send_fd)
|
||||
}
|
||||
|
||||
///| Send `buf` with several fds in one SCM_RIGHTS message, the way libwayland flushes (tests).
|
||||
pub fn sendmsg_fds(fd : Int, buf : Bytes, fds : Array[Int]) -> Int {
|
||||
c_sendmsg_fds(fd, buf, buf.length(), FixedArray::from_array(fds), fds.length())
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn memfd_create(name : String, size : Int) -> Int {
|
||||
c_memfd_create(str_to_utf8_bytes(name), size)
|
||||
|
||||
@@ -121,7 +121,11 @@ int32_t mp_poll_read(int32_t fd, int32_t timeout_ms) {
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t mp_recvmsg_fd(int32_t fd, moonbit_bytes_t buf, int32_t max_len, int32_t *out_fd) {
|
||||
out_fd[0] = -1;
|
||||
/* out_fd[0] = number of fds received, out_fd[1..] the fds. A client flushes up to 28 fds
|
||||
(libwayland's limit) with one sendmsg, e.g. a Vulkan swapchain making a wl_shm_pool per
|
||||
image: every one of them is needed, in order. */
|
||||
int32_t out_cap = Moonbit_array_length(out_fd) - 1;
|
||||
out_fd[0] = 0;
|
||||
int32_t cap = Moonbit_array_length(buf);
|
||||
int32_t n = max_len < cap ? max_len : cap;
|
||||
if (n <= 0) return 0;
|
||||
@@ -130,7 +134,7 @@ int32_t mp_recvmsg_fd(int32_t fd, moonbit_bytes_t buf, int32_t max_len, int32_t
|
||||
iov.iov_base = buf;
|
||||
iov.iov_len = (size_t)n;
|
||||
|
||||
char cmsg_buf[CMSG_SPACE(sizeof(int) * 4)];
|
||||
char cmsg_buf[CMSG_SPACE(sizeof(int) * 32)];
|
||||
memset(cmsg_buf, 0, sizeof(cmsg_buf));
|
||||
|
||||
struct msghdr msg;
|
||||
@@ -140,7 +144,7 @@ int32_t mp_recvmsg_fd(int32_t fd, moonbit_bytes_t buf, int32_t max_len, int32_t
|
||||
msg.msg_control = cmsg_buf;
|
||||
msg.msg_controllen = sizeof(cmsg_buf);
|
||||
|
||||
ssize_t recvd = recvmsg(fd, &msg, 0);
|
||||
ssize_t recvd = recvmsg(fd, &msg, MSG_CMSG_CLOEXEC);
|
||||
if (recvd < 0) {
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK) return -1;
|
||||
return -2;
|
||||
@@ -150,9 +154,11 @@ int32_t mp_recvmsg_fd(int32_t fd, moonbit_bytes_t buf, int32_t max_len, int32_t
|
||||
if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
|
||||
int *fds = (int *)CMSG_DATA(cmsg);
|
||||
size_t num_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
|
||||
if (num_fds > 0) {
|
||||
out_fd[0] = fds[0];
|
||||
for (size_t i = 1; i < num_fds; i++) {
|
||||
for (size_t i = 0; i < num_fds; i++) {
|
||||
if (out_fd[0] < out_cap) {
|
||||
out_fd[1 + out_fd[0]] = fds[i];
|
||||
out_fd[0]++;
|
||||
} else {
|
||||
close(fds[i]);
|
||||
}
|
||||
}
|
||||
@@ -161,6 +167,30 @@ int32_t mp_recvmsg_fd(int32_t fd, moonbit_bytes_t buf, int32_t max_len, int32_t
|
||||
return (int32_t)recvd;
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t mp_sendmsg_fds(int32_t fd, moonbit_bytes_t buf, int32_t len, int32_t *fds, int32_t n_fds) {
|
||||
int32_t cap = Moonbit_array_length(buf);
|
||||
int32_t n = len < cap ? len : cap;
|
||||
if (n <= 0 || n_fds < 0 || n_fds > 28) return -1;
|
||||
struct iovec iov = { .iov_base = buf, .iov_len = (size_t)n };
|
||||
char cmsg_buf[CMSG_SPACE(sizeof(int) * 28)];
|
||||
memset(cmsg_buf, 0, sizeof(cmsg_buf));
|
||||
struct msghdr msg;
|
||||
memset(&msg, 0, sizeof(msg));
|
||||
msg.msg_iov = &iov;
|
||||
msg.msg_iovlen = 1;
|
||||
if (n_fds > 0) {
|
||||
msg.msg_control = cmsg_buf;
|
||||
msg.msg_controllen = CMSG_SPACE(sizeof(int) * n_fds);
|
||||
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
|
||||
cmsg->cmsg_level = SOL_SOCKET;
|
||||
cmsg->cmsg_type = SCM_RIGHTS;
|
||||
cmsg->cmsg_len = CMSG_LEN(sizeof(int) * n_fds);
|
||||
memcpy(CMSG_DATA(cmsg), fds, sizeof(int) * n_fds);
|
||||
}
|
||||
return (int32_t)sendmsg(fd, &msg, MSG_NOSIGNAL);
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t mp_sendmsg_fd(int32_t fd, moonbit_bytes_t buf, int32_t len, int32_t send_fd) {
|
||||
int32_t cap = Moonbit_array_length(buf);
|
||||
|
||||
@@ -83,6 +83,32 @@ pub fn recvmsg_fd(fd : Int, buf : FixedArray[Byte], max_len : Int) -> (Int, Int)
|
||||
(r.count, r.fd)
|
||||
}
|
||||
|
||||
///| Receive bytes and every SCM_RIGHTS fd that came with them, in order. Returns
|
||||
/// (bytes_read, fds); bytes_read == -1 means EAGAIN, -2 an error, 0 EOF.
|
||||
pub fn recvmsg_fds(fd : Int, buf : FixedArray[Byte], max_len : Int) -> (Int, Array[Int]) {
|
||||
let (n, first) = recvmsg_fd(fd, buf, max_len)
|
||||
let fds : Array[Int] = []
|
||||
if first >= 0 {
|
||||
fds.push(first)
|
||||
while true {
|
||||
let more = @hostsys.take_fd(fd)
|
||||
if more < 0 {
|
||||
break
|
||||
}
|
||||
fds.push(more)
|
||||
}
|
||||
}
|
||||
(n, fds)
|
||||
}
|
||||
|
||||
///| Several fds in one message are a native-only test helper.
|
||||
pub fn sendmsg_fds(fd : Int, buf : Bytes, fds : Array[Int]) -> Int {
|
||||
if fds.length() > 1 {
|
||||
abort("sendmsg_fds: one fd at most on the wasm build")
|
||||
}
|
||||
sendmsg_fd(fd, buf, if fds.length() == 1 { fds[0] } else { -1 })
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn sendmsg_fd(fd : Int, buf : Bytes, send_fd : Int) -> Int {
|
||||
@hostsys.sendmsg(fd, to_fixed(buf), send_fd)
|
||||
|
||||
Reference in new issue
Block a user