tauri-apps / tauri

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

`beforeDevCommand` is not killed on app exit #2794

Closed harshkhandeparkar closed 2 years ago

harshkhandeparkar commented 2 years ago

Describe the bug

Closing the tauri dev app window doesn't close the dev server started with beforeDevCommand (confirmed using the create-react-app template for create-tauri-app)

To Reproduce

Steps to reproduce the behavior:

  1. Create an app using create-react-app template
  2. Run tauri dev
  3. Close the app window using the x button or alt + f4
  4. Run tauri dev again.

Expected behavior

The dev server should be gracefully shut down when the app is closed.

rondonjon commented 2 years ago

gracefully shut down

Gracefully or not, please terminate the darn process :-)

I cannot reproduce the behavior reliably. Sometimes the dev server gets shut down, sometimes it keeps running. This is esepcially annoying while debugging: trying to force the app to do something, wondering why the app is acting so strange and then realizing a few minutes later that none of the code changes were actually deployed.

frnco commented 2 years ago

Gracefully or not, please terminate the darn process :-) Couldn't agree more.

I cannot reproduce the behavior reliably. Sometimes the dev server gets shut down, sometimes it keeps running. This is esepcially annoying while debugging: trying to force the app to do something, wondering why the app is acting so strange and then realizing a few minutes later that none of the code changes were actually deployed. For me the beforeDevCommand always closes properly, I'm on Arch Linux and mostly using Mint, not React, but that means there are some conditions for this problem to happen. To add some info on my system so it may help figure out what's different, it's Arch Linux, x64, I'm running tauri from cargo(i.e. cargo tauri dev), and I started a bunch of small projects using Tauri to test different combinations, which means I've had a lot (And I mean A LOT) of stuff on beforeDevCommand, never had any problem, all of the commands closed properly when I exited the Tauri app, some of the commands I can recall were vite for running VueJS, mint, elm... There were so many things I don't even remember it all. I'll check React later and report here just for completeness.

And as this points to something specific being necessary to trigger this failure in terminating the child process, I believe some tauri info would be nice here, or at least the OS and what the beforeDevCommand actually runs.

lucasfernog commented 2 years ago

I couldn't reproduce this one yet, can one of you see if #2883 fixes it? I assume this is on Windows so that's what i'm testing, no one pasted a tauri info output.

SeedyROM commented 2 years ago

@lucasfernog Here's my tauri info for the same issue, I consistently get this bug.

Operating System - Manjaro, version 21.2.2 X64

Node.js environment
  Node.js - 16.13.2
  @tauri-apps/cli - 1.0.0-rc.5
  @tauri-apps/api - 1.0.0-rc.1

Global packages
  npm - 8.3.0
  pnpm - 6.23.3
  yarn - 1.22.15

Rust environment
  rustup - 1.24.3
  rustc - 1.60.0-nightly
  cargo - 1.60.0-nightly
  toolchain - nightly-x86_64-unknown-linux-gnu 

App directory structure
/src
/node_modules
/build
/public
/src-tauri

App
  tauri - 1.0.0-rc.3 (no lockfile)
  tauri-build - no manifest (no lockfile)
  tao - no manifest (no lockfile)
  wry - no manifest (no lockfile)
  build-type - bundle
  CSP - unset
  distDir - ../build
  devPath - http://localhost:3000/
  framework - React
Janaka-Steph commented 2 years ago

I also always got it.

▶ cargo tauri info

Operating System - Mac OS, version 12.2.1 X64

Node.js environment Node.js - 16.14.0 @tauri-apps/cli - 1.0.0-rc.5 @tauri-apps/api - 1.0.0-rc.1

Global packages npm - 8.1.4 pnpm - Not installed yarn - 1.22.17

Rust environment rustup - 1.24.3 rustc - 1.58.1 cargo - 1.58.0 toolchain - stable-aarch64-apple-darwin

App directory structure /node_modules /public /scripts /.github /src-tauri /build /.git /.idea /src

App tauri - 1.0.0-rc.3 tauri-build - 1.0.0-rc.3 tao - 0.6.2 wry - 0.13.2 build-type - bundle CSP - default-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsafe-eval' 'unsafe-inline' 'self' img-src: 'self' distDir - ../build devPath - http://localhost:3003/ framework - React bundler - Webpack

nothingismagick commented 2 years ago

So this does seem to look like a react issue. I think we should go through and verify other non-"reacty" frameworks and see if its just particular here, or relevant for others.

FabianLars commented 2 years ago

It is a general problem, it's just worse with some frameworks/bundlers. For example i have the same problem with vitejs (no matter the frontend), but only if i close the app with ctrl+c. This also renders the terminal completely unusable for me.

JonasKruckenberg commented 2 years ago

As a workaround (and good practice in general) for those who use vite, here's the config we will be recommending in the guides:

