Retrieve app version from crash rather than from current binary

The crash might have been generated weeks before and the app may
have been updated since then.
This commit is contained in:
Antonio Scandurra 2022-05-02 15:34:05 +02:00
parent 09a8b8e675
commit 54a45095cd
2 changed files with 45 additions and 27 deletions

View file

@ -137,7 +137,7 @@ struct Crash {
text: String, text: String,
} }
#[instrument] #[instrument(skip(crash))]
async fn trace_crash(crash: Json<Crash>) -> Result<()> { async fn trace_crash(crash: Json<Crash>) -> Result<()> {
tracing::error!(version = %crash.version, text = %crash.text, "crash report"); tracing::error!(version = %crash.version, text = %crash.text, "crash report");
Ok(()) Ok(())

View file

@ -265,36 +265,53 @@ fn init_crash_handler(
background background
.spawn({ .spawn({
let logs_dir_path = logs_dir_path.clone(); let logs_dir_path = logs_dir_path.clone();
let app_version = ZED_APP_VERSION.map_or("dev".to_string(), |v| v.to_string());
async move { async move {
let crash_report_url = format!("{}/api/crash", &*client::ZED_SERVER_URL); let crash_report_url = format!("{}/api/crash", &*client::ZED_SERVER_URL);
let mut children = smol::fs::read_dir(&logs_dir_path).await?; let mut children = smol::fs::read_dir(&logs_dir_path).await?;
while let Some(child) = children.next().await { while let Some(child) = children.next().await {
let child = child?; let child = child?;
let child_path = child.path(); let child_path = child.path();
if child_path.extension() == Some(OsStr::new("crash")) { if child_path.extension() != Some(OsStr::new("crash")) {
let text = smol::fs::read_to_string(&child_path) continue;
.await }
.context("error reading crash file")?; let filename = if let Some(filename) = child_path.file_name() {
let body = serde_json::to_string(&json!({ filename.to_string_lossy()
"text": text, } else {
"version": app_version continue;
})) };
.unwrap();
let request = Request::builder() let mut components = filename.split('-');
.uri(&crash_report_url) if components.next() != Some("zed") {
.method(http::Method::POST) continue;
.redirect_policy(isahc::config::RedirectPolicy::Follow) }
.header("Content-Type", "application/json") let version = if let Some(version) = components.next() {
.body(AsyncBody::from(body))?; version
let response = http.send(request).await.context("error sending crash")?; } else {
if response.status().is_success() { continue;
fs::remove_file(child_path) };
.context("error removing crash after sending it successfully")
.log_err(); let text = smol::fs::read_to_string(&child_path)
} else { .await
log::error!("{:?}", response); .context("error reading crash file")?;
} let body = serde_json::to_string(&json!({
"text": text,
"version": version
}))
.unwrap();
let request = Request::builder()
.uri(&crash_report_url)
.method(http::Method::POST)
.redirect_policy(isahc::config::RedirectPolicy::Follow)
.header("Content-Type", "application/json")
.body(AsyncBody::from(body))?;
let response = http.send(request).await.context("error sending crash")?;
if response.status().is_success() {
fs::remove_file(child_path)
.context("error removing crash after sending it successfully")
.log_err();
} else {
log::error!("{:?}", response);
} }
} }
Ok::<_, anyhow::Error>(()) Ok::<_, anyhow::Error>(())
@ -303,6 +320,7 @@ fn init_crash_handler(
}) })
.detach(); .detach();
let app_version = ZED_APP_VERSION.map_or("dev".to_string(), |v| v.to_string());
let is_pty = stdout_is_a_pty(); let is_pty = stdout_is_a_pty();
panic::set_hook(Box::new(move |info| { panic::set_hook(Box::new(move |info| {
let backtrace = Backtrace::new(); let backtrace = Backtrace::new();
@ -335,9 +353,9 @@ fn init_crash_handler(
), ),
}; };
let crash_filename = chrono::Utc::now().format("%Y-%m-%d-%H_%M_%S").to_string(); let crash_filename = chrono::Utc::now().format("%Y_%m_%d %H_%M_%S").to_string();
fs::write( fs::write(
logs_dir_path.join(format!("zed-{}.crash", crash_filename)), logs_dir_path.join(format!("zed-{}-{}.crash", app_version, crash_filename)),
&message, &message,
) )
.context("error writing panic to disk") .context("error writing panic to disk")