No way to interrupt a function call? #81

Closed
opened 2020-11-29 09:29:18 +00:00 by mininotallarines · 8 comments
mininotallarines commented 2020-11-29 09:29:18 +00:00 (Migrated from github.com)

I can't seem to find a way to interrupt a running function.

Say you have an infinite while loop within a function

function x()
    while true do
        ... code
    end
end

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 InterruptedException thrown 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 DebugLib class with an interrupt field so whenever a new line is processed and the interrupt state is true, the lib will throw a LuaError. 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.

I can't seem to find a way to interrupt a running function. Say you have an infinite while loop within a function ```lua function x() while true do ... code end end ``` 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 `InterruptedException` thrown 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 `DebugLib` class with an **interrupt** field so whenever a new line is processed and the interrupt state is true, the lib will throw a `LuaError`. 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.
orange451 commented 2020-11-30 14:33:33 +00:00 (Migrated from github.com)

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.

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.
mininotallarines commented 2020-11-30 16:36:38 +00:00 (Migrated from github.com)

@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..

@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..
orange451 commented 2020-11-30 16:44:58 +00:00 (Migrated from github.com)

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:

while(true)do
    wait(1/60) -- 60 hz
end

It would take 1666666 seconds (19 days) before you reach that exception.

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: ```lua while(true)do wait(1/60) -- 60 hz end ``` It would take 1666666 seconds (19 days) before you reach that exception.
mininotallarines commented 2020-11-30 16:55:54 +00:00 (Migrated from github.com)

@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.

@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.
orange451 commented 2020-11-30 16:57:11 +00:00 (Migrated from github.com)

If you come up with a better solution please share :) I'd love to see it!

If you come up with a better solution please share :) I'd love to see it!
mininotallarines commented 2020-11-30 17:27:14 +00:00 (Migrated from github.com)

@orange451 Will do :)

@orange451 Will do :)
Way911 commented 2021-08-11 06:06:02 +00:00 (Migrated from github.com)

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.

Instead of tracking count of instructions , I solved it by checking whether current thread is interrupted or not.

> 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. Instead of tracking count of instructions , I solved it by checking whether current thread is interrupted or not.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: open-autonomous-connection/luaj#81