no-stack-dub-sack / cs-playground-react

In-Browser Algorithm and Data Structures Practice
http://cs-playground-react.surge.sh/
MIT License
520 stars 91 forks source link

Error (stack exhausted, infinite loop, reference error etc.) silences console.log #48

Closed btruhand closed 6 years ago

btruhand commented 6 years ago

When an error is caught it seems that user defined console.log statements are rendered mute. That is:

function infinite() {
  console.log("This won't be shown in console");
  while (true) {}
}

infinite();

Running the above would cause the console statement to not display the string. Instead you will only get:

Error: Timed out after 500ms due to infinite loop on line 3

Having console log statements can be very helpful during any sort of quick debugging

no-stack-dub-sack commented 6 years ago

@btruhand Yes... this is a good point, and a decision I had a bit of trouble with.

The problem, is here and here. In order to loop-protect the code, I was stuck in a situation where basically I have to eval the code twice. I also made a note about this here.

You would think that I'd be able to get rid of the surrounding if block add the loop-protect transform directly right here, without having the intermediate function wontTimeout which evals the code first to check for infinite loops (and silences the console), like this:

import wontTimeout, { loopProtect } from './loop-protect'

// ... other code from eval-code-run-tests.js

 // if (wontTimeout(code)) {
    try {
      eval(
        loopProtect(code) +
        tail +
        tests +
        executeTests
      )
    } catch (e) {
      console.log(e.toString())
    }
//  }

However, when I did this (my first try), it would work correctly when an infinite-loop was detected, but when there was no loop in the user's code, it would cause some very strange behavior which I was totally unable to explain (basically the test-runner would think that there was 1000s tests, and it would loop over those tests and log them all as failing, basically creating an infinite loop that didn't exist!).

So... I silenced the console in the first eval to prevent everything from being logged twice. I suppose I could do some kind of regexp test to check for the presence of loops and only transform the code if they exist, but I haven't tested this. I do see how silencing the console could be a disadvantage though.

no-stack-dub-sack commented 6 years ago

Oh, and this should only be for infinite loops, when other errors are caught, it should still log any messages prior to the error stopping exection of the code

no-stack-dub-sack commented 6 years ago

☝️@btruhand this PR fixes things, but it's a bit of a hack! Also removes another hack, so I guess it all evens out 😕