{
  // prevent vite from obscuring rust errors
  clearScreen: false,
  // tauri expects a fixed port, fail if that port is not available
  server: {
    port: 3000,
    strictPort: true,
  },
  // to make use of `TAURI_PLATFORM`, `TAURI_ARCH`, `TAURI_FAMILY`, `TAURI_PLATFORM_VERSION`, `TAURI_PLATFORM_TYPE` and `TAURI_DEBUG` env variables
  envPrefix: ['VITE_', 'TAURI_'],
  build: {
    // tauri supports es2021
    target: ['es2021', 'chrome97', 'safari13'],
    // don't minify for debug builds
    minify: !process.env.TAURI_DEBUG && 'esbuild',
    // produce sourcemaps for debug builds
    sourcemap: !!process.env.TAURI_DEBUG,
  },
}

The important settings for this issue are these:

  // Fixes the console override issue
  clearScreen: false,
  // makes vite abort if orphan processes keep port 3000 occupied
  server: {
    port: 3000,
    strictPort: true,
  },
nothingismagick commented 2 years ago

There is another approach in bash, where you can grab the PID of the last command run after sending to the background and then using fg to bring it back - and then we could potentially try to sigkill that:

> npm start & export REACTPID=$! ; fg
# in another terminal
> kill 9 ${REACTPID}

Not sure this is really going to work though, I do see the need for us to dive back into this problem.

SeedyROM commented 2 years ago

This is what I've been doing to kill the process:

> lsof -n -i :3000 | grep LISTEN
node    273716 USER   20u  IPv4 3315639      0t0  TCP *:hbci (LISTEN)
> kill 273716
lucasfernog commented 2 years ago

The issue was fixed on Windows, but the Svelte template still has issues on Linux/macOS, we could only fix it using SIGKILL which is not a good approach.

Janaka-Steph commented 2 years ago

The issue should stay open then

Janaka-Steph commented 2 years ago

If anyone interested, you can kill the previous process when launching the app like so: "beforeDevCommand": "kill -9 $(lsof -i :3003 -t) &>/dev/null || true && yarn start"

Janaka-Steph commented 2 years ago

@FabianLars As discussed on Discord it appears that the issue still persist on MacOS M1, with create-react-app, quitting the app from red cross or Dock. Quitting with Ctrl+C on CLI kills the process successfully. Here are the steps I took:

npx create-react-app my-app
cd my-app
cargo tauri init
cargo update -p tauri
# add `yarn start` as beforeDevCommand
cargo tauri dev
# quit app with red cross or from Dock
lsof -i -P -n | grep LISTEN 
# process still running
▶ cargo tauri info

Environment
  › OS: Mac OS 12.3.0 X64
  › Node.js: 16.14.2
  › npm: 8.1.4
  › pnpm: Not installed!
  › yarn: 1.22.17
  › rustup: 1.24.3
  › rustc: 1.59.0
  › cargo: 1.59.0
  › Rust toolchain: stable-aarch64-apple-darwin 

Packages
  › @tauri-apps/cli [NPM]: 1.0.0-rc.7
  › @tauri-apps/api [NPM]: Not installed!
  › tauri [RUST]: 1.0.0-rc.6,
  › tauri-build [RUST]: 1.0.0-rc.5,
  › tao [RUST]: 0.7.0,
  › wry [RUST]: 0.14.0,

App
  › build-type: bundle
  › CSP: unset
  › distDir: ../build
  › devPath: http://localhost:3000/
  › framework: React

App directory structure
  ├─ node_modules
  ├─ public
  ├─ src-tauri
  ├─ .git
  ├─ .idea
  └─ src
lucasfernog commented 2 years ago

Thanks for the steps to reproduce it, I'll give it a try soon.

NanoAi commented 1 year ago

This issue still happens for me on Vite, mirrored from the Discord.

To reproduce...

  1. Create a Vite + Tauri app.
  2. Run tauri dev (cargo tauri dev in my case.)
  3. Close the app.
  4. Observe that the console remains held by the "vite" process however the standard exit command doesn't work and a ctrl + c must be sent.

(I'll attach a text version when I can.)

![image](https://user-images.githubusercontent.com/3096584/212435745-43a32fc8-cda9-4fe8-9bdd-e291ed478c77.png)
chirag1992m commented 5 months ago

I have been running into the same issue. Is there a resolution to this?

Killing the app with ctrl + C is fine, but closing the app with the cross button runs into the error:

ELIFECYCLE  Command failed with exit code 4294967295
juji commented 2 months ago
npm i -D kill-port

./package.json

{
  "scripts":{
    "dev": "kill-port 5173; npx tauri dev; kill-port 5173",
    "dev:vite": "vite --port 5173 --mode ssr",
  }
}

./src-tauri/tauri.conf.json

{
  "build": {
    "beforeDevCommand": "npm run dev:vite",
    "devPath": "http://localhost:5173",
  }
}