Skip to content
Merged
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
56 changes: 56 additions & 0 deletions src-tauri/src/app_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,29 @@ use crate::{
DEFAULT_SHELL_LOCALE, DESKTOP_LOG_FILE, STARTUP_MODE_ENV,
};

const WEBKIT_DISABLE_DMABUF_RENDERER_ENV: &str = "WEBKIT_DISABLE_DMABUF_RENDERER";
const WAYLAND_DISPLAY_ENV: &str = "WAYLAND_DISPLAY";

fn should_set_webkit_dmabuf_renderer_env(
existing_value: Option<&std::ffi::OsStr>,
wayland_display: Option<&std::ffi::OsStr>,
) -> bool {
existing_value.is_none() && wayland_display.is_some()
}

#[cfg(target_os = "linux")]
fn configure_linux_webkit_workarounds() {
if should_set_webkit_dmabuf_renderer_env(
std::env::var_os(WEBKIT_DISABLE_DMABUF_RENDERER_ENV).as_deref(),
std::env::var_os(WAYLAND_DISPLAY_ENV).as_deref(),
) {
std::env::set_var(WEBKIT_DISABLE_DMABUF_RENDERER_ENV, "1");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Starting in Rust 1.81.0 (and enforced in the Rust 2024 edition), std::env::set_var is marked as unsafe because modifying environment variables in a multi-threaded environment can lead to data races and undefined behavior.

To ensure compatibility with newer Rust editions and prevent compilation errors or deprecation warnings, wrap the call in an unsafe block. Since this function is called at the very beginning of main before any other threads are spawned, it is safe to do so.

        // SAFETY: This is called at the very beginning of the application's execution
        // before any other threads are spawned, ensuring no data races occur.
        unsafe {
            std::env::set_var(WEBKIT_DISABLE_DMABUF_RENDERER_ENV, "1");
        }

append_startup_log(&format!(
"applied Linux WebKit workaround: set {WEBKIT_DISABLE_DMABUF_RENDERER_ENV}=1"
));
}
}

Comment thread
sourcery-ai[bot] marked this conversation as resolved.
fn configure_plugins(builder: Builder<tauri::Wry>) -> Builder<tauri::Wry> {
builder
.plugin(tauri_plugin_autostart::init(
Expand Down Expand Up @@ -168,6 +191,9 @@ fn handle_run_event(app_handle: &tauri::AppHandle, event: RunEvent) {
}

pub(crate) fn run() {
#[cfg(target_os = "linux")]
configure_linux_webkit_workarounds();

append_startup_log("desktop process starting");
append_startup_log(&format!(
"desktop log path: {}",
Expand Down Expand Up @@ -207,3 +233,33 @@ pub(crate) fn run() {
.expect("error while building tauri application")
.run(handle_run_event);
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn webkit_dmabuf_workaround_is_set_only_for_wayland_without_override() {
assert!(should_set_webkit_dmabuf_renderer_env(
None,
Some(std::ffi::OsStr::new("wayland-0"))
));
assert!(!should_set_webkit_dmabuf_renderer_env(None, None));
assert!(!should_set_webkit_dmabuf_renderer_env(
Some(std::ffi::OsStr::new("0")),
Some(std::ffi::OsStr::new("wayland-0"))
));
assert!(!should_set_webkit_dmabuf_renderer_env(
Some(std::ffi::OsStr::new("1")),
Some(std::ffi::OsStr::new("wayland-0"))
));
assert!(!should_set_webkit_dmabuf_renderer_env(
Some(std::ffi::OsStr::new("0")),
None
));
assert!(!should_set_webkit_dmabuf_renderer_env(
Some(std::ffi::OsStr::new("1")),
None
));
}
}