Node.js sample results in "RangeError: Maximum call stack size exceeded"
See original GitHub issueHi 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:
- Created 7 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
And it also resulted in:
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