Repository: tauri-apps/tauri (language: rust)

Issue:
[bug] panic: assertion failed: removed_resource.is_none() in src/resources/mod.rs:116:5
### Describe the bug

I received the information from a person who has run the application many times in short intervals since closing it.
It threw him an error from tauri crate.

![image](https://github.com/user-attachments/assets/bbc6245f-e31b-47ad-97a3-082cae4fb2ca)
![image](https://github.com/user-attachments/assets/3c7c5cd0-8cca-465d-a56c-f7cfe77003db)


### Reproduction

1. Launch apllication
2. Close Application
3. Launch apllication
4. Close Application
etc.

### Expected behavior

The application should work without a problem.

### Full `tauri info` output

```text
[✔] Environment
    - OS: Windows 10.0.26100 x86_64 (X64)
    ✔ WebView2: 131.0.2903.86
    ✔ MSVC: 
        - Visual Studio Build Tools 2022
        - Visual Studio Community 2022
    ✔ rustc: 1.85.0-nightly (d49be02cf 2024-12-02)
    ✔ cargo: 1.85.0-nightly (4c39aaff6 2024-11-25)
    ✔ rustup: 1.27.1 (54dd3d00f 2024-04-24)
    ✔ Rust toolchain: nightly-x86_64-pc-windows-msvc (default)
    - node: 18.20.4
    - npm: 10.7.0

[-] Packages
    - tauri 🦀: 2.1.1
    - tauri-build 🦀: 2.0.3
    - wry 🦀: 0.47.2
    - tao 🦀: 0.30.8
    - tauri-cli 🦀: 2.1.0
    - @tauri-apps/api : 2.0.0 (outdated, latest: 2.1.1)
    - @tauri-apps/cli : 2.1.0

[-] Plugins
    - tauri-plugin-single-instance 🦀: 2.0.2
    - @tauri-apps/plugin-single-instance : not installed!
    - tauri-plugin-dialog 🦀: 2.0.4
    - @tauri-apps/plugin-dialog : 2.0.1 (outdated, latest: 2.2.0)
    - tauri-plugin-http 🦀: 2.0.4
    - @tauri-apps/plugin-http : 2.0.1 (outdated, latest: 2.2.0)
    - tauri-plugin-os 🦀: 2.0.1
    - @tauri-apps/plugin-os : 2.0.0 (outdated, latest: 2.2.0)
    - tauri-plugin-notification 🦀: 2.0.1
    - @tauri-apps/plugin-notification : 2.0.0 (outdated, latest: 2.2.0)
    - tauri-plugin-global-shortcut 🦀: 2.0.1
    - @tauri-apps/plugin-global-shortcut : 2.0.0 (outdated, latest: 2.2.0)
    - tauri-plugin-fs 🦀: 2.1.0
    - @tauri-apps/plugin-fs : 2.0.3 (outdated, latest: 2.2.0)
    - tauri-plugin-updater 🦀: 2.1.0
    - @tauri-apps/plugin-updater : 2.0.0 (outdated, latest: 2.3.0)
    - tauri-plugin-process 🦀: 2.0.1
    - @tauri-apps/plugin-process : 2.0.0 (outdated, latest: 2.2.0)
    - tauri-plugin-shell 🦀: 2.0.2
    - @tauri-apps/plugin-shell : 2.0.1 (outdated, latest: 2.2.0)
    - tauri-plugin-log 🦀: 2.0.3
    - @tauri-apps/plugin-log : not installed!

[-] App
    - build-type: bundle
    - CSP: unset
    - frontendDist: ../dist
    - devUrl: http://localhost:1420/
    - framework: React
    - bundler: Vite
```


### Stack trace

```text
no provided
```


### Additional context

_No response_
[bug] panic: assertion failed: removed_resource.is_none() in src/resources/mod.rs:116:5
### Describe the bug

I received the information from a person who has run the application many times in short intervals since closing it.
It threw him an error from tauri crate.

![image](https://github.com/user-attachments/assets/bbc6245f-e31b-47ad-97a3-082cae4fb2ca)
![image](https://github.com/user-attachments/assets/3c7c5cd0-8cca-465d-a56c-f7cfe77003db)


### Reproduction

1. Launch apllication
2. Close Application
3. Launch apllication
4. Close Application
etc.

### Expected behavior

The application should work without a problem.

### Full `tauri info` output

```text
[✔] Environment
    - OS: Windows 10.0.26100 x86_64 (X64)
    ✔ WebView2: 131.0.2903.86
    ✔ MSVC: 
        - Visual Studio Build Tools 2022
        - Visual Studio Community 2022
    ✔ rustc: 1.85.0-nightly (d49be02cf 2024-12-02)
    ✔ cargo: 1.85.0-nightly (4c39aaff6 2024-11-25)
    ✔ rustup: 1.27.1 (54dd3d00f 2024-04-24)
    ✔ Rust toolchain: nightly-x86_64-pc-windows-msvc (default)
    - node: 18.20.4
    - npm: 10.7.0

[-] Packages
    - tauri 🦀: 2.1.1
    - tauri-build 🦀: 2.0.3
    - wry 🦀: 0.47.2
    - tao 🦀: 0.30.8
    - tauri-cli 🦀: 2.1.0
    - @tauri-apps/api : 2.0.0 (outdated, latest: 2.1.1)
    - @tauri-apps/cli : 2.1.0

[-] Plugins
    - tauri-plugin-single-instance 🦀: 2.0.2
    - @tauri-apps/plugin-single-instance : not installed!
    - tauri-plugin-dialog 🦀: 2.0.4
    - @tauri-apps/plugin-dialog : 2.0.1 (outdated, latest: 2.2.0)
    - tauri-plugin-http 🦀: 2.0.4
    - @tauri-apps/plugin-http : 2.0.1 (outdated, latest: 2.2.0)
    - tauri-plugin-os 🦀: 2.0.1
    - @tauri-apps/plugin-os : 2.0.0 (outdated, latest: 2.2.0)
    - tauri-plugin-notification 🦀: 2.0.1
    - @tauri-apps/plugin-notification : 2.0.0 (outdated, latest: 2.2.0)
    - tauri-plugin-global-shortcut 🦀: 2.0.1
    - @tauri-apps/plugin-global-shortcut : 2.0.0 (outdated, latest: 2.2.0)
    - tauri-plugin-fs 🦀: 2.1.0
    - @tauri-apps/plugin-fs : 2.0.3 (outdated, latest: 2.2.0)
    - tauri-plugin-updater 🦀: 2.1.0
    - @tauri-apps/plugin-updater : 2.0.0 (outdated, latest: 2.3.0)
    - tauri-plugin-process 🦀: 2.0.1
    - @tauri-apps/plugin-process : 2.0.0 (outdated, latest: 2.2.0)
    - tauri-plugin-shell 🦀: 2.0.2
    - @tauri-apps/plugin-shell : 2.0.1 (outdated, latest: 2.2.0)
    - tauri-plugin-log 🦀: 2.0.3
    - @tauri-apps/plugin-log : not installed!

[-] App
    - build-type: bundle
    - CSP: unset
    - frontendDist: ../dist
    - devUrl: http://localhost:1420/
    - framework: React
    - bundler: Vite
```


### Stack trace

```text
no provided
```


### Additional context

_No response_


Discussion / hints:

Fix patch:
diff --git a/.changes/fix-resource-id.md b/.changes/fix-resource-id.md
new file mode 100644
index 000000000000..177f8a7bfe2f
--- /dev/null
+++ b/.changes/fix-resource-id.md
@@ -0,0 +1,5 @@
+---
+'tauri': patch:bug
+---
+
+Fixed a panic caused by an assert when the resource random id has been used already.
\ No newline at end of file
diff --git a/crates/tauri/src/resources/mod.rs b/crates/tauri/src/resources/mod.rs
index 2c7fa018227f..72498503c730 100644
--- a/crates/tauri/src/resources/mod.rs
+++ b/crates/tauri/src/resources/mod.rs
@@ -111,7 +111,11 @@ impl ResourceTable {
   ///
   /// Returns a unique resource ID, which acts as a key for this resource.
   pub fn add_arc_dyn(&mut self, resource: Arc<dyn Resource>) -> ResourceId {
-    let rid = Self::new_random_rid();
+    let mut rid = Self::new_random_rid();
+    while self.index.contains_key(&rid) {
+      rid = Self::new_random_rid();
+    }
+
     let removed_resource = self.index.insert(rid, resource);
     assert!(removed_resource.is_none());
     rid


Test patch: