tauri-apps / tauri

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

[bug] Sidecar not killed if the application is forced kill #5576

Closed Sytten closed 1 year ago

Sytten commented 1 year ago

Describe the bug

From our experience, the sidecars are killed properly without any code on Linux and MacOs (not Windows) without any code. To fix the issue on Windows, we added code to kill the sidecar on RunEvent::ExitRequested. While it fixed the issue for Windows, we found that if the app is force killed with a kill -9 or Force Quit on Mac, it will not kill the sidecar properly. What is interesting is that the webkit processes are killed properly so I am unsure why the sidecar isn't.

Reproduction

  1. Launch an app with a sidecar
  2. Force Quit the app
  3. Observe that the sidecar is still running

Expected behavior

The sidecar should be killed alongside the application

Platform and versions

We use a forked version of tauri but our changes are not related to this.

Environment
  › OS: Mac OS 11.6.3 X64
  › Node.js: 16.17.0
  › npm: 8.15.0
  › pnpm: 7.14.0
  › yarn: 1.22.15
  › rustup: 1.24.3
  › rustc: 1.61.0
  › cargo: 1.61.0
  › Rust toolchain: stable-x86_64-apple-darwin

Packages
  › @tauri-apps/cli [NPM]: 1.1.1
  › @tauri-apps/api [NPM]: 1.0.1 (outdated, latest: 1.2.0)
  › tauri [RUST]: git+https://github.com/caido/dependency-tauri?branch=caido#428d6600f70bce020f6e0ab9cb5a5dc29e2ca9ac (1.1.1),
  › tauri-build [RUST]: git+https://github.com/caido/dependency-tauri?branch=caido#428d6600f70bce020f6e0ab9cb5a5dc29e2ca9ac (1.1.1),
  › tao [RUST]: 0.14.0,
  › wry [RUST]: 0.21.1,

App
  › build-type: bundle
  › CSP: default-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsafe-eval' 'unsafe-inline' 'self' img-src: 'self'
  › distDir: ../caido-ui/dist
  › devPath: http://localhost:10000/
  › framework: Vue.js

App directory structure
  ├─ dist
  ├─ node_modules
  ├─ scripts
  └─ src

Stack trace

No response

Additional context

No response

amrbashir commented 1 year ago

The webkit processes are handled by the OS, so it can successfully close that, but sidecar is handled by the tauri app and if you force close the app, it means all code execution is interrupted immediately and the cleanup code won't have a chance to run its logic, nothing we can do about this.

Sytten commented 1 year ago

I get that, but is there a best practice to handle that? Users somethings force kill applications. I guess the Sidecar process could monitor for it's parent somehow, but I appreciate any suggestion on that.

amrbashir commented 1 year ago

Nothing off the top of my head. kill -9 should be used carefully https://medium.com/naukri-engineering/use-of-kill-kill-9-naukri-engineering-5f01c4ff31ea

Sytten commented 1 year ago

Agreed but I would expect to have a solution for the MacOS Force Quit menu. It should send a SIGTERM, but our testing shows that it might not or at least it is not handled properly. For example, I tried to force quit VSCode and all the LSP servers (rust-analyzer) were also killed so clearly there is a way to handle it without leaving children process alive.

amrbashir commented 1 year ago

We are not doing anything special tbh, we are merely using the normal Rust Command and I couldn't find a reliable way to ensure there is no zombie processes.

Sytten commented 1 year ago

Yeah I did look a lot into it and I found what VSCode does. It's basically polling for the parent PID and cleaning the resources when it detects the parent was killed. I am trying to reproduce the same behaviour in rust. https://github.com/microsoft/vscode/blob/14459d6db1a088fbf1c631350cb27ade59fe1e39/src/bootstrap-fork.js#L226

Sytten commented 1 year ago

For those that might find this thread later, I built a simple crate (https://github.com/caido/process_alive) to verify if a certain process is alive. I then pass the tauri PID to the child process on start and the child starts monitoring the parent status in a thread every 5s and kills itself if the parent is dead.