tauri-apps / tauri

Build smaller, faster, and more secure desktop applications with a web frontend.
https://tauri.app
Apache License 2.0
82.02k stars 2.47k forks source link

[bug] localStorage not syncing between multiple webview windows on Linux #10981

Open Ynng opened 1 week ago

Ynng commented 1 week ago

Describe the bug

localStorage is synced between multiple webview windows on Windows and macOS but not Linux.

On Linux, only 1 window's local storage gets saved when the tauri app exits. The rest of the windows' local storage is out of sync with each other and is disposed of when the tauri app exits.

In the example video below, the 2 windows on the left are running in Fedora 40, their counters are out of sync. The 2 windows on the right are running in Windows 11, their counters are in sync.

https://github.com/user-attachments/assets/d1113ddc-5264-4426-b606-73cbe2e58f1b

Reproduction

The counter example shown in the video: https://github.com/Ynng/tauri-localstorage-sync-test

OR:

Reproduce from scratch:

Expected behavior

The localStorage entries should be in sync between the two windows. In the reproduction repo, the counter should stay in sync between the two windows.

Full tauri info output

[✔] Environment
    - OS: Ubuntu 24.4.0 x86_64 (X64)
    ✔ webkit2gtk-4.1: 2.44.3
    ✔ rsvg2: 2.58.0
    ✔ rustc: 1.81.0 (eeb90cda1 2024-09-04)
    ✔ cargo: 1.81.0 (2dbb1af80 2024-08-20)
    ✔ rustup: 1.27.1 (54dd3d00f 2024-04-24)
    ✔ Rust toolchain: stable-x86_64-unknown-linux-gnu (environment override by RUSTUP_TOOLCHAIN)
    - node: 22.8.0
    - pnpm: 9.10.0
    - npm: 10.8.2

[-] Packages
    - tauri 🦀: 2.0.0-rc.11
    - tauri-build 🦀: 2.0.0-rc.10
    - wry 🦀: 0.43.1
    - tao 🦀: 0.30.0
    - tauri-cli 🦀: 2.0.0-rc.12
    - @tauri-apps/api : 2.0.0-rc.4
    - @tauri-apps/cli : 2.0.0-rc.13

[-] Plugins
    - tauri-plugin-shell 🦀: 2.0.0-rc.3
    - @tauri-apps/plugin-shell : 2.0.0-rc.1

[-] App
    - build-type: bundle
    - CSP: unset
    - frontendDist: ../dist
    - devUrl: http://localhost:1420/
    - framework: React
    - bundler: Vite

Stack trace

No response

Additional context

I get warnings like these sometimes on linux

(process:1737224): libsoup-WARNING **: 19:55:42.244: Failed to execute query: database is locked

(process:1737224): libsoup-WARNING **: 19:55:42.244: Failed to execute query: no such table: soup_hsts_policies
Failed to create hard link from /home/wenqi/.cache/tauri-app/WebKitCache/Version 16/Blobs/3DE2C89C97AF2523D1A566F90E64D1EB6FED08A2 to /home/wenqi/.cache/tauri-app/WebKitCache/Version 16/Records/2D857AF3C0D6CD0AE5639C83ED0E05BB5A00AEED/Resource/D36F616E0D261016A1484FD7FF748035207E4BBE-blob

The problem exists with both Tauri v1 and v2

greenhat616 commented 6 days ago

Maybe you could checkout the plugin tauri-plugin-store for a workaround? Most state managers such as jotai, receive a Storage interface, like:

export interface AsyncStorage<Value> {
    getItem: (key: string, initialValue: Value) => PromiseLike<Value>;
    setItem: (key: string, newValue: Value) => PromiseLike<void>;
    removeItem: (key: string) => PromiseLike<void>;
    subscribe?: Subscribe<Value>;
}
export interface AsyncStringStorage {
    getItem: (key: string) => PromiseLike<string | null>;
    setItem: (key: string, newValue: string) => PromiseLike<void>;
    removeItem: (key: string) => PromiseLike<void>;
    subscribe?: StringSubscribe;
}

And tauri-plugin-store provide a onKeyChange fn to recevie value changed: https://github.com/tauri-apps/tauri-plugin-store/blob/96185a8aedb7a8658de7281cd8ac0149874de425/guest-js/index.ts#L181-L190

You could wrap it to a Storage impl to finish the cross platform usage.

Ynng commented 3 days ago

Thanks for the suggestion, I'm using tauri store as a work around for now