[SERVER-8308] Javascript shell max call stack exceeded on deeply nested json object Created: 23/Jan/13  Updated: 05/Apr/17  Resolved: 29/Nov/16

Status: Closed
Project: Core Server
Component/s: JavaScript
Affects Version/s: None
Fix Version/s: 3.5.1

Type: Bug Priority: Minor - P4
Reporter: Jared Rosoff Assignee: Matt Cotter
Resolution: Done Votes: 0
Labels: platforms-re-triaged
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to SERVER-8309 Benchrun crashes if passed deeply nes... Closed
Backwards Compatibility: Minor Change
Operating System: ALL
Steps To Reproduce:

MongoDB shell version: 2.3.3-pre-
connecting to: test
Server has startup warnings:
Wed Jan 23 14:52:39.843 [initandlisten]
Wed Jan 23 14:52:39.844 [initandlisten] ** NOTE: This is a development version (2.3.3-pre-) of MongoDB.
Wed Jan 23 14:52:39.844 [initandlisten] ** Not recommended for production.
Wed Jan 23 14:52:39.844 [initandlisten]
Wed Jan 23 14:52:39.844 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
Wed Jan 23 14:52:39.844 [initandlisten]
> var doc = {}
> var cur = doc;
> for( var i = 0; i< 65536; i++ ) {
... cur[i] = {}
... cur = cur[i];
... }
{ }
> cur['a'] = 'foo'
foo
> doc
Wed Jan 23 15:00:51.265 javascript execution failed src/mongo/shell/types.js:527 RangeError: Maximum call stack size exceeded
tojsonObject = function(x, indent, nolint){
^

>

Sprint: Platforms 2017-01-23
Participants:

 Description   

trying to create a deeply nested json document. shell gives error when trying to display.



 Comments   
Comment by Githook User [ 30/Nov/16 ]

Author:

{u'username': u'benety', u'name': u'Benety Goh', u'email': u'benety@mongodb.com'}

Message: SERVER-8308 fixed lint
Branch: master
https://github.com/mongodb/mongo/commit/575d2d2596db653fd46c275154f41c393bafc293

Comment by Githook User [ 29/Nov/16 ]

Author:

{u'username': u'Machyne', u'name': u'Matt Cotter', u'email': u'matt.cotter@mongodb.com'}

Message: SERVER-8308 limit tojson call depth
Branch: master
https://github.com/mongodb/mongo/commit/2b8fe6c13cb08c8de2e1cd622ca8456d0f08caab

Comment by Matt Cotter [ 28/Nov/16 ]

Looks like node handles this similarly to how I wanted to:

> nestobj(100)
{ '0': { '1': { '2': [Object] } } }
> nestarr(100)
[ [ [ [Object] ] ] ]

Where the functions are defined as follows

const nestobj = (up) => {
    let doc = {};
    let cur = doc;
    for (let i = 0; i < up; i++) {
        cur[i] = {};
        cur = cur[i];
    }
    cur['a'] = 'foo';
    return doc
};
 
const nestarr = (up) => {
    let doc = [0];
    let cur = doc;
    for (let i = 0; i < up; i++) {
        cur[0] = [0];
        cur = cur[0];
    }
    cur[0] = 'foo';
    return doc;
};

Comment by Matt Cotter [ 28/Nov/16 ]

This is the same issue as

$ mongo --nodb
MongoDB shell version v3.4.0
> arr = [1,2]
[ 1, 2 ]
> arr[1] = arr
2016-11-28T14:54:09.536-0500 E QUERY    [main] InternalError: too much recursion :
tojsonObject@src/mongo/shell/types.js:668:9
...

I will try just truncating for now and seeing what happens.

FWIW the spidermonkey shell chokes on this too basically in the same way.
My browser handles these as separate cases, but still fails:

Uncaught TypeError: Converting circular structure to JSON
    at JSON.stringify (<anonymous>)
    at <anonymous>:1:6

vs.

Uncaught RangeError: Maximum call stack size exceeded
    at JSON.stringify (<anonymous>)
    at <anonymous>:1:6

Comment by Tad Marshall [ 24/Jan/13 ]

Similar, but slightly different, result from SpiderMonkey:

MongoDB shell version: 2.3.3-pre-
> var doc = {}
> var cur = doc;
> for( var i = 0; i< 65536; i++ ) { cur[i] = {} ; cur = cur[i]; }
{ }
> cur['a'] = 'foo'
foo
> doc
Wed Jan 23 20:25:30.280 InternalError: too much recursion src/mongo/shell/types.js:513
>

Generated at Thu Feb 08 03:17:05 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.