You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NodeJS出现RangeError: Maximum call stack size exceeded问题求助

解决批量发送邮件时的RangeError问题

Hey there, let's break down why you're hitting that RangeError: Maximum call stack size exceeded when sending a large number of emails, and how to fix it.

错误原因分析

The core issue here is uncontrolled high concurrency. When you use Promise.all(promises) with a huge list of emails, you're creating hundreds (or thousands) of asynchronous sendEmail calls all at once.

Even though these are async functions, initializing all those promises and queueing up so many tasks can overload Node.js's call stack with synchronous setup work. Additionally, flooding the event loop with too many pending requests can lead to stack overflow as the runtime tries to manage all those concurrent operations.

Your processEmail function's Promise wrapping might also contribute if there's any deep synchronous call chain inside the SES/nodemailer logic, but the main culprit is definitely the massive simultaneous batch of requests.

解决办法

1. 手动分批处理(最直接的无依赖方案)

Instead of firing off all emails at once, split your email list into smaller batches. Process one batch fully before moving on to the next—this keeps the number of concurrent requests manageable.

Here's how to adjust your code:

const emails = await readingCsvFile('emails.csv');
const batchSize = 15; // 可以根据SES限制或服务器性能调整这个数值

// 分批遍历邮箱列表
for (let i = 0; i < emails.length; i += batchSize) {
  const currentBatch = emails.slice(i, i + batchSize);
  // 处理当前批次的所有邮件
  const batchPromises = currentBatch.map(async (email) => {
    try {
      await sendEmail(email);
    } catch (err) {
      console.error(`发送邮件到 ${email} 失败:`, err);
      // 这里可以添加自定义错误处理,比如记录到日志文件
    }
  });
  await Promise.all(batchPromises);
}

2. 使用限流库(更简洁的方案)

If you don't mind adding a small dependency, libraries like p-limit make it easy to cap concurrent tasks without writing manual batch loops.

Example with p-limit:

const pLimit = require('p-limit');
// 限制同时执行10个邮件发送任务
const limit = pLimit(10);

const emails = await readingCsvFile('emails.csv');
const promises = emails.map((email) => 
  limit(async () => {
    try {
      await sendEmail(email);
    } catch (err) {
      console.error(`发送邮件到 ${email} 失败:`, err);
    }
  })
);

await Promise.all(promises);

3. 排查processEmail内部的同步代码

Double-check the logic inside your processEmail function (where you integrate nodemailer with SES). Make sure there's no deep synchronous recursion or overly nested function calls—these can also trigger stack overflow, though this is less common than the concurrency issue.

内容的提问来源于stack exchange,提问作者Dev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:38:17