Skip to content

Commit

Permalink
fix(desktop): work around memory leak caused by tauris invoke api in …
Browse files Browse the repository at this point in the history
…combination with larger payloads
  • Loading branch information
pascalbreuninger committed Feb 8, 2024
1 parent b07fa85 commit 4f1c45e
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 18 deletions.
18 changes: 18 additions & 0 deletions desktop/src-tauri/Cargo.lock

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

4 changes: 3 additions & 1 deletion desktop/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ tauri = { version = "1.2.4", features = [
"window-set-focus",
"window-start-dragging",
"icon-ico",
] }
] }
# Serde
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
Expand Down Expand Up @@ -62,6 +62,8 @@ ts-rs = { version = "6.2.1", features = ["serde-compat", "chrono-impl"] }
semver = "1.0.18"
strip-ansi-escapes = "0.1.1"
axum = { version = "0.7.1", features = ["ws"] }
tower-http = { version = "0.5.1", features = ["cors"] }
http = "1.0.0"

[target.'cfg(target_os = "windows")'.dependencies]
winreg = "0.50.0"
Expand Down
1 change: 0 additions & 1 deletion desktop/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ fn main() -> anyhow::Result<()> {
action_logs::sync_action_logs,
install_cli::install_cli,
community_contributions::get_contributions,
updates::get_releases,
updates::get_pending_update,
updates::check_updates
]);
Expand Down
40 changes: 32 additions & 8 deletions desktop/src-tauri/src/server.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
use crate::{custom_protocol, ui_messages, AppHandle, AppState};
use crate::{ui_messages, AppHandle, AppState};
use axum::{
extract::{
connect_info::ConnectInfo,
ws::{Message, WebSocket, WebSocketUpgrade},
},
http::HeaderMap,
response::Response,
http::{HeaderMap, StatusCode},
response::{IntoResponse, Response},
routing::get,
Router, ServiceExt,
Json, Router,
};
use http::Method;
use log::{info, warn};
use std::net::SocketAddr;
use tauri::{Manager, State};
use tower_http::cors::{Any, CorsLayer};

pub async fn setup(app_handle: &AppHandle) -> anyhow::Result<()> {
let handle = app_handle.clone();
let handle_releases = app_handle.clone();
let cors = CorsLayer::new()
.allow_methods([Method::GET, Method::POST])
.allow_origin(Any);

let router = Router::new().route(
"/ws",
get(move |upgrade, headers, info| ws_handler(upgrade, headers, info, handle.clone())),
);
let router = Router::new()
.route(
"/ws",
get(move |upgrade, headers, info| ws_handler(upgrade, headers, info, handle.clone())),
)
.route("/releases", get(move || releases_handler(handle_releases)))
.layer(cors);

let listener = tokio::net::TcpListener::bind("127.0.0.1:25842").await?;
info!("Listening on {}", listener.local_addr()?);
Expand All @@ -30,6 +39,21 @@ pub async fn setup(app_handle: &AppHandle) -> anyhow::Result<()> {
.await
.map_err(anyhow::Error::from);
}
async fn releases_handler(app_handle: AppHandle) -> impl IntoResponse {
#[cfg(feature = "enable-updater")]
{
let state = app_handle.state::<AppState>();
let releases = state.releases.lock().unwrap();
let releases = releases.clone();

Json(releases)
}

#[cfg(not(feature = "enable-updater"))]
{
(StatusCode::NOT_FOUND, "Not found")
}
}

async fn ws_handler(
ws: WebSocketUpgrade,
Expand Down
7 changes: 0 additions & 7 deletions desktop/src-tauri/src/updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,6 @@ pub struct Author {
pub site_admin: bool,
}

#[tauri::command]
pub async fn get_releases(state: tauri::State<'_, AppState>) -> Result<Releases, ()> {
let releases = state.releases.lock().unwrap();

Ok(releases.to_vec())
}

#[tauri::command]
pub async fn get_pending_update(state: tauri::State<'_, AppState>) -> Result<Release, ()> {
let release = state.pending_update.lock().unwrap();
Expand Down
8 changes: 7 additions & 1 deletion desktop/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,13 @@ class Client {

public async fetchReleases(): Promise<Result<readonly Release[]>> {
try {
const releases = await invoke<readonly Release[]>("get_releases")
// WARN: This is a workaround for a memory leak in tauri, see https://github.com/tauri-apps/tauri/issues/4026 for more details.
// tl;dr tauri doesn't release the memory in it's invoke api properly which is specially noticeable with larger payload, like the releases.
const res = await fetch("http://localhost:25842/releases")
if (!res.ok) {
return Return.Failed(`Fetch releases: ${res.statusText}`)
}
const releases = (await res.json()) as readonly Release[]

return Return.Value(releases)
} catch (e) {
Expand Down

0 comments on commit 4f1c45e

Please sign in to comment.