具有异步意外无限循环的节点 JS



>我有一个JS文件,只有在预期由于超时而导致错误时才会自动重复,例如:

  • 无法访问网站 ->访问超时
  • 在"waitSelector"中查找的标记不存在 ->超时

由于我无法理解的原因,这会产生内存泄漏错误消息。已经检查了这样的问题,试图弄清楚它是否以某种方式链接到Node JS,Puppeteer或Forever库,但没有遇到其他人面临这个问题。这是我找到的最接近的,但根据这里的答案,他的脚本似乎没有错误。

JS文件基本上创建了一个具有基本url的服务器,并侦听端口88。当有人进入"http://localhost:88/some_parameters时,代码似乎到达第一个 try-catch,然后通过 console.log(2) 无限期地返回到 console.log(6),直到 try catch 成功或失败。

我不理解这种行为,因为我本来希望脚本被执行到 try catch,然后等待响应,然后根据返回的成功或错误继续。

我输入了两个命令,并在两种情况下都得到了相同的结果:forever start forever.jsonnode loadPage.js

我花了相当多的时间试图解决这个问题,但仍然不理解这种行为。我得到了我想要的图像,但它仍然很奇怪,最终污染了原木。

任何帮助将非常受欢迎!

更新

这种奇怪的行为仅在 try-catch 返回错误时发生。返回成功的响应后,代码将正常执行,而不会重复任何内容。

加载页面.js

var http = require('http');
var url = require('url');
const puppeteer = require('puppeteer');
const { exit } = require('process');

let baseUrl = "http://localhost:88";
// let url = "chrome://gpu";
// demo/love_down.php?clean=0&line1=1,8,0,0-125&orderid=ORDIB2B-9000105953_1
console.log(1);
http.createServer(function (req, res) {
console.log(2);
(async () => {
console.log(3);
// headless set to false opens new browser and closes it when process is completed
const browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox'],
timeout: 10000,
devtools: true
});
console.log(4);
const completeUrl = `${baseUrl}${req.url}`;
console.log(5);
const parsedUrl = url.parse(completeUrl, true);
console.log(6);
const page = await browser.newPage();
console.log(7);
// Variable to help determine and log required time for different operations lower in the code.
let t0 = Date.now();
// Console logs used in logs to make them easier to read
// new Date().toISOString().replace('T', ' ').substr(0, 19) converts time in acceptable format
// with space instead of weird characters.
console.log("[" + new Date().toISOString().replace('T', ' ').substr(0, 19) + "] - " +
"------------------------------Process start for new url------------------------------"
);
console.log("[" + new Date().toISOString().replace('T', ' ').substr(0, 19) + "] - " +
"The url being called is: " + `${baseUrl}${parsedUrl.pathname}${parsedUrl.search}`
);

// ------------------------------START - GO TO URL PART------------------------------
// Try-Catch reaching the targeted url
try {
await page.goto(`${baseUrl}${parsedUrl.pathname}${parsedUrl.search}`, {
waitUntil: 'networkidle0',
timeout: 10000
}).then(() => {
console.log("[" + new Date().toISOString().replace('T', ' ').substr(0, 19) + "] - " +
"Reached the website!"
);
});
} catch (error) {
console.log("[" + new Date().toISOString().replace('T', ' ').substr(0, 19) + "] - " +
"the following 'go to url action' error happened: ",
error
);
console.log("[" + new Date().toISOString().replace('T', ' ').substr(0, 19) + "] - " +
"------------------------------Error 'go to action' end for new url------------------------------"
);
browser.close();
process.exit();
}
let t2 = Date.now();
console.log("[" + new Date().toISOString().replace('T', ' ').substr(0, 19) + "] - " +
"The 'go to url action' took " + (t2 - t0) + " milliseconds."
);
// ------------------------------END - GO TO URL PART------------------------------

// ------------------------------START - WAIT FOR SELECTOR PART------------------------------
// try catch to detect the agreed tag's ID selector
// Once that selected is detected then it means that the model if fully loaded
try {
await page.waitForSelector('#uploadsuccess', {
timeout: 30000
}).then(() => {
console.log("[" + new Date().toISOString().replace('T', ' ').substr(0, 19) + "] - " +
"Got the selector!"
);
});
} catch (error) {
console.log("[" + new Date().toISOString().replace('T', ' ').substr(0, 19) + "] - " +
"the following 'wait for selector action' error happened: ",
error
);
console.log("[" + new Date().toISOString().replace('T', ' ').substr(0, 19) + "] - " +
"------------------------------Error 'wait for selector action' end for new url------------------------------"
);
browser.close();
process.exit();
}
let t3 = Date.now();
console.log("[" + new Date().toISOString().replace('T', ' ').substr(0, 19) + "] - " +
"The 'waitForSelector' action took " + (t3 - t2) + " milliseconds."
);
// ------------------------------END - WAIT FOR SELECTOR PART------------------------------

// ------------------------------START - IMAGE SAVING PART------------------------------
// Take a printscreen and saving the image in the corresponding folder
try {
await page.screenshot({
path: "./images/" + new Date().toISOString().replace('T', ' ').substr(0, 19) + "-apm-3d.png"
}).then(() => {
console.log("[" + new Date().toISOString().replace('T', ' ').substr(0, 19) + "] - " +
"Image saved"
);
});
} catch (error) {
console.log("[" + new Date().toISOString().replace('T', ' ').substr(0, 19) + "] - " +
"the following 'image saving' error happened: ",
error
);
console.log("[" + new Date().toISOString().replace('T', ' ').substr(0, 19) + "] - " +
"------------------------------Error 'image saving' end for new url------------------------------"
);
browser.close();
process.exit();
}
// ------------------------------END - IMAGE SAVING PART------------------------------

let t1 = Date.now();
console.log("[" + new Date().toISOString().replace('T', ' ').substr(0, 19) + "] - " +
"The whole process took " + (t1 - t0) + " milliseconds."
);
console.log("[" + new Date().toISOString().replace('T', ' ').substr(0, 19) + "] - " +
"------------------------------Process end for new url------------------------------"
);
browser.close();
})()
}).listen(88)

