XojoScript and infinite loops

I’m trying to detect and stop a XojoScript run if it contains an infinite loop of some kind.

I tried putting it in a secondary thread and watching its elapsed run time. However, it would seem that such a XojoScript blocks all other threads and the app just seizes up.

Any suggestions?

Don’t put in infinite loops? :slight_smile:

I won’t, but end-users are the ignorant buttheads who might. This is a web app. I don’t want them to intentionally or unintentionally bring the entire app to its knees.

Are you letting users enter their own xojoscript? if so, a general solution to the problem is impossible, and is a major theorem of computer science: https://en.wikipedia.org/wiki/Halting_problem

As a practical solution, you could probably spin off a second executable to run the xojoscript and then kill it if it’s running for too long. But you can’t do that in the same process, I believe.

It’s just a pity XojoScript.run doesn’t yield to other threads.

Michael beat me to it. :slight_smile:

Just chatted with @Paul Lefebvre and he offered something that might do it. Have the script run in a Context module with a YieldToNextThread method that calls App.YieldToNextThread.

Here’s my addition: when your users post a script, intervene and insert that call before any line that starts with Next, Wend or Loop.

Here’s a RegEx for it:

rx.SearchPattern = "^[\\x20\\t]*(Next|Loop|Wend)\\b"
rx.ReplacementPattern = "YieldToNextThread\
\\0"

Edit: Tweaked the pattern a bit.

If you prefer, the module can keep a counter so it only actually calls App.YieldToNextThread every x times it’s called.

Edit: Or every m ticks (or milliseconds).

I keep saying “Module”, but of course, it’s a Class.

Anyway, I just tested this and it works perfectly.

Excellent. Thanks. Should prolly add “goto” to that list for evil people who don’t know any better and use backwards gotos in their code.

Or reject any script that contains a line that starts with Goto, and, for good measure, disables or outright deletes that user’s account, blocks them from any further access to your app, web site, or company, then sends them a nasty letter (postage-due, of course).

goto has uses … few but they exist
otherwise you write really contorted code to avoid them
there were still one or two in the IDE last I looked

[quote=459565:@Paul Rodman]Paul Rodman 6 hours ago Pre-Release Testers, Xojo Pro Kirkland, WA
It’s just a pity XojoScript.run doesn’t yield to other threads.[/quote]

But I think it does yield to timer calls. I have used that in the past to update the interface during long XojoScripts.

Then a timer could be used to kill the XojoScript after a given delay. But the idea to launch a new copy of the app exclusively for XojoScript is probably the best, to avoid the main app lock.

Are you proposing to use a shell here, or to launch a thread containing the XojoScript?

You could have an app dedicated to XojoScript, which would be launched from a FolderItem.Launch in API 1 or a shell in API 2, and provide the name of the file containing the XojoScript source. The user is presented with that app’s screen from then on.

That app can have a TimeOut timer to kill itself after a preset period, to avoid infinite loops, which would return the user to the calling app, and then quit.