“检测到EventEmitter内存泄漏”错误导致崩溃

时间:2018-07-31 18:38:50

标签: javascript node.js pa11y

我(第一次)对Javascript / NodeJS还是很陌生,并且一直遇到内存泄漏错误的问题。我不断收到以下信息:

  

检测到可能的EventEmitter内存泄漏。添加了11个SIGINT侦听器。使用generator.setMaxListeners()增加限制

我似乎找不到解决它的方法。我尝试使用似乎可以在本地主机上运行的require('events').EventEmitter.defaultMaxListeners = 0;,但是在Linux服务器上运行它使我遇到多个“子进程xxxx仍然没有退出,发送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()

1 个答案:

答案 0 :(得分:2)

可能是循环重复太快,因此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);