永远.log

1
2
3
4
5
6
7
[2020-09-09 10:52:14] - ------------------------------Process start for new url------------------------------
[2020-09-09 10:52:14] - The url being called is: http://localhost:88/demo/love_down.php?clean=0&line1=1,8,0,0-125&orderid=ORDIB2B-9000105953_1
2
3
4
5
6
7
[2020-09-09 10:52:14] - ------------------------------Process start for new url------------------------------
[2020-09-09 10:52:14] - The url being called is: http://localhost:88/demo/love_down.php?clean=0&line1=1,8,0,0-125&orderid=ORDIB2B-9000105953_1
2
3
4
5
6
7
[2020-09-09 10:52:15] - ------------------------------Process start for new url------------------------------
[2020-09-09 10:52:15] - The url being called is: http://localhost:88/demo/love_down.php?clean=0&line1=1,8,0,0-125&orderid=ORDIB2B-9000105953_1
2
3
4
5
6
7
[2020-09-09 10:52:15] - ------------------------------Process start for new url------------------------------
[2020-09-09 10:52:15] - The url being called is: http://localhost:88/demo/love_down.php?clean=0&line1=1,8,0,0-125&orderid=ORDIB2B-9000105953_1
2
3
4
5
6
7
[2020-09-09 10:52:15] - ------------------------------Process start for new url------------------------------
[2020-09-09 10:52:15] - The url being called is: http://localhost:88/demo/love_down.php?clean=0&line1=1,8,0,0-125&orderid=ORDIB2B-9000105953_1
2
3
4
5
6
7
[2020-09-09 10:52:15] - ------------------------------Process start for new url------------------------------
[2020-09-09 10:52:15] - The url being called is: http://localhost:88/demo/love_down.php?clean=0&line1=1,8,0,0-125&orderid=ORDIB2B-9000105953_1
2
3
4
5
6
7
[2020-09-09 10:52:16] - ------------------------------Process start for new url------------------------------
[2020-09-09 10:52:16] - The url being called is: http://localhost:88/demo/love_down.php?clean=0&line1=1,8,0,0-125&orderid=ORDIB2B-9000105953_1
2
3
4
5
6
7
[2020-09-09 10:52:16] - ------------------------------Process start for new url------------------------------
[2020-09-09 10:52:16] - The url being called is: http://localhost:88/demo/love_down.php?clean=0&line1=1,8,0,0-125&orderid=ORDIB2B-9000105953_1
2
3
4
5
6
7
[2020-09-09 10:52:17] - ------------------------------Process start for new url------------------------------
[2020-09-09 10:52:17] - The url being called is: http://localhost:88/demo/love_down.php?clean=0&line1=1,8,0,0-125&orderid=ORDIB2B-9000105953_1
2
3
4
5
6
7
[2020-09-09 10:52:17] - ------------------------------Process start for new url------------------------------
[2020-09-09 10:52:17] - The url being called is: http://localhost:88/demo/love_down.php?clean=0&line1=1,8,0,0-125&orderid=ORDIB2B-9000105953_1
2
3
(node:12919) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 exit listeners added to [process]. Use emitter.setMaxListeners() to increase limit
(node:12919) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 SIGINT listeners added to [process]. Use emitter.setMaxListeners() to increase limit
(node:12919) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 SIGTERM listeners added to [process]. Use emitter.setMaxListeners() to increase limit
(node:12919) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 SIGHUP listeners added to [process]. Use emitter.setMaxListeners() to increase limit
4
5
6
7
[2020-09-09 10:52:17] - ------------------------------Process start for new url------------------------------
[2020-09-09 10:52:17] - The url being called is: http://localhost:88/demo/love_down.php?clean=0&line1=1,8,0,0-125&orderid=ORDIB2B-9000105953_1
2
3
4
5
6
7
[2020-09-09 10:52:18] - ------------------------------Process start for new url------------------------------
[2020-09-09 10:52:18] - The url being called is: http://localhost:88/demo/love_down.php?clean=0&line1=1,8,0,0-125&orderid=ORDIB2B-9000105953_1
2
3
4
5
6
7
[2020-09-09 10:52:18] - ------------------------------Process start for new url------------------------------
[2020-09-09 10:52:18] - The url being called is: http://localhost:88/demo/love_down.php?clean=0&line1=1,8,0,0-125&orderid=ORDIB2B-9000105953_1
2
3
4
5
6
7
[2020-09-09 10:52:18] - ------------------------------Process start for new url------------------------------
[2020-09-09 10:52:18] - The url being called is: http://localhost:88/demo/love_down.php?clean=0&line1=1,8,0,0-125&orderid=ORDIB2B-9000105953_1
2
3
4
5
6
7
[2020-09-09 10:52:19] - ------------------------------Process start for new url------------------------------
[2020-09-09 10:52:19] - The url being called is: http://localhost:88/demo/love_down.php?clean=0&line1=1,8,0,0-125&orderid=ORDIB2B-9000105953_1
2
3
4
5
6
7
[2020-09-09 10:52:19] - ------------------------------Process start for new url------------------------------
[2020-09-09 10:52:19] - The url being called is: http://localhost:88/demo/love_down.php?clean=0&line1=1,8,0,0-125&orderid=ORDIB2B-9000105953_1
2
3
4
5
6
7
[2020-09-09 10:52:19] - ------------------------------Process start for new url------------------------------
[2020-09-09 10:52:19] - The url being called is: http://localhost:88/demo/love_down.php?clean=0&line1=1,8,0,0-125&orderid=ORDIB2B-9000105953_1
2
3
4
5
6
7
[2020-09-09 10:52:19] - ------------------------------Process start for new url------------------------------
[2020-09-09 10:52:19] - The url being called is: http://localhost:88/demo/love_down.php?clean=0&line1=1,8,0,0-125&orderid=ORDIB2B-9000105953_1
2
3
4
5
6
7
[2020-09-09 10:52:20] - ------------------------------Process start for new url------------------------------
[2020-09-09 10:52:20] - The url being called is: http://localhost:88/demo/love_down.php?clean=0&line1=1,8,0,0-125&orderid=ORDIB2B-9000105953_1
2
3
4
5
6
7
[2020-09-09 10:52:20] - ------------------------------Process start for new url------------------------------
[2020-09-09 10:52:20] - The url being called is: http://localhost:88/demo/love_down.php?clean=0&line1=1,8,0,0-125&orderid=ORDIB2B-9000105953_1
2
3
4
5
6
7
[2020-09-09 10:52:20] - ------------------------------Process start for new url------------------------------
[2020-09-09 10:52:20] - The url being called is: http://localhost:88/demo/love_down.php?clean=0&line1=1,8,0,0-125&orderid=ORDIB2B-9000105953_1
2
3
4
5
6
7
[2020-09-09 10:52:21] - ------------------------------Process start for new url------------------------------
[2020-09-09 10:52:21] - The url being called is: http://localhost:88/demo/love_down.php?clean=0&line1=1,8,0,0-125&orderid=ORDIB2B-9000105953_1
2
3
4
5
6
7
[2020-09-09 10:52:21] - ------------------------------Process start for new url------------------------------
[2020-09-09 10:52:21] - The url being called is: http://localhost:88/demo/love_down.php?clean=0&line1=1,8,0,0-125&orderid=ORDIB2B-9000105953_1
2
3
4
5
6
7
[2020-09-09 10:52:21] - ------------------------------Process start for new url------------------------------
[2020-09-09 10:52:21] - The url being called is: http://localhost:88/demo/love_down.php?clean=0&line1=1,8,0,0-125&orderid=ORDIB2B-9000105953_1
2
3
4
5
6
7
[2020-09-09 10:52:22] - ------------------------------Process start for new url------------------------------
[2020-09-09 10:52:22] - The url being called is: http://localhost:88/demo/love_down.php?clean=0&line1=1,8,0,0-125&orderid=ORDIB2B-9000105953_1
2
3
4
5
6
7
[2020-09-09 10:52:22] - ------------------------------Process start for new url------------------------------
[2020-09-09 10:52:22] - The url being called is: http://localhost:88/demo/love_down.php?clean=0&line1=1,8,0,0-125&orderid=ORDIB2B-9000105953_1
2
3
4
5
6
7
[2020-09-09 10:52:22] - ------------------------------Process start for new url------------------------------
[2020-09-09 10:52:22] - The url being called is: http://localhost:88/demo/love_down.php?clean=0&line1=1,8,0,0-125&orderid=ORDIB2B-9000105953_1
2
3
4
5
6
7
[2020-09-09 10:52:23] - ------------------------------Process start for new url------------------------------
[2020-09-09 10:52:23] - The url being called is: http://localhost:88/demo/love_down.php?clean=0&line1=1,8,0,0-125&orderid=ORDIB2B-9000105953_1
2
3
4
5
6
7
[2020-09-09 10:52:23] - ------------------------------Process start for new url------------------------------
[2020-09-09 10:52:23] - The url being called is: http://localhost:88/demo/love_down.php?clean=0&line1=1,8,0,0-125&orderid=ORDIB2B-9000105953_1
2
3
4
5
6
7
[2020-09-09 10:52:23] - ------------------------------Process start for new url------------------------------
[2020-09-09 10:52:23] - The url being called is: http://localhost:88/demo/love_down.php?clean=0&line1=1,8,0,0-125&orderid=ORDIB2B-9000105953_1
2
3
4
5
6
7
[2020-09-09 10:52:24] - ------------------------------Process start for new url------------------------------
[2020-09-09 10:52:24] - The url being called is: http://localhost:88/demo/love_down.php?clean=0&line1=1,8,0,0-125&orderid=ORDIB2B-9000105953_1
2
3
4
5
6
7
[2020-09-09 10:52:24] - ------------------------------Process start for new url------------------------------
[2020-09-09 10:52:24] - The url being called is: http://localhost:88/demo/love_down.php?clean=0&line1=1,8,0,0-125&orderid=ORDIB2B-9000105953_1
2
3
[2020-09-09 10:52:24] - the following 'go to url action' error happened:  TimeoutError: Navigation timeout of 10000 ms exceeded
at /Users/sebastienponcelet/Documents/3d-puppeteer/3d-puppeteer/node_modules/puppeteer/lib/cjs/puppeteer/common/LifecycleWatcher.js:106:111
at async FrameManager.navigateFrame (/Users/sebastienponcelet/Documents/3d-puppeteer/3d-puppeteer/node_modules/puppeteer/lib/cjs/puppeteer/common/FrameManager.js:88:21)
at async Frame.goto (/Users/sebastienponcelet/Documents/3d-puppeteer/3d-puppeteer/node_modules/puppeteer/lib/cjs/puppeteer/common/FrameManager.js:405:16)
at async Page.goto (/Users/sebastienponcelet/Documents/3d-puppeteer/3d-puppeteer/node_modules/puppeteer/lib/cjs/puppeteer/common/Page.js:826:16)
at async /Users/sebastienponcelet/Documents/3d-puppeteer/3d-puppeteer/loadPage.js:49:13
[2020-09-09 10:52:24] - ------------------------------Error 'go to action' end for new url------------------------------
error: Forever detected script exited with code: 0
error: Script restart attempt #1
1
error: restarting script because /Users/sebastienponcelet/Documents/3d-puppeteer/3d-puppeteer/stack-overflow.md changed
error: Forever detected script was killed by signal: SIGKILL
error: Script restart attempt #2
1

