Skip to content
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
21 changes: 21 additions & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ bytes = "1"
cuid2 = "0.1"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tracing-appender = "0.2"
regex = "1"
ts-rs = { version = "12", features = ["serde-compat"] }
ndarray = "0.15"
# CLAP plugin hosting. clack is git-only and pre-1.0; pin an exact rev because
Expand Down
14 changes: 14 additions & 0 deletions src-tauri/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ pub fn clear_logs() {
crate::logs::clear();
}

/// Webview errors, so the session log on disk shows both sides of a crash.
#[tauri::command]
pub fn log_frontend(message: String) {
error!(target: "webview", "{message}");
}

/// Dev-only: panics on the main thread to exercise the crash-persistence path
/// (a real panic, not a faked event). Crashes the app on purpose.
#[tauri::command]
Expand Down Expand Up @@ -220,6 +226,14 @@ pub fn debug_unexpected_exit() {
std::process::exit(86);
}

/// Dev-only: blocks the main thread to exercise the hang watchdog. Kill the app
/// while it is stuck to get the next-launch hang report.
#[tauri::command]
pub fn debug_hang() {
#[cfg(debug_assertions)]
std::thread::sleep(Duration::from_secs(20));
}

#[tauri::command]
pub async fn close_plugin_editor(node_id: String) -> AppResult<()> {
let id = node_id.clone();
Expand Down
67 changes: 57 additions & 10 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ mod error;
mod logs;
mod native_crash;
mod state;
mod watchdog;

use std::path::PathBuf;
use std::sync::OnceLock;
use std::sync::{Mutex, OnceLock};

use serde_json::json;
use state::AppState;
Expand All @@ -25,6 +26,8 @@ static APP_HANDLE: OnceLock<AppHandle> = OnceLock::new();
// next launch because a dying process cannot reliably notify the webview.
static CRASH_FILE: OnceLock<PathBuf> = OnceLock::new();
static SESSION_FILE: OnceLock<PathBuf> = OnceLock::new();
/// Tail of the log of a session that ended uncleanly, attached to its reports.
static PREVIOUS_LOGS: Mutex<Option<String>> = Mutex::new(None);

fn append_report(path: &std::path::Path, payload: &serde_json::Value) {
if let Ok(mut file) = std::fs::OpenOptions::new()
Expand All @@ -37,29 +40,34 @@ fn append_report(path: &std::path::Path, payload: &serde_json::Value) {
}
}

fn initialize_crash_reporting(dir: &std::path::Path) {
fn initialize_crash_reporting(dir: &std::path::Path, home: Option<&std::path::Path>) {
let crash_file = dir.join("crashes.jsonl");
let session_file = dir.join("running-session");
let crash_len = std::fs::metadata(&crash_file).map_or(0, |meta| meta.len());
let previous_hang = watchdog::take_previous_hang(dir);

if let Ok(baseline) = std::fs::read_to_string(&session_file) {
if let Ok(mut logs) = PREVIOUS_LOGS.lock() {
*logs = logs::previous_session_tail(dir, home);
}
let baseline = baseline.trim().parse::<u64>().unwrap_or(0);
if crash_len <= baseline {
let ts = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|duration| duration.as_millis() as u64)
.unwrap_or(0);
append_report(
&crash_file,
&json!({
let report = match previous_hang {
Some(hang) => hang_report(&hang, ts),
None => json!({
"kind": "unexpectedExit",
"message": "Splitwave did not shut down cleanly",
"backtrace": "No native stack was captured. The process may have been killed by the OS, an out-of-memory condition, or a native crash without a dump.",
"thread": "<unknown>",
"version": env!("CARGO_PKG_VERSION"),
"ts": ts,
}),
);
};
append_report(&crash_file, &report);
}
}

Expand All @@ -72,10 +80,37 @@ fn initialize_crash_reporting(dir: &std::path::Path) {
}
}

fn hang_report(hang: &serde_json::Value, ts: u64) -> serde_json::Value {
let seconds = hang["stalledMs"].as_u64().unwrap_or(0) / 1000;
let place = match hang["command"].as_str() {
Some(command) => format!("in the `{command}` command"),
None => "outside any IPC command".to_string(),
};
json!({
"kind": "hang",
"message": "Splitwave stopped responding and was closed",
"backtrace": format!("The main thread was blocked for at least {seconds} s {place} before the app was closed. A hang captures no stack; the session log shows what led up to it."),
"thread": "main",
"version": env!("CARGO_PKG_VERSION"),
"ts": hang["ts"].as_u64().unwrap_or(ts),
})
}

fn with_watchdog(
handler: impl Fn(tauri::ipc::Invoke) -> bool + Send + Sync + 'static,
) -> impl Fn(tauri::ipc::Invoke) -> bool + Send + Sync + 'static {
move |invoke| {
let _command = watchdog::enter_command(invoke.message.command());
handler(invoke)
}
}

fn mark_clean_exit() {
if let Some(path) = SESSION_FILE.get() {
let _ = std::fs::remove_file(path);
}
watchdog::clear_marker();
logs::flush_file_sink();
}

#[cfg(target_os = "windows")]
Expand All @@ -102,10 +137,17 @@ pub fn take_crash_reports() -> Vec<serde_json::Value> {
if let Some(session) = SESSION_FILE.get() {
let _ = std::fs::write(session, "0");
}
let logs = PREVIOUS_LOGS.lock().ok().and_then(|mut l| l.take());
contents
.lines()
.filter(|l| !l.trim().is_empty())
.filter_map(|l| serde_json::from_str(l).ok())
.filter_map(|l| serde_json::from_str::<serde_json::Value>(l).ok())
.map(|mut report| {
if let (Some(logs), Some(fields)) = (&logs, report.as_object_mut()) {
fields.insert("logs".into(), json!(logs));
}
report
})
.collect()
}

Expand Down Expand Up @@ -253,7 +295,10 @@ pub fn run() {

if let Ok(dir) = handle.path().app_log_dir() {
let _ = std::fs::create_dir_all(&dir);
initialize_crash_reporting(&dir);
logs::start_file_sink(&dir);
let home = handle.path().home_dir().ok();
initialize_crash_reporting(&dir, home.as_deref());
watchdog::start(handle.clone(), &dir);
if let Some(file) = CRASH_FILE.get() {
info!(path = %file.display(), "crash log");
}
Expand Down Expand Up @@ -301,7 +346,7 @@ pub fn run() {
.plugin(tauri_plugin_os::init())
.plugin(tauri_plugin_global_shortcut::Builder::new().build())
.manage(AppState::spawn())
.invoke_handler(tauri::generate_handler![
.invoke_handler(with_watchdog(tauri::generate_handler![
commands::scan_plugins,
commands::open_plugin_editor,
commands::close_plugin_editor,
Expand All @@ -311,9 +356,11 @@ pub fn run() {
commands::take_crash_reports,
commands::get_logs,
commands::clear_logs,
commands::log_frontend,
commands::debug_panic,
commands::debug_native_crash,
commands::debug_unexpected_exit,
commands::debug_hang,
commands::list_input_devices,
commands::list_output_devices,
commands::play_cue,
Expand Down Expand Up @@ -363,7 +410,7 @@ pub fn run() {
commands::net_sender_stats,
commands::webrtc_set_identity,
commands::webrtc_session_state,
])
]))
.build(tauri::generate_context!())
.expect("error while building tauri application")
.run(|_, event| {
Expand Down
Loading
Loading