Khan / live-editor

A browser-based live coding environment.
Other
764 stars 183 forks source link

There should be an option to disable infinite loop protection so slower laptops can run larger programs #446

Open brew4321 opened 9 years ago

brew4321 commented 9 years ago

I understand the need for infinite loop protection, I remember before there was any loosing some programs because I wrote the wrong code and my program spiraled into an infinite loop and the browser just crashed so hard I couldn't even save my work... that sucks for sure, but on the flip side the current level of protection tends to kill legit programs if they get to large, especially for slower laptops like mine! Would it be possible to have a line of code that is able to disable the protection? So for example you could define some kind of bool like KAinfiniteloops and make all that stuff depend on the bool, and then give us access to it so all we would have to do is write KAinfiniteloops=false; at the top of a program.

kevinbarabash commented 9 years ago

I'm really leery about providing an option to disable this b/c it could be abused to create malicious programs which intentionally freeze the browser.

brew4321 commented 9 years ago

Oh, good call I didn't even think about that... Just brain storming here, but what if instead of a line of code it was a button on the error message with a warning about malicious programs? So if you clicked a link to a malicious program you'd still have to click a button to actually crash the browser, and at that point you would be able to see the code, and other peoples comments if they exist.

codeHusky commented 9 years ago

I'd just suggest moving your program to the webpage environment. Works much better for speed.

lgto4 commented 9 years ago

@brew4321: Amen to that. The mechansim need not be a boolean. In my ideal world it would be a realtime value - "I expect my compute intesive program to consume up to 1234 milliseconds of real time in the draw loop." Our overlords can constrain that parameter to something reasonable like 5000.

Maybe we can get together and buy them a four year old Intel Atom to test this and other enhancements on...

benburrill commented 9 years ago

@MikaalSky Perhaps this should be a separate issue, but I've found that the webpage environment is far more liberal at giving out infinite loop warnings. It could just be how the environment is used and I haven't looked into this very well, but it really seems like the most minimal loops eventually cause the program to freeze (although not usually immediately).

codeHusky commented 9 years ago

Just having a switch on the page to axe it wouls be fine but having a URL param would destroy everything.

kevinbarabash commented 9 years ago

I'd like to eventually get away from the kind of loop protecting that we're doing because it's very rigid. I'd like to eventually have each function be a generator and have each of them yield at least at the start and inside each loop. yield-ing allows us to escaping from running user code and re-enter at exactly the same position. Each time we yield we could check how much time has elapsed and if enough time has passed we could present a dialog asking if the user wants to continue running the program. We could re-enter the program in a setTimeout callback so that we allow other items on the event queue to be processed.

TL;DR: we can use generators and yield to keep the browser responsive while running long running programs and we can resume running of a program after asking the user if they want to continue.

benburrill commented 9 years ago

That would be amazing, except how would you deal with user defined generators? Because I like generators a lot and would want to use them frequently if they ever stopped throwing esprima? errors when you try to use them in the pjs code environment.

kevinbarabash commented 9 years ago

@Potato42 live-editor currently doesn't support generators or any ES6 language features yet. Once we get there I'm sure we'll be able to come up with some way to differentiate them via some sort of AST transform.

ghost commented 8 years ago

I like this idea. Maybe have something where if the program crashes, give EB a special message saying "I think this program may never stop. Click me if you want to keep trying." There could also be a <select> on the page where you could set how many more seconds you want the loop to run for: 1, 2, 5, 10, 30, 60.

kevinbarabash commented 8 years ago

We could even add a pause button to the UI so that users wouldn't have to choose a timeout a priori.

kevinbarabash commented 8 years ago

I did try the yield-ing solution described above, but the performance was awful.

I'm experimenting now with using console.prompt as a way to pausing the program and allowing the user to decide whether they want to continue or not. Each time you continue, it doubles the timeout. Give it a try and let me know what you think: http://kevinbarabash.github.io/live-proxy/.

ghost commented 8 years ago

I don't understand how to work the live-proxy.

kevinbarabash commented 8 years ago

@DozenalFTW you should be able to paste from a processing-js program on khanacademy.org into the live-proxy editor. Is it not working for a particular program?

