No way to interrupt a function call? #81
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
I can't seem to find a way to interrupt a running function.
Say you have an infinite while loop within a function
And you run that function x (in my case by finding it in the bindings) by calling the
call()method on it. Is there anyway to interrupt it?I'm running the script in another thread so my UI doesn't freeze. Usually there is an
InterruptedExceptionthrown whenever the thread gets interrupted with blocking methods but the call method doesn't seem to throw such exception.I'm currently just loading a custom library extending from the
DebugLibclass with an interrupt field so whenever a new line is processed and the interrupt state is true, the lib will throw aLuaError. But I wonder if there's no simpeler solution to this?Also the above "fix" doesn't apply to infinite loops since it doesn't detect infinite loops. You'd have to interrupt the running script which might not always work.
This is a problem I tackled in my project. To solve the run-away while(true) scripts, I created my own class that extends DebugLib, and overrides the onInstruction method. In there, for a given script I keep track of how many instructions are sent through (a simple counter that does +=1).
For my purposes, 1.0e8 was enough of a threshold to throw a ScriptInterruptException, which is a custom exception that extends RuntimeException.
@orange451 I'm sure your solution works and I might implement it the same way for now but it's more of a workaround than an actual fix. If there are multiple loops or there's a lot going on or it has been running for a long time than this won't really work since it will just throw an exception after x amount instructions..
If you have multiple loops, they cant be running from the same script/thread anyways, so that isn't an issue. Just keep track of what thread is running what script.
I use this approach in my game engine, which handles quite a bit of data and a large amount of scripts running, and havent had any issues thus far.
Though you are right, that after a LONG time, you will get that exception.
For reference, if you had a script:
It would take 1666666 seconds (19 days) before you reach that exception.
@orange451 Loops can't run at the same time in a single thread indeed but what I meant is if you have multiple ("legal") loops that cause a lot of instructions and throw an exception over time.
I'm looking to implement this in a home automation project. I don't need a lot of scripts or anything, just need something reliable. I'm also not creating any other threads or allowing coroutines to be created during runtime for now.
If you come up with a better solution please share :) I'd love to see it!
@orange451 Will do :)
Instead of tracking count of instructions , I solved it by checking whether current thread is interrupted or not.
Fixed