child_process在docker容器内未收到SIGTERM

问题描述

我正在用打字稿写电子应用测试。

内部应用程序中有SIGTERM的注册侦听器

    process.on('SIGTERM',async () => {
      console.log('before exit');
      await this.exit(); //some inner function can't reach this statement anyway
    });

在本地一切都很好,但是在CI上,当在docker容器中运行的应用看起来没有收到SIGTERM时。

对于启动应用程序,我使用的是child_process.spawn

import type { ChildProcess } from 'child_process';
let yarnStart: ChildProcess = spawn('yarn','start',{ shell: true });

// 'start' is just script in package.json

我尝试用三种不同的方式杀死应用程序,但它们都不起作用。应用程序未收到SIGTERM否before exit,并且在最后一步ps aux中手动停止了ci-build后显示了我的流程。

// 1-way
yarnStart.kill('SIGTERM');

// 2-way
process.kill(yarnStart.pid,'SIGTERM');

// 3-way
import { execSync } from 'child_process';
execSync(`kill -15 ${yarnStart.pid}`);

为什么nodejs无法在docker容器内正确发送SIGTERM


唯一的区别-在本地,我有debian-9(拉伸)和基于debian-10(破坏者)的图像。相同的nodejs版本12.14.1。我将尝试构建具有拉伸效果的容器,以了解其行为,但对此是否会持怀疑态度。


UPD 进程启动之间存在某种差异(由于容器中CI上运行脚本,因此任何指令都以/bin/sh -c运行)

执行ps aux时,您会看到

//locally
myuser   101457  1.3  0.1 883544 58968 pts/8    Sl+  10:32   0:00 /usr/bin/node /usr/share/yarn/bin/yarn.js start
myuser   101468  1.6  0.2 829316 69456 pts/8    Sl+  10:32   0:00 /usr/bin/node /usr/share/yarn/lib/cli.js start
myuser   101479  1.6  0.2 829576 69296 pts/8    Sl+  10:32   0:00 /usr/bin/node /usr/share/yarn/lib/cli.js start:debug
myuser   101490  0.2  0.0 564292 31140 pts/8    Sl+  10:32   0:00 /usr/bin/node /home/myuser/myrepo/electron-app/node_modules/.bin/electron -r ts-node/register ./src/main.ts
myuser   101497  143  1.4 9215596 485132 pts/8  Sl+  10:32   0:35 /home/myuser/myrepo/node_modules/electron/dist/electron -r ts-node/register ./src/main.ts
//container
root       495  0.0  0.0   2392   776 ?        S    09:05   0:00 /bin/sh -c yarn start
root       496  1.0  0.2 893240 74336 ?        Sl   09:05   0:00 /usr/local/bin/node /opt/yarn-v1.22.5/bin/yarn.js start
root       507  1.7  0.2 885588 68652 ?        Sl   09:05   0:00 /usr/local/bin/node /opt/yarn-v1.22.5/lib/cli.js start
root       518  0.0  0.0   2396   712 ?        S    09:05   0:00 /bin/sh -c yarn start:debug
root       519  1.7  0.2 885336 68608 ?        Sl   09:05   0:00 /usr/local/bin/node /opt/yarn-v1.22.5/lib/cli.js start:debug
root       530  0.0  0.0   2396   780 ?        S    09:05   0:00 /bin/sh -c electron -r ts-node/register ./src/main.ts
root       531  0.3  0.0 554764 32080 ?        Sl   09:05   0:00 /usr/local/bin/node /opt/ci/jobfolder/job_id_423/electron-app/node_modules/.bin/electron -r ts-node/register ./src/main.ts
root       538  140  1.5 9072388 520824 ?      Sl   09:05   0:26 /opt/ci/jobfolder/job_id_423/node_modules/electron/dist/electron -r ts-node/register ./src/main.ts

实际上是杀死进程

// 1-way
yarnStart.kill('SIGTERM');

可以工作,但只能杀死/bin/sh -c yarn start和他的child_process /usr/local/bin/node /opt/yarn-v1.22.5/bin/yarn.js start,而实际上导致应用程序仍然挂起

解决方法

/bin/sh -c带来许多问题,其中一个值得注意的问题是您在应用程序中永远看不到信号。后代进程创建它们自己的/bin/sh -c

我在https://stackoverflow.com/a/33556110/4577788

中找到了解决方案

还找到了替代解决方案(但对我而言不起作用,可能是因为nodejs执行的细节)。 您可以使用进程组ID杀死属于同一进程树的所有进程。可以在这里https://stackoverflow.com/a/15139734/4577788

找到更多数据信息

当我尝试执行execSync('kill -- -942');execSync('kill -- "-942"');

发生了错误kill: illegal number -,没有找到发生原因的原因,只好予以修复。