-
Notifications
You must be signed in to change notification settings - Fork 56
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
Restart does not close old Electron instance #251
Comments
This problem happened before, and we solved it using vite-plugin-electron/src/index.ts Line 166 in 149c4a9
vite-plugin-electron/src/utils.ts Lines 144 to 150 in 149c4a9
|
Yes I do have the problem on the latest version (v0.28.7). However, it works if I call onstart({ startup }) {
if (process.electronApp) {
treeKillSync(process.electronApp.pid)
// or
process.kill(process.electronApp.pid)
}
startup()
}, It would be neat if it did a graceful shutdown with |
@probablykasper Would you like to test the following changes locally? vite-plugin-electron/src/utils.ts Line 179 in 149c4a9
function killTree(tree: PidTree) {
if (tree.children) {
for (const child of tree.children) {
killTree(child)
}
}
try {
- process.kill(tree.pid) // #214
+ process.kill(tree.pid, 'SIGKILL') // Force kill process
} catch { /* empty */ }
} |
That does work. Personally I would prefer waiting for the child process to exit though instead of force quitting |
Here's a working solution without force quitting: startup.exit = async () => {
if (process.electronApp) {
process.electronApp.removeAllListeners();
process.kill(process.electronApp.pid, 'SIGTERM');
await new Promise((resolve) => {
process.electronApp.once("exit", resolve);
})
}
}; |
Yes, it works for me. But I don't know how you want to incorporate |
Maybe you could try this for me, it should work fine. startup.exit = async () => {
if (process.electronApp) {
await new Promise((resolve) => {
process.electronApp.removeAllListeners()
process.electronApp.once('exit', resolve)
treeKillSync(process.electronApp.pid!)
})
}
} |
This comment was marked as outdated.
This comment was marked as outdated.
That does not work unfortunately. It doesn't quit, and gives me this error:
|
This comment was marked as outdated.
This comment was marked as outdated.
I am on macOS, yes. Are you testing with a |
This seems to be very tricky. The current known way is to circumvent this problem by customizing the import electron, { startup } from 'vite-plugin-electron'
startup.exit = async () => {
if (process.electronApp) {
process.electronApp.removeAllListeners()
process.kill(process.electronApp.pid)
}
} |
@probablykasper You can try this. This may seem like a complete hack. But it can at least help us circumvent the problem we are facing now. import { resolve } from 'path'
import { defineConfig } from 'vite'
import { svelte, vitePreprocess } from '@sveltejs/vite-plugin-svelte'
import electron, { startup } from 'vite-plugin-electron'
import tailwindcss from '@tailwindcss/vite'
+ startup.exit = async () => {
+ if (process.electronApp) {
+ process.electronApp.removeAllListeners()
+ process.kill(process.electronApp.pid)
+ }
+ }
export default defineConfig({
base: '/',
clearScreen: false,
resolve: {
alias: {
'@': resolve(__dirname, './src'),
},
},
build: {
outDir: './build/web',
sourcemap: true,
target: 'chrome106',
},
plugins: [
svelte({
preprocess: vitePreprocess(),
}),
tailwindcss(),
electron({
entry: ['./src/electron/main.ts', './src/electron/preload.ts'],
- onstart({ startup }) {
- if (process.electronApp) {
- process.kill(process.electronApp.pid, 'SIGTERM')
- } else {
- startup()
- }
- },
vite: {
build: {
outDir: './build/electron',
emptyOutDir: true,
rollupOptions: {
external: [/^.*\.node$/],
},
},
},
}),
],
}) |
Do Wondows users have that issue even with the Seems weird if the tree-kill is the only way to quit the app 🤔 |
This comment was marked as duplicate.
This comment was marked as duplicate.
I created a I tried your example and it works fine on |
It does not work unfortunately. I made a reproduction repo here: https://github.com/probablykasper/vite-plugin-electron-bug |
Private repo? |
So sorry, public now |
I write a module to terminate Electron zombie processes, which has been successfully used in my project for a long time. It has only been tested on Windows. import electron from 'vite-plugin-electron/simple';
import { killElectron } from 'kill-electron';
export default defineConfig({
plugins: [
electron({
onstart: args => {
if (os.platform() === 'win32') {
spawn('chcp', ['65001']);
}
if (process.env.VSCODE_DEBUG) {
console.log(
/* For `.vscode/.debug.script.mjs` */ '[startup] Electron App'
);
} else {
void args.startup();
}
// Orphaned Electron processes can occur during debugging
// To prevent system slowdowns, these processes need to be killed before each startup
killElectron({
zombieOnly: true
});
},
// ... rest code of vite config |
vite-plugin-electron v0.28.8 In mac, when I make modifications in the main process main.js, the BrowserWindow instance of electron will be re-created, export default defineConfig({
plugins: [
vue(),
vueJsx(),
vueDevTools(),
electron([
{
entry: "electron/main.js",
},
{
entry: "electron/preload.js",
},
]),
electronRenderer(),
],
}) |
also present when stopped and are more instances which uses the same dev port... Some of this https://github.com/vite-plugin repos may solve multiple instances or dev instances, but when there are multiple clients and users of Electron apps on Desktop, then this issue become something more when ports could be assigned for same port in different frameworks or different plugins or something different than Electron... |
Problem
My app has a
before-quit
handler which waits for the webview to be ready. But when the plugin makes Electron restart, the webview page/code is gone, so it can't tell the app to finish quitting. This means I end up with many Electron instances running at the same timeSolution
Keep the Vite webpage loaded until the app quits
Workaround
Disable restarting:
The text was updated successfully, but these errors were encountered: