Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(updater): run cleanup before exit on Windows #1070

Merged
merged 8 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
5 changes: 5 additions & 0 deletions .changes/fix-updater-cleanup-md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"updater": patch
---

Add a `on_before_exit` hook for cleanup before spawning the updater on Windows, defaults to `app.cleanup_before_exit` when used through `UpdaterExt`
10 changes: 7 additions & 3 deletions plugins/updater/permissions/schemas/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,14 @@
},
"platforms": {
"description": "Target platforms this permission applies. By default all platforms are affected by this permission.",
"type": [
"array",
"null"
"default": [
"linux",
"macOS",
"windows",
"android",
"iOS"
],
"type": "array",
"items": {
"$ref": "#/definitions/Target"
}
Expand Down
5 changes: 5 additions & 0 deletions plugins/updater/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ impl<R: Runtime, T: Manager<R>> UpdaterExt<R> for T {
}
}

let app_handle = app.app_handle().clone();
builder = builder.on_before_exit(move || {
app_handle.cleanup_before_exit();
});

builder
}

Expand Down
28 changes: 22 additions & 6 deletions plugins/updater/src/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{
io::{Cursor, Read},
path::{Path, PathBuf},
str::FromStr,
sync::Arc,
time::Duration,
};

Expand Down Expand Up @@ -88,6 +89,8 @@ impl RemoteRelease {
}
}

pub type OnBeforeExit = Arc<dyn Fn() + Send + Sync + 'static>;

pub struct UpdaterBuilder {
current_version: Version,
config: Config,
Expand All @@ -99,6 +102,7 @@ pub struct UpdaterBuilder {
timeout: Option<Duration>,
proxy: Option<Url>,
installer_args: Vec<OsString>,
on_before_exit: Option<OnBeforeExit>,
}

impl UpdaterBuilder {
Expand All @@ -118,6 +122,7 @@ impl UpdaterBuilder {
headers: Default::default(),
timeout: None,
proxy: None,
on_before_exit: None,
}
}

Expand Down Expand Up @@ -197,6 +202,11 @@ impl UpdaterBuilder {
self
}

pub fn on_before_exit<F: Fn() + Send + Sync + 'static>(mut self, f: F) -> Self {
self.on_before_exit.replace(Arc::new(f));
self
}

pub fn build(self) -> Result<Updater> {
let endpoints = self
.endpoints
Expand Down Expand Up @@ -236,6 +246,7 @@ impl UpdaterBuilder {
json_target,
headers: self.headers,
extract_path,
on_before_exit: self.on_before_exit,
})
}
}
Expand All @@ -256,6 +267,7 @@ pub struct Updater {
json_target: String,
headers: HeaderMap,
extract_path: PathBuf,
on_before_exit: Option<OnBeforeExit>,
}

impl Updater {
Expand Down Expand Up @@ -354,6 +366,7 @@ impl Updater {
let update = if should_update {
Some(Update {
config: self.config.clone(),
on_before_exit: self.on_before_exit.clone(),
current_version: self.current_version.to_string(),
target: self.target.clone(),
extract_path: self.extract_path.clone(),
Expand All @@ -375,9 +388,10 @@ impl Updater {
}
}

#[derive(Debug, Clone)]
#[derive(Clone)]
pub struct Update {
config: Config,
on_before_exit: Option<OnBeforeExit>,
amrbashir marked this conversation as resolved.
Show resolved Hide resolved
/// Update description
pub body: Option<String>,
/// Version used to check for update
Expand Down Expand Up @@ -490,7 +504,7 @@ impl Update {
}

#[cfg(mobile)]
fn install_inner(&self, bytes: Vec<u8>) -> Result<()> {
fn install_inner(&self, _bytes: Vec<u8>) -> Result<()> {
Ok(())
}

Expand Down Expand Up @@ -557,9 +571,13 @@ impl Update {
continue;
}

if let Some(on_before_exit) = self.on_before_exit.as_ref() {
on_before_exit();
}

let file = encode_wide(found_path.as_os_str());
let parameters = encode_wide(installer_args.join(OsStr::new(" ")).as_os_str());
let ret = unsafe {
unsafe {
ShellExecuteW(
0,
w!("open"),
Expand All @@ -569,9 +587,7 @@ impl Update {
SW_SHOW,
)
};
if ret <= 32 {
return Err(Error::Io(std::io::Error::last_os_error()));
}

std::process::exit(0);
}

Expand Down
Loading