"EventEmitter memory leak detected" 个导致崩溃的错误

"EventEmitter memory leak detected" errors causing crashes

我是 Javascript/NodeJS 的新手(第一次),一直遇到内存泄漏错误的问题。我不断收到以下信息:

Possible EventEmitter memory leak detected. 11 SIGINT listeners added. Use emitter.setMaxListeners() to increase limit

我似乎找不到修复它的方法。我尝试使用似乎在本地主机上工作的 require('events').EventEmitter.defaultMaxListeners = 0;,但是 运行 它在 linux 服务器上给我多个 "child process xxxx still did not exit, sending a SIGTERM" 错误并迫使我不得不重新启动服务器。

var html = require('pa11y-reporter-html');
var pa11y = require('pa11y');
var fs = require("fs");
//require('events').EventEmitter.defaultMaxListeners = 0;


async function runPa11y(url) {
    try {
        let results = await pa11y(url);
        let htmlResults = html.results(results);
        return htmlResults
    } catch (err) {
        console.log("Error: " + err)
    }
}


function listScript() {
    const args = process.argv;
    const os = require('os');
    const siteName = args[2];

    pathToSiteDir = os.homedir() + "/" + siteName
    try {
        fd = fs.openSync(pathToSiteDir + '/audits/results-pally.html', 'w');
    } catch (err) {
        console.log("Could not open results.html" + err)
    } finally {
        if (fd !== undefined)
            fs.closeSync(fd);
    }

    var array = fs.readFileSync(pathToSiteDir + "/crawled.txt").toString().split("\n");
    array = array.filter(function(entry) { return entry.trim() != ''; });

    (function theLoop (i) {
        setTimeout(function () {
            console.log("url: " + array[i])
            let reply = runPa11y(array[i])
            process.removeAllListeners('exit')
            reply.then(function(result) {
                try {
                    fd = fs.openSync(pathToSiteDir + '/audits/results-pally.html', 'a');
                    fs.appendFileSync(fd, result + "<br>", 'utf8');
                } catch (err) {
                    console.log("Could not open results.html" + err)
                } finally {
                if (fd !== undefined)
                    fs.closeSync(fd);
                }
            });

            --i
            if (i >= 0) {          
                theLoop(i);       
                console.log("Links left to audit: " + i)
            }
        }, 300);
    })(array.length -1);
}

listScript()

可能是循环重复太快导致Node不堪重负。尝试将循环条件放入 promise 中,例如:

(function theLoop (i) {
    setTimeout(function () {
        console.log("url: " + array[i])
        let reply = runPa11y(array[i])
        process.removeAllListeners('exit')
        reply.then(function(result) {
            try {
                fd = fs.openSync(pathToSiteDir + '/audits/results-pally.html', 'a');
                fs.appendFileSync(fd, result + "<br>", 'utf8');
            } catch (err) {
                console.log("Could not open results.html" + err)
            } finally {
            if (fd !== undefined)
                fs.closeSync(fd);
            }

            --i
            if (i >= 0) {          
                theLoop(i);       
                console.log("Links left to audit: " + i)
            }
        });

    }, 300);
})(array.length -1);