question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Node.js sample results in "RangeError: Maximum call stack size exceeded"

See original GitHub issue

Hi all.

I’ve run into an Exception using the node.js sample provided on the 0.15.0 release, which I think is a bug.

It happens on a recursive loop function, using setImmediate, process.nextTick or a setTimeout wrapper.

The following scripts are simple ways of replicating my problem. This script:

var petStoreApi = require('./petStoreSampleApi.js');
var msRest = require('ms-rest');
var credentials = new msRest.TokenCredentials("special-key");
var petStoreClient = new petStoreApi(credentials);

var petId = 1;

function loop( i, callback ) {
    if( i < 10000 ) {
        petStoreClient.getPetById(petId, function(err, result, httpReq, response) {
            console.log(i, new Date());
            process.nextTick( function() {
                loop( i+1, resume ); 
            });
        });
    } else {
        callback();
    }
}

console.log('start at ', new Date());
loop( 0, function() {
    console.log('end at ', new Date());
});

results in:

marcelo@local:~/opt/autorest$ node getPetById-github-issue-code-used.js 
start at  Tue Apr 05 2016 14:26:33 GMT-0300 (BRT)
0 Tue Apr 05 2016 14:26:34 GMT-0300 (BRT)
1 Tue Apr 05 2016 14:26:34 GMT-0300 (BRT)
(...)
4951 Tue Apr 05 2016 15:02:21 GMT-0300 (BRT)
4952 Tue Apr 05 2016 15:02:22 GMT-0300 (BRT)
url.js:426
  if (search && search.charAt(0) !== '?') search = '?' + search;
                                                       ^
RangeError: Maximum call stack size exceeded
    at String.replace (native)
    at Url.format (url.js:428:23)
    at Url.parse (url.js:365:20)
    at Object.urlParse [as parse] (url.js:104:5)
    at Request.init (/opt/autorest/node_modules/ms-rest/node_modules/request/request.js:250:20)
    at new Request (/opt/autorest/node_modules/ms-rest/node_modules/request/request.js:141:8)
    at request (/opt/autorest/node_modules/ms-rest/node_modules/request/index.js:55:10)
    at /opt/autorest/node_modules/ms-rest/node_modules/request/index.js:101:12
    at /opt/autorest/node_modules/ms-rest/node_modules/request/index.js:101:12
    at /opt/autorest/node_modules/ms-rest/node_modules/request/index.js:101:12

However when I use only request it works pretty well. Here is the working sample:

var request = require('request');

var simpleRequestPetById = function(petId, callback) {
    if (!callback) {
        throw new Error('callback cannot be null.');
    }

    var reqUrl = 'http://petstore.swagger.io/v2/pet/' + petId;
    var reqOptions = {
        method: 'GET',
        uri: reqUrl,
        headers: {
            Authorization: 'Bearer special-key'
        }
    };
    request(reqOptions, function(error, response, body) {
        if (error) {
            callback(error);
        } else {
            if (body) {
                callback(null, response, body);
            } else {
                callback(null, response);
            }
        }
    });
}

var petId = 1;

function loop( i, callback ) {
    if( i < 10000 ) {
        simpleRequestPetById(petId, function(err, result) {
            console.log(i, new Date());
            process.nextTick( function() {
                loop( i+1, callback ); 
            });
        });
    } else {
        callback();
    }
}

console.log('start at ', new Date());
loop( 0, function() {
    console.log('end at ', new Date());
});

results in:

marcelo@local:~/opt/autorest$ node getPetById-github-issue-working-code.js 
start at  Tue Apr 05 2016 14:52:38 GMT-0300 (BRT)
0 Tue Apr 05 2016 14:52:39 GMT-0300 (BRT)
1 Tue Apr 05 2016 14:52:39 GMT-0300 (BRT)
2 Tue Apr 05 2016 14:52:39 GMT-0300 (BRT)
3 Tue Apr 05 2016 14:52:40 GMT-0300 (BRT)
(...)
9998 Tue Apr 05 2016 15:25:00 GMT-0300 (BRT)
9999 Tue Apr 05 2016 15:25:01 GMT-0300 (BRT)
end at  Tue Apr 05 2016 15:25:01 GMT-0300 (BRT)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mcoimbraccommented, May 23, 2016

