fix: add frame.finish() to flush GL commands, fix IPC write ordering

Two critical fixes:

1. **Flickering (GL frame not finalized)**: render_frame was missing
   the frame.finish() call that nested.rs has (line 688). Without it,
   GL rendering commands are never flushed to the GBM BO, so the DRM
   page flip scans out a buffer with stale/incomplete pixel data,
   alternating between rendered and unrendered content every frame.

2. **IPC protocol corruption (EPIPE/ECONNRESET)**: Node's socket.write()
   is async and buffers data in userspace. The native sendFd() call
   (sendmsg with SCM_RIGHTS) goes directly to the kernel, racing ahead
   of the buffered header+plane bytes. The compositor then reads the
   1-byte SCM_RIGHTS dummy as the next message magic, hits 'unknown
   magic', and the IPC reader thread exits — closing the socket (EPIPE).
   Fix: use fs.writeSync() on the raw fd for header+plane data so it
   reaches the kernel buffer before sendFd's sendmsg.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Stephen Hellings 2026-03-09 14:31:47 -04:00
parent 6c487d6b75
commit e245af128c
4 changed files with 1823 additions and 11 deletions

1782
electron/dist/main.js vendored Normal file

File diff suppressed because it is too large Load diff

View file

@ -1,4 +1,5 @@
import { Socket } from 'net';
import { writeSync } from 'fs';
import { MAGIC } from './protocol.js';
@ -181,8 +182,14 @@ export class WoDmabufSender {
plane.writeUInt32LE(imported.modifier_hi, 8);
plane.writeUInt32LE(imported.modifier_lo, 12);
this.socket.write(header);
this.socket.write(plane);
// CRITICAL: We must use synchronous, unbuffered writes to the raw
// socket fd so that the header+plane bytes are in the kernel buffer
// BEFORE sendFd's sendmsg() call. Node's socket.write() is async
// and buffers in userspace — sendFd (a native sendmsg) would race
// ahead, delivering the SCM_RIGHTS ancillary data before the header
// bytes, corrupting the protocol framing on the compositor side.
const combined = Buffer.concat([header, plane]);
writeSync(socketFd, combined, 0, combined.length);
this.native.sendFd(socketFd, imported.fd);
const sentSeq = this.seq;

View file

@ -832,7 +832,7 @@ fn handle_connection(
match msg {
ElectronMessage::Frame(frame) => {
let seq = frame.seq;
debug!(window = %name, seq, "sending FRAME_ACK");
info!(window = %name, seq, "received FRAME, sending ACK");
if let Err(e) = client.write_frame_ack(seq) {
error!(window = %name, seq, "failed to send FRAME_ACK: {e:#}");
}

View file

@ -93,10 +93,13 @@ struct EventLoopData {
electron_render_node: String,
/// Total successful page flips (for startup diagnostics).
total_flips: u64,
/// Hold the currently-displayed swapchain slot so the GBM BO is not
/// recycled while the CRTC is still scanning it out. Released when
/// the next frame is submitted.
/// Hold the buffer the CRTC is currently scanning out so its GBM BO is
/// not recycled. Promoted from `pending_slot` on VBlank.
current_slot: Option<Slot<smithay::backend::allocator::gbm::GbmBuffer>>,
/// Hold the buffer submitted for the next page flip, waiting for VBlank
/// confirmation. Without this, the CRTC's active scanout buffer could be
/// freed prematurely, causing every-other-frame flicker.
pending_slot: Option<Slot<smithay::backend::allocator::gbm::GbmBuffer>>,
}
const MAX_CONSECUTIVE_FLIP_FAILURES: u32 = 5;
@ -335,6 +338,7 @@ fn render_frame(data: &mut EventLoopData) {
);
data.vblank_pending = false;
data.vblank_since = None;
data.current_slot = data.pending_slot.take();
} else {
return;
}
@ -544,6 +548,21 @@ fn render_frame(data: &mut EventLoopData) {
);
}
// Finalize the GL frame — flushes all rendering commands to the
// framebuffer. Without this, the GBM BO may contain stale or
// incomplete pixel data, causing flicker.
match frame.finish() {
Ok(_) => {}
Err(e) => {
warn!("Failed to finish frame: {e}");
if is_egl_context_or_surface_lost(&e.to_string()) {
error!("EGL context/surface lost while finishing frame");
context_lost = true;
return false;
}
}
}
true
})();
@ -617,9 +636,9 @@ fn render_frame(data: &mut EventLoopData) {
data.vblank_pending = true;
data.vblank_since = Some(Instant::now());
data.swapchain.submitted(&slot);
// Hold the slot so its GBM BO is not recycled while the CRTC
// scans it out. The previous slot (if any) is released here.
data.current_slot = Some(slot);
// Hold the slot as pending until VBlank confirms the flip.
// current_slot (the active scanout buffer) stays alive until then.
data.pending_slot = Some(slot);
data.total_flips += 1;
if data.total_flips == 1 {
info!("First successful page flip (commit_pending={})", commit_pending);
@ -670,7 +689,7 @@ fn render_frame(data: &mut EventLoopData) {
data.vblank_pending = true;
data.vblank_since = Some(Instant::now());
data.swapchain.submitted(&slot);
data.current_slot = Some(slot);
data.pending_slot = Some(slot);
data.total_flips += 1;
}
}
@ -1188,6 +1207,7 @@ fn run_drm(mut config: Config) -> Result<(), anyhow::Error> {
electron_render_node: electron_render_node,
total_flips: 0,
current_slot: None,
pending_slot: None,
};
let signal_for_handler = loop_signal.clone();
@ -1205,14 +1225,17 @@ fn run_drm(mut config: Config) -> Result<(), anyhow::Error> {
if let DrmEvent::VBlank(_) = event {
data.vblank_pending = false;
data.vblank_since = None;
data.current_slot = data.pending_slot.take();
}
trace!("Ignoring DRM event while session is inactive");
return;
}
if let DrmEvent::VBlank(_) = event {
// The screen just refreshed! We are clear to render the next frame.
// The flip is complete: the pending buffer is now being scanned.
// Promote it to current (releasing the old scanout buffer).
data.vblank_pending = false;
data.vblank_since = None;
data.current_slot = data.pending_slot.take();
render_frame(data);
}
})