huenchao / questions

每天想到的问题,都放在issue中。
6 stars 2 forks source link

未解之谜之fd消耗殆尽。 #3

Open huenchao opened 4 years ago

huenchao commented 4 years ago

背景:

我用有个应用程序,接收到请求的时候,就fork一个子进程,开启puppeteer去做一些事情。每个子进程自然会有很多pid,并且占用一些fd。

问题是:

我明明在主进程里kill了子进程的process.pid。被运维通知我的宿主机的fd被消耗殆尽了,并且有2w多个僵尸进程。一开一关的逻辑为什么会造成这的问题?

运维原话:

准确地说 还不确定昨天这个 100+w 的 pid 是谁创建的. 因为现场已经没了. 只是今天看起来 大概率可能是你们的实例造成的. 今天有台机器 tst4-taknode-3上有 2 w+ 僵尸进程, 对比下你们 dig-it 创建的 pid 和 fd 是相符的

  1. 有没有必要开这么多 pid
  2. 开完 pid 有没有正确回收
  3. 如果没有正确回收, 会不会造成僵尸进程. => 归根到底, 看下代码写的有没有问题.

进程 fork 如果没有回收的话,很容易变成僵尸进程,这个看能否在主进程代码里加一下回收逻辑?

huenchao commented 4 years ago

解决步骤:

  1. fork-pr,先给dockerfile里加上lsof方便查看句柄。
  2. 本地压力测试。
huenchao commented 4 years ago

fork - pr 协作模式

主仓库: https://github.com/owner/repo.git 开发仓库:https://github.com/collaborator/repo.git

开发准备: git clone https://github.com/collaborator/repo.git git remote -v (看一下是否有添加 upstream) git remote add upstream https://github.com/owner/repo.git (如果没有,添加一下) git pull upstream branchname(与主仓库最新的代码合并一下)

本地开发: git add file1 file2 ... (添加修改的文件) git commit -m '我是一条 commit 信息,说明这次 commit 干嘛了' git push origin branchname(把本地的 commits 推到开发仓库对应的 branch)

发PR: 在开发仓库的 web 界面上创建 pull request 给主仓库 主仓库管理员 review PR 后合并

huenchao commented 4 years ago

参考文章: https://codereview.chromium.org/2384163002 https://github.com/GoogleChrome/puppeteer/issues/1825

huenchao commented 4 years ago

最后利用参考文章的参数修改后,稳定了业务。