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

[bug] Store cannot save with app_handle.restart() #2256

Open
SergeFan opened this issue Jan 3, 2025 · 1 comment · May be fixed by tauri-apps/tauri#12313
Open

[bug] Store cannot save with app_handle.restart() #2256

SergeFan opened this issue Jan 3, 2025 · 1 comment · May be fixed by tauri-apps/tauri#12313
Labels
bug Something isn't working status: upstream This issue needs to be fixed in an upstream project

Comments

@SergeFan
Copy link

SergeFan commented Jan 3, 2025

Describe the bug

I have an App with Store and Process plugin activated. The store cannot be saved if I restart the App with app_handle.restart(), but it can be saved via app_handle.exit(0).

Reproduction

The code is from my project. I save the app settings with store, and then call the app_handle.restart().

#[derive(Serialize, Deserialize)]
pub struct AppSettings {
    language: String,
    direct_generation: bool,
}

#[tauri::command]
pub fn get_app_settings(app_handle: AppHandle) -> AppSettings {
    let store = app_handle.store("store.json").unwrap();

    let language = store
        .get("language")
        .expect("No language setting is found.");
    let direct_generation = store
        .get("direct_generation")
        .expect("No direct generation setting is found.");

    AppSettings {
        language: language.get("value").unwrap().as_str().unwrap().to_string(),
        direct_generation: direct_generation.get("value").unwrap().as_bool().unwrap(),
    }
}

#[tauri::command]
pub fn set_app_settings(app_handle: AppHandle, language: &str, direct_generation: bool) {
    let store = app_handle.store("store.json").unwrap();

    store.set("language", json!({"value": language}));
    store.set("direct_generation", json!({"value": direct_generation}));

    // This won't save the store, after restart it still shows the old setting values
    app_handle.restart();

    // This works, it will save the store and after I restart the app manually it shows the new setting values
    // app_handle.exit(0);
}

I have those plugins activated.

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    tauri::Builder::default()
        .plugin(tauri_plugin_dialog::init())
        .plugin(tauri_plugin_fs::init())
        .plugin(tauri_plugin_process::init())
        .plugin(tauri_plugin_shell::init())
        .plugin(tauri_plugin_store::Builder::default().build())
        .setup(|app| {
            StoreBuilder::new(app, "store.json")
                .default("language", json!({"value": "en"}))
                .default("direct_generation", json!({"value": false}))
                .build()?;

            Ok(())
        })
        .invoke_handler(tauri::generate_handler![
            get_app_settings,
            set_app_settings,
        ])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

Expected behavior

Store should be saved during restart, just like it exits.

Full tauri info output

[✔] Environment
    - OS: Manjaro 24.2.1 x86_64 (X64)
    ✔ webkit2gtk-4.1: 2.46.5
    ✔ rsvg2: 2.59.2
    ✔ rustc: 1.83.0 (90b35a623 2024-11-26)
    ✔ cargo: 1.83.0 (5ffbef321 2024-10-29)
    ✔ rustup: 1.27.1 (2024-05-07)
    ✔ Rust toolchain: stable-x86_64-unknown-linux-gnu (environment override by RUSTUP_TOOLCHAIN)
    - node: 23.4.0
    - pnpm: 9.12.3
    - yarn: 1.22.22
    - npm: 11.0.0

[-] Packages
    - tauri 🦀: 2.2.0
    - tauri-build 🦀: 2.0.4
    - wry 🦀: 0.48.0
    - tao 🦀: 0.31.0
    - tauri-cli 🦀: 2.0.4

[-] Plugins
    - tauri-plugin-fs 🦀: 2.2.0
    - tauri-plugin-store 🦀: 2.2.0
    - tauri-plugin-dialog 🦀: 2.2.0
    - tauri-plugin-process 🦀: 2.2.0
    - tauri-plugin-shell 🦀: 2.2.0

[-] App
    - build-type: bundle
    - CSP: unset
    - frontendDist: ../dist
    - devUrl: http://localhost:1420/
@Legend-Master Legend-Master added the bug Something isn't working label Jan 3, 2025
@Legend-Master
Copy link
Contributor

Looks like a bug from Tauri side, restart terminated the app before the Exit event gets send to the store plugin for it to save the states

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working status: upstream This issue needs to be fixed in an upstream project
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants