node-tree-kill icon indicating copy to clipboard operation
node-tree-kill copied to clipboard

EBADF error when spawning processes on macOS in watch mode environments

Open jinmatt opened this issue 10 months ago • 4 comments

When using tree-kill on macOS within applications that frequently restart (such as NestJS in watch mode), the module fails with a spawn EBADF error. The error occurs in the buildProcessTree function when attempting to spawn child processes to get the process tree. This appears to be due to file descriptor handling issues specific to macOS.

Environment

OS: macOS 15.3.1 Node.js version: v20.18.3 tree-kill version: v1.2.2

Error stack trace

node:internal/child_process:420
    throw new ErrnoException(err, 'spawn');
    ^

Error: spawn EBADF
    at ChildProcess.spawn (node:internal/child_process:420:11)
    at spawn (node:child_process:762:9)
    at /Users/.../node_modules/tree-kill/index.js:33:18
    at buildProcessTree (/Users/.../node_modules/tree-kill/index.js:90:14)
    at module.exports (/Users/.../node_modules/tree-kill/index.js:32:9)
    at /Users/.../node_modules/@nestjs/cli/actions/start.action.js:57:17
    at Object.onWatchStatusChange (/Users/.../node_modules/@nestjs/cli/lib/compiler/watch-compiler.js:83:17)
    at /Users/.../node_modules/typescript/lib/typescript.js:124995:32
    at emitFilesAndReportErrors (/Users/.../node_modules/typescript/lib/typescript.js:124843:7)
    at result.afterProgramCreate (/Users/.../node_modules/typescript/lib/typescript.js:124991:7) {
  errno: -9,
  code: 'EBADF',
  syscall: 'spawn'
}

Noticed this started happening after updating macOS to Sequoia(v15).

The issue appears to be related to how spawn handles file descriptors on macOS when used recursively in rapid succession. The current implementation uses spawn to build the process tree, which can lead to file descriptor exhaustion or race conditions when processes are frequently being killed and restarted. I'm using nestjs cli in a monorepo, so can be because of the large number of files.

I have tested a fix that uses execSync, happy to submit a PR if this looks good:

function buildProcessTreeMacOS(parentPid, tree, pidsToProcess, cb) {
    try {
        let stdout = '';
        try {
            stdout = execSync('pgrep -P ' + parentPid, {
                encoding: 'utf8',
                timeout: 2000,
                maxBuffer: 1024 * 1024
            });
        } catch (execError) {
            if (execError.status !== 1) {
                throw execError;
            }
        }

        delete pidsToProcess[parentPid];

        const childPids = stdout.trim().split('\n').filter(Boolean);
        
        if (childPids.length === 0) {
            if (Object.keys(pidsToProcess).length === 0) {
                cb();
            }
            return;
        }

        childPids.forEach(function(pidStr) {
            const pid = parseInt(pidStr, 10);
            if (!isNaN(pid)) {
                tree[parentPid].push(pid);
                tree[pid] = [];
                pidsToProcess[pid] = 1;
                buildProcessTreeMacOS(pid, tree, pidsToProcess, cb);
            }
        });
    } catch (err) {
        delete pidsToProcess[parentPid];
        if (Object.keys(pidsToProcess).length === 0) {
            cb();
        }
    }
}

jinmatt avatar Mar 12 '25 02:03 jinmatt

Opened a PR with the above changes https://github.com/pkrumins/node-tree-kill/pull/44

jinmatt avatar Mar 12 '25 03:03 jinmatt

I am running into this issue too, and the nest.js team is considering moving from tree-kill to another library if this issue is not resolved.

https://discord.com/channels/520622812742811698/1364381581938262116

@pkrumins @billiegoose - is tree-kill still maintained? If not, we can look for a replacement

yaacov-commenda avatar Apr 23 '25 00:04 yaacov-commenda

We're running into this issue. Looking at the commit history and issues and prs I think it is safe to say this repo is not maintained anymore.

AdriVanHoudt avatar Jun 19 '25 09:06 AdriVanHoudt

@AdriVanHoudt - my team just generated a patch from https://github.com/pkrumins/node-tree-kill/pull/44 using patch-package

yaacov-commenda avatar Jun 23 '25 04:06 yaacov-commenda