Optional interruption support#1039
Open
whilo wants to merge 4 commits intobabashka:masterfrom
Open
Conversation
Adds an optional :resource-check function to SCI contexts. When provided, it is called on every loop/recur iteration and every fn-call dispatch. The function can throw to abort execution. This enables: - Iteration limits (prevent infinite loops) - Memory limits (via JVM getThreadAllocatedBytes in the callback) - External timeout (via Thread.interrupted check in the callback) - Per-context bounds (different sandboxes get different limits) The check is cached at function creation time (not looked up per call). When nil (default), zero overhead — the `when` branch is never taken. Overhead with amortized check (every 10K iterations): ~2.5x on tight loops. With every 100K: ~10%. For real workloads: negligible. Changes: - opts.cljc: add :resource-check field to Ctx record, wire through init - fns.cljc: call resource-check on each recur in gen-fn macro - evaluator.cljc: call resource-check on fn-call dispatch
- Rename the option to :interrupt-fn throughout (opts, fns, evaluator) - Move the check to the top of gen-fn's loop: fires on every function entry (initial call) AND every recur, covering direct recursion, mutual recursion, dotimes, while, and loop/recur uniformly - Remove the check from fn-call: it was only reachable for 20+ arg calls (gen-return-call generates direct (f ...) calls for 0-19 args), so the original placement was both insufficient and added overhead for existing users — now fn-call is truly zero-cost for nil :interrupt-fn - Add rc# capture to arity-many (20+ arg) fallback in gen-fn - Fix merge-opts to preserve :interrupt-fn when not overridden - Add interrupt_fn_test.cljc covering recur, dotimes, direct recursion, mutual recursion, nil default, fork, and merge-opts propagation
…ll/dorun/count/into/reduce When :interrupt-fn is provided, opts/init installs interruptible versions of nine clojure.core functions that would otherwise bypass the interrupt mechanism by running entirely host-side: Producers: range, repeat, cycle, iterate Materializers: doall, dorun, count, into, reduce Each wrapper calls store/get-ctx at invocation time to read :interrupt-fn, so fork and merge-opts work correctly. When :interrupt-fn is absent the original host functions are used unchanged — zero overhead for existing users. counted? collections (vectors, maps, sets) take the fast O(1) path in count. reduce supports reduced for early termination.
borkdude
reviewed
Apr 17, 2026
| #_`(defn ~'fn-call ~'[ctx f args] | ||
| (apply ~'f (map #(eval ~'ctx %) ~'args))) | ||
| `(defn ~'fn-call ~'[ctx bindings f args] | ||
| ;; TODO: can we prevent hitting this at all, by analyzing more efficiently? |
Collaborator
There was a problem hiding this comment.
Please don't remove anything unrelated to the PR
borkdude
reviewed
Apr 17, 2026
| ~@(when varargs | ||
| [`(aset ~'invoc-array ~'vararg-idx ~varargs-param)]) | ||
| (loop [] | ||
| (when interrupt-fn# (interrupt-fn#)) |
Collaborator
There was a problem hiding this comment.
(when (some? interrupt-fn#) ...) seems to be faster on CLJS. Since this is on a hot path let's use that instead.
Collaborator
There was a problem hiding this comment.
The same goes for all other places.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
:interrupt-fncallback for per-context execution bounding (fixes #1038).An optional
:interrupt-fnin the SCI context options is called on every function entry. The function can throw to abort execution, enabling iteration limits, memory limits, and external timeout checks — independently per context.The check is captured as a closed-over local at function-creation time, so it is a free nil test when not configured: zero overhead for existing users.
Coverage:
loop/recur,dotimes,while, direct self-calls, mutual recursion.merge-optsandforkpreserve the callback.I have read the developer documentation.
This PR corresponds to an issue with a clear problem statement.
This PR contains a test to prevent against future regressions.