Open ilmai opened 1 year ago
I'm getting reports of the same issue from users, and can confirm it happens when using Vizia, Iced, and Egui, so it's not tied to Vizia specifically.
Edit: I should mention I reproduced the error using the nih-plug example plugins, and a Linux Mint (Debian Edition) VM. I've also seen it on regular Debian/Ubuntu VMs, but it was particularly quick to reproduce on the Mint (Debian Edition) one.
I've finally managed to track down the issue, and have a quick fix! It's caused by a race condition that starts in baseview::x11::WindowHandle::close()
.
Explaination of the Bug:
WindowHandle::close()
sets the close_requested
atomic so that the event loop knows to finish up, but the function returns immediately allowing windows to close before baseview
can complete rendering and process the close request. To make matters worse, this frequently happens at the start of frame rendering because activating an OpenGL context will wait for things to catch up (like vsync).
Because it's a race condition that also depends on driver quirks, every system I've tested behaves differently. Usually crashes occur upon reopening the window. Some systems crash on the first reopening, some can survive 100, it's all over the place.
A proper fix would be to synchronize with the event loop thread, as the existing comment points out... I wish I had spotted that before spending an embarrassing amount of time tracking this down. 🤦 A quick-and-dirty fix is to add a small delay when closing:
impl WindowHandle {
pub fn close(&mut self) {
if self.raw_window_handle.take().is_some() {
// FIXME: This will need to be changed from just setting an atomic to somehow
// synchronizing with the window being closed (using a synchronous channel, or
// by joining on the event loop thread).
self.close_requested.store(true, Ordering::Relaxed);
// HACK: Allow the window time to close nicely to avoid OpenGL errors/crashing
// until proper synchronizing (mentioned above) can be implemented.
thread::sleep(Duration::from_millis(200));
}
}
...
These issues are the same: https://github.com/robbert-vdh/nih-plug/issues/98 https://github.com/ardura/Subhoofer/issues/9 Possibly the same: https://github.com/robbert-vdh/nih-plug/issues/124
Tested on two systems. I get this crash inside baseview when closing the plugin window of my plugin using Vizia: