Skip to content

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

Closed
tauri-apps/tauri
#12313
@SergeFan

Description

@SergeFan

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/

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingstatus: upstreamThis issue needs to be fixed in an upstream project

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions