-
-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a flag to control execution length/time #348
Comments
One detail concerning 1.: probably that counter should be global, since it will probably be possible to write a |
Another detail concerning 1 and #349: we might want to reset the counter at every new invocation of |
An alternative to iterate-max could be an interrupt. E.g.:
This takes forever, but after 10 seconds the interrupt-state is set to Something like this could work for babashka/babashka.nrepl#23. Note that this won't work for |
Another option would to be to be able to pass a function which is called on every step and when it returns
By making it a function the user can also choose between invocation counting or a timeout or both, or something else. Regarding
This can easily be done by wrapping the evaluation in a
|
See https://stackoverflow.com/questions/11520394/why-do-cancelled-clojure-futures-continue-using-cpu why future-cancel is not a proper solution in all cases. |
@borkdude yeah I mostly meant that |
@jeroenvandijk I merged the PR #349 branch to a branch in sci and made it up to date: Right now the counter is global and that may be problematic:
Maybe we should make the counter a per-function thing? Or not explicitly use a counter at all, but a callback, so the user can do whatever they want:
I think it's best to make the invoke-callback take a map with whatever options we need in the future, e.g the function name, arguments, etc. |
@jeroenvandijk A thing that crossed my mind with this example:
Since we forbid recur, we could also forbid self-referential functions like these in But unfortunately I found an example in which that doesn't work:
So probably the simplest option is to have a (global) interruption hook/callback like above. Still needs a bit of hammock time... |
@borkdude Yeah I think I agree that there is no perfect solution for every case. A hook/callback will allow any runtime (babashka, sci in js, sci in the jvm) to make their own trade offs and solutions to this problem. So the counter i proposed in #349 is probably a less ideal solution than having a hook that would allow you to implement this same counter |
@jeroenvandijk Ok, I made a branch |
Another example which can take quite a while:
but for lower values of Another example that might not be easy to catch:
|
Another example in which neither
The expression still terminates to the name On the JVM this can be wrapped in a thread which you can forcibly kill. On the JS side I'm not certain what's a more fundamental way to solve this. |
Another example in https://babashka.org/xterm-sci/:
|
I just checked this example |
As for |
For now I removed support for :realize-max and :preset termination-safe via #429. |
@jeroenvandijk I don't know if you are using SCI on Node.js but there I found a proper solution for this problem, using You can see this in action when you type |
@borkdude Not using Sci on Node.js yet, but this looks interesting. Thanks for the heads up! |
Problem statement
Sci cannot detect when it is running code that will run forever. Clojure cannot do this either, but the flag
:termination-safe
suggests that Sci is able to do this. A counter example is:This code takes more than 6 seconds with
n=30
via babashka. Withn=100
this code will practically never finish.Why does this matter?
A Babashka user is fully aware of what is being executed and will therefore be responsible for the code that is being run. If it takes to long, one CTRL-C will kill the program. However when you use Sci to run third party code in your JVM or in a web browser things start to change. Think of the following examples:
In the JVM example one can use an approach like Clojail to work around this. In browser there are no threads and this becomes a different story. One needs to hack with webworkers to make it safe (not sure how succesful this will be). In both cases it would be cleaner if Sci had an option to protect us against endless execution. It would allow us to provide better error messages and remove complexity in the hosting code.
Potential solution(s)
Other requirements of potential solution
When the safety flag is turned off it should not affect performance.
Alternatives to control execution time
Open questions
Other examples
Background of this issue can be found here https://clojurians-log.clojureverse.org/babashka/2020-06-10/1591808634.003100
The text was updated successfully, but these errors were encountered: