async.js icon indicating copy to clipboard operation
async.js copied to clipboard

Maximum call stack size exceeded

Open ghost opened this issue 15 years ago • 2 comments

When I used "until" with a 1000 or so iterations, node is saying "Maximum call stack size exceeded". Is there a limit to the number of iterations? My code was working and I think I've coded it properly. FWIW, here is my code.

    async.until(function() {
      return opt.eof && !buf;
    }, function(cb) {
      var delim, match, parts;
      if ((parts = regex.exec(buf))) {
        match = parts[0], delim = parts[1], buf = parts[2];
        switch (opt.delimType) {
          case 'prefix':
            opt.data = prefix + match;
            prefix = delim;
            break;
          case 'suffix':
            opt.data = match + delim;
            break;
          default:
            opt.data = match;
        }
        callback(opt);
        return stopChk(cb);
      } else if (opt.eof) {
        opt.data = prefix + buf;
        callback(opt);
        opt.stop = true;
        return stopChk(cb);
      } else {
        return fs.read(fd, inBuf, 0, opt.bufSize, null, function(err, bytesRead) {
          if (err) {
            return cb(err);
          } else {
            if (bytesRead) {
              buf += inBuf.toString('utf8', 0, bytesRead);
              return cb();
            } else {
              opt.eof = true;
              return fs.close(fd, cb);
            }
          }
        });
      }
    });
    return function(err) {
      if (!opt.stop) {
        opt.err = err;
        return callback(opt);
      }
    };
)

Coffeescript original source ...

async.until \
    -> (opt.eof and not buf),
    (cb) -> 
        if (parts = regex.exec buf)
            [match, delim, buf] = parts
            switch opt.delimType
                when 'prefix' then opt.data = prefix + match; prefix = delim
                when 'suffix' then opt.data = match + delim
                else               opt.data = match
            callback opt
            stopChk cb
        else if opt.eof
            opt.data = prefix + buf
            callback opt
            opt.stop = true
            stopChk cb
        else
            fs.read fd, inBuf, 0, opt.bufSize, null, (err, bytesRead) ->
                if err then cb err
                else
                    if bytesRead
                        buf += inBuf.toString 'utf8', 0, bytesRead
                        cb()
                    else
                        opt.eof = true; fs.close fd, cb
    (err) ->
        if not opt.stop
            opt.err = err
            callback opt

ghost avatar Jan 26 '11 00:01 ghost

Having a similar issue and would love to know if anyone has an explanation for this

brenzenb avatar Oct 12 '11 21:10 brenzenb

Stumbled upon this. There's likely another root cause, but for a band-aid, see increasing the V8 stack size options:

node --stack_size=4096 index.js

https://groups.google.com/group/nodejs-dev/browse_thread/thread/2b206819302ad5c4

CrabDude avatar Mar 07 '12 00:03 CrabDude