问题是你一次又一次地调用同一个HTTP服务器,而不返回响应。

HTTP 服务器正在侦听端口 88:

http.createServer(function (req, res) {
...
}).listen(88)

然后发出第一个 HTTP 请求,以便进程启动。2-7后,工艺到达生产线:

await page.goto(`${baseUrl}${parsedUrl.pathname}${parsedUrl.search}`, {

这指示木偶师转到页面http://localhost:88/demo/love_down.php?clean=0&line1=1,8,0,0-125&orderid=ORDIB2B-9000105953_1,该页面也在localhost:88上,再次点击相同的HTTP服务器。

当第二个请求到来时,它会经历相同的过程,启动浏览器,提取 URL,然后向http://localhost:88/...发出请求,生成另一个浏览器,发出另一个请求,依此类推。这就是为什么你一遍又一遍地看到2-7,这是无限递归的教科书例子。

由于所有浏览器都在等待响应,但永远不会到来,因此它们最终会超时,这就是您看到错误的原因。

你的 catch 块中有process.exit(),它正在关闭你的服务器。永远比看到进程被杀死并重新启动它。只需删除该行即可。

我已经设法通过简单地删除 try-catch 来解决问题。 真的不知道为什么,但这阻止了程序无限期循环,直到返回响应。

那里的代码现在看起来像这样:

// ------------------------------START - GO TO URL PART------------------------------
await page.goto(`${baseUrl}${parsedUrl.pathname}${parsedUrl.search}`, {
waitUntil: 'networkidle2',
timeout: 10000
})
let t2 = Date.now();
console.log("[" + new Date().toISOString().replace('T', ' ').substr(0, 19) + "] - " +
"The 'go to url action' took " + (t2 - t0) + " milliseconds."
);
// ------------------------------END - GO TO URL PART------------------------------

很高兴有任何评论来解释为什么这能解决问题。

最新更新