Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sidecar process is still alive when the main process exits #1896

Closed
zzl221000 opened this issue May 25, 2021 · 7 comments
Closed

Sidecar process is still alive when the main process exits #1896

zzl221000 opened this issue May 25, 2021 · 7 comments

Comments

@zzl221000
Copy link

First of all, I think this project is really great!❤
But I have encountered a little problem in using it.
I have embedded an aria2 downloader in the program using Sidecar. Everything was working fine until I closed the program and found that the aria2 process was still alive.
How should I deal with this problem?

I solved it in the following way for now. But I think it is the wrong way.

pub struct MyCommandChild {
  pub inner: Arc<SharedChild>,
  pub stdin_writer: PipeWriter,
}
thread::spawn(move || {
        let child = spawn_aria2c_server();
        let my_child: MyCommandChild = unsafe {
          std::mem::transmute(child)
        };
        let sc = my_child.inner;

        window.on_window_event(move |event|
          match event {
            WindowEvent::Destroyed =>{match sc.kill(){
              Ok(_) => {println!("shutdown aria2 server success")}
              Err(_) => {eprintln!("shutdown aria2 server failed,please close by self")}
            };}
            _ => {}
          }
        )
      });
@lucasfernog
Copy link
Member

Process cleanup must be done manually :( I think we can do it on our end for sidecar processes though.

@selfconfigio
Copy link

@lucasfernog Do you know how specify to tauri to keep the sidecar process alive since now it is killed by default?

@FabianLars
Copy link
Member

You could listen to the ExitRequested RunEvent like this:

RunEvent::ExitRequested { api, .. } => {
api.prevent_exit();
}
and then use std::process::exit(0) to exit the app instead of Tauri's exit. This will skip the sidecar cleanup but also the system tray cleanup :/

p.s. not sure if that's actually enough to keep the process running, but this removes Tauri's manual clean-up

@selfconfigio
Copy link

@FabianLars Nice, I never noticed this part of the API
I was able to keep the process alive when combining this with std::process::Command over tauri::api::process::Command
To future readers, the tauri config I used to achieve the behaviour of non killed exes when tauri exits also included:

    "allowlist": {
      "shell": {
        "scope": [
          {
            "name": "resources/my_bin1",
            "args": true,
            "sidecar": true // I don't think this is necessary actually with std::process
          },
          {
            "name": "resources/my_bin2.exe",
            "args": true,
            "sidecar": true // I don't think this is necessary actually with std::process
          }
        ],
        "execute": true,
        "open": true,
        "sidecar": true // I don't think this is necessary actually with std::process
      }
    },
    "bundle": {
      "externalBin": [
      ],
       // Using resources over externalBin, since the binaries are already cross platform/handled
      "resources": [
        "resources/my_bin1",
        "resources/my_bin2.exe",
        "resources/a_normal_resource.png",
      ]
    }

@FabianLars
Copy link
Member

Forgot to mention that stuff, you can also convert sidecars to std commands (in case you wanna use sidecars instead of resources). Something like this iirc

let tauri_cmd = tauri::process::Command::new_sidecar("sidecar").unwrap();
let std_cmd = std::process::Command::from(tauri_cmd);

@CMiller3
Copy link

CMiller3 commented Nov 4, 2022

Hi FabianLars -

For some reason I'm attempting to recreate what you suggested, and even attempted to follow the tauri::api::process::Command -> from(cmd: Command) -> StdCommand, but for some reason I get this error:

mismatched types
expected struct `std::process::Command`, found struct `tauri::api::process::Command`
type StdCommand = std::process::Command;
type SideCarCommand = tauri::api::process::Command;

let tauri_cmd: SideCarCommand = SideCarCommand::new_sidecar("mpvote-processor").unwrap();
let std_cmd: StdCommand = SideCarCommand::from(tauri_cmd);

Edit: I'm a Swift first developer, and I seriously need to work more on FnMut and such... here was my workaround if it helps anyone:

RunEvent::ExitRequested { api, .. } => {
println!("EXIT IS REQUESTED {}", pid);

        let output = if cfg!(target_os = "windows") {
            Command::new("cmd")
                    .args(["taskkill", "-f", "-im", "processName"])
                    .output()
                    .expect("failed to execute process")
        } else {
            Command::new("sh")
                    .args(["pkill -f processName"])
                    .output()
                    .expect("failed to execute process")

        };
        
    }

@FabianLars
Copy link
Member

The last line was wrong, it'd be StdCommand::from not SidecarCommand::from. Glad you found another way in the meantime

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants