Fix dead surface hook VRAM leak (#3404)
Browse files at this commit2 changed files +24 -2
- src/handlers/compositor.rs +15 -1
- src/handlers/xdg_shell.rs +9 -1
src/handlers/compositor.rsmodified+15 -1
| @@ -498,7 +498,16 @@ impl CompositorHandler for State { | |||
| 498 | .root_surface | 498 | .root_surface |
| 499 | .retain(|k, v| k != surface && v != surface); | 499 | .retain(|k, v| k != surface && v != surface); |
| 500 | 500 | ||
| 501 | - self.niri.dmabuf_pre_commit_hook.remove(surface); | 501 | + // The object destruction order is not guaranteed to follow the logical role order. So for |
| 502 | + // example when a client disconnects unexpectedly, WlSurface::destroyed() may be called | ||
| 503 | + // before XdgShellHandler::toplevel_destroyed(). In this case, the surface will *not* have | ||
| 504 | + // the default dmabuf pre-commit hook: it will still have the toplevel pre-commit hook. | ||
| 505 | + // | ||
| 506 | + // So, this may come out empty, and then the toplevel pre-commit hook will be removed in the | ||
| 507 | + // subsequent toplevel_destroyed() call. | ||
| 508 | + if let Some(hook) = self.niri.dmabuf_pre_commit_hook.remove(surface) { | ||
| 509 | + remove_pre_commit_hook(surface, hook); | ||
| 510 | + } | ||
| 502 | } | 511 | } |
| 503 | } | 512 | } |
| 504 | 513 | ||
| @@ -517,6 +526,11 @@ delegate_shm!(State); | |||
| 517 | 526 | ||
| 518 | impl State { | 527 | impl State { |
| 519 | pub fn add_default_dmabuf_pre_commit_hook(&mut self, surface: &WlSurface) { | 528 | pub fn add_default_dmabuf_pre_commit_hook(&mut self, surface: &WlSurface) { |
| 529 | + if !surface.is_alive() { | ||
| 530 | + error!("tried to add dmabuf pre-commit hook for a dead surface"); | ||
| 531 | + return; | ||
| 532 | + } | ||
| 533 | + | ||
| 520 | let hook = add_pre_commit_hook::<Self, _>(surface, move |state, _dh, surface| { | 534 | let hook = add_pre_commit_hook::<Self, _>(surface, move |state, _dh, surface| { |
| 521 | let maybe_dmabuf = with_states(surface, |surface_data| { | 535 | let maybe_dmabuf = with_states(surface, |surface_data| { |
| 522 | surface_data | 536 | surface_data |
src/handlers/xdg_shell.rsmodified+9 -1
| @@ -863,7 +863,15 @@ impl XdgShellHandler for State { | |||
| 863 | 863 | ||
| 864 | self.niri.window_mru_ui.remove_window(id); | 864 | self.niri.window_mru_ui.remove_window(id); |
| 865 | self.niri.layout.remove_window(&window, transaction.clone()); | 865 | self.niri.layout.remove_window(&window, transaction.clone()); |
| 866 | - self.add_default_dmabuf_pre_commit_hook(surface.wl_surface()); | 866 | + |
| 867 | + let surface = surface.wl_surface(); | ||
| 868 | + // This check is necessary because implicit resource destruction is done with | ||
| 869 | + // undefined order, so the surface might get destroyed before toplevel_destroyed() is | ||
| 870 | + // called. In this case, adding the default pre-commit hook here would leak it, since the | ||
| 871 | + // place that removes it is WlSurface::destroyed(), which had already been called by now. | ||
| 872 | + if surface.is_alive() { | ||
| 873 | + self.add_default_dmabuf_pre_commit_hook(surface); | ||
| 874 | + } | ||
| 867 | 875 | ||
| 868 | // If this is the only instance, then this transaction will complete immediately, so no | 876 | // If this is the only instance, then this transaction will complete immediately, so no |
| 869 | // need to set the timer. | 877 | // need to set the timer. |