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

Change: use tree-kill #122

Merged
merged 11 commits into from
Oct 31, 2023
Merged

Change: use tree-kill #122

merged 11 commits into from
Oct 31, 2023

Conversation

sevenc-nanashi
Copy link
Contributor

This PR makes this plugin use tree-kill, which kills child processes.

@caoxiemeihao
Copy link
Member

caoxiemeihao commented Dec 26, 2022

Thanks for your PR!
Is this required? Do the subprocesses refer to the ones opened in Electron Main? Or is there another way to accomplish what you need?
vite-plugin-electron needs to be kept lean enough.

@sevenc-nanashi
Copy link
Contributor Author

Yes, tree-kill kills the subprocesses of electron main.
We can kill the process with SIGINT and do post-process in app, but app may refuse SIGINT and may do something bad in vite-plugin-electron.

@caoxiemeihao
Copy link
Member

caoxiemeihao commented Dec 26, 2022

Does this happen often? Not sure there is a good benefit from introducing tree-kill, or is there no way to solve this problem without introducing tree-kill?

@sevenc-nanashi
Copy link
Contributor Author

There's a issue to do tree-kill in node, but it's closed: nodejs/node#40438

@caoxiemeihao
Copy link
Member

caoxiemeihao commented Dec 27, 2022

Once again, thank you for the PR.
But vite-plugin-electron goes for minimalism and zero dependencies is hard to come by.
Not sure how many users will need this feature, and even as it is now, users can still do the same thing in onstart.
I'll keep an eye on this PR.
https://github.com/electron-vite/vite-plugin-electron/blob/v0.11.1/examples/custom-start-electron-app/vite.config.ts#L8-L11

export default defineConfig({
  plugins: [
    electron({
      entry: 'electron/main.ts',
      onstart: options => {
        import('tree-kill').then(({ default: kill }) => kill(process.electronApp.pid!))
        options.startup(['.', '--no-sandbox'])
      },
    }),
  ],
})

@caoxiemeihao
Copy link
Member

Okay!
Looks like other people are having this issue too, so thanks for the PR.

But I don't like to add tree-kill to the required dependencies, like vite-plugin-electron-renderer

package.json

{
  "peerDependencies": {
    "vite-plugin-electron-renderer": "*",
+   "tree-kill": "*"
  },
  "peerDependenciesMeta": {
    "vite-plugin-electron-renderer": {
      "optional": true
    },
+   "tree-kill": {
+     "optional": true
+   }
  }
}

src/index.ts

startup.exit = async () => {
  if (process.electronApp) {
    process.electronApp.removeAllListeners()
    try {
      const { default: treeKill } = await import('tree-kill')
      treeKill(process.electronApp.pid!)
    } catch (error) {
      process.electronApp.kill()
    }
  }
}

Would you mind making some changes? 😄

cc @shuyuTech, thanks you for explaining this issue. 👍

@sevenc-nanashi
Copy link
Contributor Author

Done.

@sevenc-nanashi
Copy link
Contributor Author

wtf

@sevenc-nanashi
Copy link
Contributor Author

Managed to open again.

@caoxiemeihao caoxiemeihao merged commit 65b73c2 into electron-vite:main Oct 31, 2023
@caoxiemeihao caoxiemeihao mentioned this pull request Nov 6, 2023
@laowus
Copy link

laowus commented Dec 9, 2023

你好,我是用js的,dev有时候,electron启动不起来。 然后随便修改下无关代码,重新dev,又能起来。不知道什么原因。

@caoxiemeihao
Copy link
Member

你好,我是用js的,dev有时候,electron启动不起来。 然后随便修改下无关代码,重新dev,又能起来。不知道什么原因。

把 tree-kill 去掉试试先,这副作用真的 yue 🤮

@laowus
Copy link

laowus commented Dec 10, 2023

没注意,原来是vite-plugin-electron里面添加了tree-kill,是可以在哪来设置可以关掉吗?

@laowus
Copy link

laowus commented Dec 10, 2023

你好,我是用js的,dev有时候,electron启动不起来。 然后随便修改下无关代码,重新dev,又能起来。不知道什么原因。

把 tree-kill 去掉试试先,这副作用真的 yue 🤮

我也碰到这个问题了,突然就起不来了

我调试了代码 dev 的时候 , 会自动打包, 然后会启动一个closeBundle , 然后我console了, 你可以看我后面贴的代码.
空的时候研究一下,我现在用用能启动就不关了。就没理他。不过好像最新代码里面已经添加了 tree-kill, 不知道怎么关闭这个功能.

closeBundle() {
if (++closeBundleCount < entryCount)
return;
if (options2.onstart) {// 走这里就启动不了.
options2.onstart.call(this, {
startup,
reload() {
server.ws.send({ type: "full-reload" });
}
});
} else { //这里是启动electron
startup();
}
}

@caoxiemeihao
Copy link
Member

哈哈,感谢你的提醒。
晚点我调试下试试,源码没多少,大家都能搞

@noob9527
Copy link

noob9527 commented Jan 16, 2024

你好,我是用js的,dev有时候,electron启动不起来。 然后随便修改下无关代码,重新dev,又能起来。不知道什么原因。

我也遇到一样的问题

  window.once('ready-to-show', () => {
    logger.debug('ready-to-show')
    window.show()
  })

我是监听 ready-to-show 事件启动 window 的,但在 dev 环境下,经常不触发这个事件。
随便改改代码,加一个空行之类的又可以了。
但我没有装 tree-kill

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