Skip to content

Conversation

@sakina1303
Copy link

Adds a best-effort process-tree killer and a lightweight runtime sidecar PID registry, ensuring that descendant processes spawned by sidecars are properly terminated when their parent sidecar is killed.

This resolves the core issue described in #14360 by introducing both API and runtime cleanup mechanisms for sidecar process management.

Details:

  • tauri::process::kill_process_tree(pid: u32) — POSIX shell & PowerShell based, best-effort.
  • Sidecar PID registry via AppHandle::register_sidecar(pid) / unregister_sidecar(pid) for cleanup on exit.
  • App::cleanup_before_exit() drains registry and calls the kill helper.
  • CLI dev-run now attempts process-tree cleanup when stopping dev children.

Files Changed:

  • process.rs: Added kill_process_tree() (POSIX + PowerShell).
  • mod.rs: Added sidecar PID tracking + helper methods.
  • app.rs: Added register/unregister APIs; updated cleanup.
  • desktop.rs: Added kill-tree logic in dev-run path.
  • CHANGELOG.md / PR_DRAFT.md: Updated with notes.

Testing:

  • [ ✅ ] cargo test -p tauri, all tests passed.
  • [ ✅ ] Manual: verified registry and cleanup invoke kill helper.
  • Runtime cleanup is opt-in:
    Call app_handle.register_sidecar(child.id() as u32) after spawning.

Fixes #14360

@sakina1303 sakina1303 requested a review from a team as a code owner November 10, 2025 12:29
@github-project-automation github-project-automation bot moved this to 📬Proposal in Roadmap Nov 10, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Nov 10, 2025

Package Changes Through ed8d624

There are 1 changes which include @tauri-apps/api with patch

Planned Package Versions

The following package releases are the planned based on the context of changes in this pull request.

package current next
@tauri-apps/api 2.9.0 2.9.1

Add another change file through the GitHub UI by following this link.


Read about change files or the docs at github.com/jbolda/covector

Copy link
Contributor

@Legend-Master Legend-Master left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this!

/// background tasks). This is guaranteed to run from the thread performing
/// the app cleanup/exit sequence.
#[allow(unused_variables)]
fn cleanup_before_exit(&mut self, app: &AppHandle<R>) {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would also want to implement this for the TauriPlugin so that tauri::plugin::Builder could have access to this API

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for sure! I’ll add this to the TauriPlugin trait as well so it’s accessible via tauri::plugin::Builder.

@Legend-Master Legend-Master added this to the 2.10 milestone Nov 12, 2025
/// protected or system processes, or when the caller lacks sufficient
/// privileges. Callers should handle and log any errors returned by this
/// function.
pub fn kill_process_tree(pid: u32) -> std::io::Result<()> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this is a generic function and not bound to this app instance imo this should be moved into https://github.com/tauri-apps/plugins-workspace/tree/v2/plugins/process (and in theory that plugin's exit and restart should probably be part of the app core plugin instead but that's a discussion for another day).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be okay if I leave a TODO for now and move it in a follow-up PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we at least have to decide how to proceed here because once this is added in tauri we cannot remove it without a major release since removing apis is a breaking change.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I move kill_process_tree to the process plugin now as you suggested? I’ll update this PR to avoid a future breaking change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think that would make sense, yes. we can always move it back if needed (that way around works)

@Legend-Master
Copy link
Contributor

Legend-Master commented Nov 13, 2025

I think we have now gathered a few different problems around this now:

  • Only the side car process is killed on app exit, not the entire process tree
    • Just from testing, it seems like some of the processes are killed while some others are not???
  • tauri dev doesn't kill side cars (it kills the app forcefully by sending SIGKILL on mac and linux, TerminateProcess on Windows to cargo)
    • It seems to me that it does kill the side car but not always the entire process tree???
  • NSIS installers don't kill side cars, they kill the app forcefully through TerminateProcess (the Wix .msi installers do, they send WM_QUERYENDSESSION to the app which is now handled after fix(windows): emit LoopDestroyed on WM_ENDSESSION tao#1126 no, I thought they do but what actually happened is just it killed the tray icon by sending WM_CLOSE)
  • Updater plugin exits the app through std::process::exit which doesn't trigger the Exit event so side cars are not killed

My brain is basically fried right now, no more processes 😭

@FabianLars
Copy link
Member

  1. and perhaps 2) should imo be fixed via [shell] Add option to spawn command in process group plugins-workspace#1332 and not by trying to kill a process tree. prob doesn't help with the rest though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: 📬Proposal

Development

Successfully merging this pull request may close these issues.

[feat] Add kill tree for sidecar

4 participants