kevinbarabash commented 8 years ago

I should also mention that you'll need to use a browser that supports most of ES6. I'm on Chrome 50. I will be fixing this eventually by adding compile infrastructure with webpack+babel.

brew4321 commented 8 years ago

Hey @kevinbarabash I'm not sure if I'm doing it wrong either, but I couldn't get it to do anything.

I went to the link. Then by right clicking and doing inspect element I saw that there was an ace editor on the left and a canvas to the right. Then I just started typing in the editor.

I tried out this:

var draw = function(){
    background(200,0,0);
};

and just to verify I went to a new project over on khan academy, and yep turns the canvas red, but on your live proxy site the canvas just remains gray?

kevinbarabash commented 8 years ago

@brew4321 the code you posted works. Make sure you're running Chrome 50. I'll work on better browser support so that more people can try it on more browsers.

brew4321 commented 8 years ago

Ok I just went to the link with chrome and I see a ball moving in a circle. Are we supposed to see each others work there?

kevinbarabash commented 8 years ago

That ball that's moving is a test program that I'm using in development. The demo is completely static so don't worry about overwriting anyone's work.

brew4321 commented 8 years ago

Ok cool... tried out a program with images and got all kinds of "getImage" is not defined on line whatever errors. Guess you have yet to implement that function?

brew4321 commented 8 years ago

Well I tried out a large program with lots of loops and at the same time made my computer do a bunch of other stuff to lag it down, and saw the pop up: popup error for lag It definitely works! I got it to pop up several times for that program and kept the browser responsive by pressing ok. One thing I noticed was that if I was on another tab Chrome brought me back to the lagged program, and wouldn't let me ignore the popup/ go back to the other tabs. You'd probably want to allow them to ignore the popup in case they wanted to go finish working on something in the other tab so they can close it so the lagged program would have more resources, but I imagine it is more a feature of using console.prompt.

kevinbarabash commented 8 years ago

@brew4321 thanks for the feedback. There's not much that can done the behavior of console.prompt. I should probably add some logic to check to disable/enable the loop protector when navigating to/from another tab.

As for the getImage issue, I've opened https://github.com/kevinbarabash/live-proxy/issues/26.

kevinbarabash commented 8 years ago

@brew4321 can you post links to the programs you tried out?

brew4321 commented 8 years ago

This https://www.khanacademy.org/computer-programming/knockit-tanks-wip/4949493379497984 is the program that has all the loops that caused the prompt. This https://www.khanacademy.org/computer-programming/3d-maze-game/5316019273465856 one crashed in mozila but stayed active in live proxy with out throwing a error, but it seemed to contribute to throwing the errors on the tank game. I'd switch over to the maze and it would lag the tank game, pulling me back as described above. This is what I had in the other tab: https://www.khanacademy.org/computer-programming/recursive-sword-storm/5500280576933888 and it also wouldn't throw an error but lagged the tank game. Also there seemed to be something off with the graphics on the swords animation, but I couldn't quite put my finger on what it was. :/

kevinbarabash commented 8 years ago

Thanks, this is super helpful. It looks like noStroke isn't being handle correctly in the sword animation. If you find anymore issues live-proxy please open them in that repo.

brew4321 commented 8 years ago

Ok, will do!

JohnyDL commented 6 years ago

I hate to necro something 2 years old but I'm hitting the wall with the infinite loop protection, I've got a few programs that do long or really nested loops but they do keep going, One is my Sudoku Solver and another is a sand-pile simulator which I'd like to work on per pixel basis but I'm currently running at a ~40x40 resolution because I'm not able to push it any higher.

I don't mind the loop protection it can be quite useful but if I tell the program I expect a frame rate about 1/second then maybe the infinite loop thing should kick in at 2 seconds. With maybe some override to a max of 10 seconds of infinite loop limit when testing other people's programs and not your own, so they can't troll you unless you choose to let it run.

I'd also like to see that when the "a loop is taking too long" error message pops up there's a button saying "I know keep working anyway" so even if I hit that wall I can, if I want to be persistent, override it one loop limit at a time and ignore it