Zane Claes
Zane Claes

Reputation: 14954

JSON.parse causes Chrome tab to crash ("Aw, Snap") despite use of try/catch

I'm loading some JSON data from an AJAX query:

       $.ajax({'url': url, type: params.method, 'data': data, timeout: this.settings.timeout, success: function(d,a,x){
                console.log('request Complete',params.endpoint,params.params);
                var json = null;
                try {
                    json = JSON.parse(d);
                } catch(e) {
                    console.error(e);
                }
                console.log('json');

                // omitted for brevity...
            }
        });

I'm seeing occasional "Aw, Snap" crashes in chrome where the last console.log is the "request Complete" (the error or 2nd log never get shown).

I suppose that it's important to note that the data may be large (sometimes as big as ~15Mb), which is why I'm not printing out d on every request and looking for malformed JSON (yet... I may result to that). FWIW, I've also tried $.parseJSON instead of JSON.parse

Research I've done into the "Aw, Snap" error is vague, at best. My best guess atm is that this is an OOM. Unfortunately, there's not much I can do to decrease the footprint of the result-set.

Is there any way I could, at the least, gracefully fail?

Upvotes: 1

Views: 2260

Answers (2)

webdevinci
webdevinci

Reputation: 310

"@the system" nailed it. 15MB of info coming back is too much for your browser to handle. I tripped upon this trying to see why my 64 Byte encoded image string is crashing the chrome tab, and it's only 200-500KB.

To 'gracefully fail', it seems you will need to have server side logic in place to prevent this from happening; perhaps limit the size of the JSON string. Only have X characters of length in the initial JSON string and add a property for 'isFinishedSending: false' or 'leftOff: [index/some marker]' so you can timeout for a bit and hit your server again with the the place you left off.

Upvotes: 0

JAAulde
JAAulde

Reputation: 19560

What happens when you tell jQuery the response data is JSON via the dataType property? Doing so should cause jQuery to pre parse and just give you the data. If you're right about what is going on, it seems this might cause a crash too. jQuery does some sanity checks before parsing, though.

$.ajax({
    url: url,
    type: params.method,
    data: data,
    dataType: 'json',
    timeout: this.settings.timeout,
    success: function (d, a, x){
        // `d` should already be parsed into an object or array, and ready to use
    }
});

If that doesn't help, please post your actual JSON response for us to take a look at.

Upvotes: 2

Related Questions