Hi @amarzavery!

As far as I can tell, the request was made and the autorest client was able to successfully get the pet data from the petstore serve.

I tested the new autorest version (0.16.0). And since you’ve pointed the credentials as a possible source of problems, I took the liberty to take it out of equation and make things simpler.

I created a public repository with the source code used to test (https://github.com/mcoimbrac/autorest-sample), in case you want to take a look at what we used.

The code is pretty much the same, just added more logs and removed credentials.

var petStoreApi = require('./swaggerPetstore.js');
var msRest = require('ms-rest');
// var credentials = new msRest.TokenCredentials("special-key");
var petStoreClient = new petStoreApi();

var petId = 1;

function loop( i, resume ) {
    if( i < 10000 ) {
        petStoreClient.getPetById(petId, function(err, result, httpReq, response) {
            if (err) console.log(err);
            if (result) console.log(result);

            console.log(i, new Date());
            process.nextTick( function() {
                loop( i+1, resume ); 
            });
        });
    } else {
        resume();
    }
}

console.log('start at ', new Date());
loop( 0, function() {
    console.log('end at ', new Date());
});

And it also resulted in:

marcelo@local:/opt/autorest$ node getPetById-github-issue-code-used-no-credentials.js 
start at  Mon May 23 2016 16:36:22 GMT-0300 (BRT)
{ id: 1, name: 'Tom', photoUrls: [ 'http://test' ], tags: [] }
0 Mon May 23 2016 16:36:22 GMT-0300 (BRT)
{ id: 1, name: 'Tom', photoUrls: [ 'http://test' ], tags: [] }
(...)
{ id: 1, name: 'Tom', photoUrls: [ 'http://test' ], tags: [] }
6231 Mon May 23 2016 17:27:57 GMT-0300 (BRT)
{ id: 1, name: 'Tom', photoUrls: [ 'http://test' ], tags: [] }
6232 Mon May 23 2016 17:27:57 GMT-0300 (BRT)
url.js:413
  pathname = pathname.replace(/[?#]/g, function(match) {
                      ^

RangeError: Maximum call stack size exceeded
    at String.replace (native)
    at Url.format (url.js:413:23)
    at Url.parse (url.js:344:20)
    at Object.urlParse [as parse] (url.js:84:5)
    at Request.init (/opt/autorest/node_modules/ms-rest/node_modules/request/request.js:250:20)
    at new Request (/opt/autorest/node_modules/ms-rest/node_modules/request/request.js:141:8)
    at request (/opt/autorest/node_modules/ms-rest/node_modules/request/index.js:55:10)
    at /opt/autorest/node_modules/ms-rest/node_modules/request/index.js:101:12
    at /opt/autorest/node_modules/ms-rest/node_modules/request/index.js:101:12
    at /opt/autorest/node_modules/ms-rest/node_modules/request/index.js:101:12
0reactions
sedouardcommented, Aug 10, 2016

Hey @amarzavery this issue is really hurting us a lot.

I’ve solved the issue and opened a PR.

https://github.com/Azure/autorest/pull/1342

Read more comments on GitHub >

github_iconTop Results From Across the Web

Node.js - Maximum call stack size exceeded
js throws a "RangeError: Maximum call stack size exceeded" exception caused by too many recursive calls. I tried to increase Node.js stack size ......
Read more >
JavaScript Error: Maximum Call Stack Size Exceeded
If you see the “Maximum Call Stack Size Exceeded” error, there's likely a problem with a recursive function within your JavaScript code. More ......
Read more >
Deal With “Maximum Call Stack Size Exceeded” in JavaScript
This happens because when you use the spread operator, all elements of the source array are stored in the stack and added to...
Read more >
RangeError: Maximum call stack size exceeded in JavaScript
The "RangeError: Maximum call stack size exceeded" error occurs when a function is called so many times that the invocations exceed the call...
Read more >
RangeError: Maximum call stack size exceeded
Hello. I'm encountering this error -- "RangeError: Maximum call stack size exceeded" -- when I run a node.js app that dynamically generates a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found