Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

surface: Don't crash due to weird state after resume #699

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/backend/kms/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,13 +325,13 @@ impl State {
let backend = self.backend.kms();
backend.libinput.suspend();
for device in backend.drm_devices.values_mut() {
device.drm.pause();
if let Some(lease_state) = device.leasing_global.as_mut() {
lease_state.suspend();
}
for surface in device.surfaces.values_mut() {
surface.suspend();
let _ = surface.suspend();
}
device.drm.pause();
}
}
}
Expand Down
41 changes: 35 additions & 6 deletions src/backend/kms/surface/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ impl Default for QueueState {

#[derive(Debug)]
pub enum ThreadCommand {
Suspend,
Suspend {
result: SyncSender<Result<()>>,
},
Resume {
surface: DrmSurface,
gbm: GbmDevice<DrmDeviceFd>,
Expand Down Expand Up @@ -389,8 +391,12 @@ impl Surface {
rx.recv().context("Surface thread died")?
}

pub fn suspend(&mut self) {
let _ = self.thread_command.send(ThreadCommand::Suspend);
pub fn suspend(&mut self) -> Result<()> {
let (tx, rx) = std::sync::mpsc::sync_channel(1);
let _ = self
.thread_command
.send(ThreadCommand::Suspend { result: tx });
rx.recv().context("Surface thread died")?
}

pub fn resume(
Expand Down Expand Up @@ -496,7 +502,9 @@ fn surface_thread(
event_loop
.handle()
.insert_source(thread_receiver, move |command, _, state| match command {
Event::Msg(ThreadCommand::Suspend) => state.suspend(),
Event::Msg(ThreadCommand::Suspend { result }) => {
let _ = result.send(state.suspend());
}
Event::Msg(ThreadCommand::Resume {
surface,
gbm,
Expand Down Expand Up @@ -546,7 +554,7 @@ fn surface_thread(
}

impl SurfaceThreadState {
fn suspend(&mut self) {
fn suspend(&mut self) -> Result<()> {
self.active.store(false, Ordering::SeqCst);
let _ = self.compositor.take();

Expand All @@ -564,6 +572,8 @@ impl SurfaceThreadState {
self.loop_handle.remove(queued_render);
}
};

Ok(())
}

fn resume(
Expand Down Expand Up @@ -673,8 +683,27 @@ impl SurfaceThreadState {
let Some(compositor) = self.compositor.as_mut() else {
return;
};

// handle edge-cases right after resume
if !matches!(
self.state,
QueueState::WaitingForVBlank { .. } | QueueState::Idle
) {
match mem::replace(&mut self.state, QueueState::Idle) {
QueueState::WaitingForVBlank { .. } | QueueState::Idle => unreachable!(),
QueueState::Queued(token) | QueueState::WaitingForEstimatedVBlank(token) => {
self.loop_handle.remove(token);
}
QueueState::WaitingForEstimatedVBlankAndQueued {
estimated_vblank,
queued_render,
} => {
self.loop_handle.remove(estimated_vblank);
self.loop_handle.remove(queued_render);
}
}
}
if matches!(self.state, QueueState::Idle) {
// can happen right after resume
return;
}

Expand Down
Loading