Liking cljdoc? Tell your friends :D

hive-weave.safe

Safe execution primitives — the antidote to bare @ and raw future.

Every bare @(future ...) or @(promise) is a potential hang. This namespace provides bounded alternatives that always terminate:

  • deref-safe — deref with timeout + fallback (never hangs)
  • deref-safe! — deref with timeout, throws on timeout (never hangs)
  • safe-future — future with timeout + Result return
  • safe-future! — future with timeout, throws on timeout

All primitives return within their timeout budget. No exceptions.

ClojureScript note: node is single-threaded, so there is no thread to interrupt and no blocking timed deref. The :cljs branches run the thunk to completion and treat the timeout as advisory — safe-future-call never answers :weave/timeout there, it answers the value or :weave/exception. The cancel token still exists (*cancel*, a reified IDeref over an atom) so cooperative work keeps one shape across hosts, and :pool is accepted but unused until pool.cljc is ported: there is no executor to submit to.

Safe execution primitives — the antidote to bare @ and raw future.

Every bare `@(future ...)` or `@(promise)` is a potential hang.
This namespace provides bounded alternatives that always terminate:

- `deref-safe`  — deref with timeout + fallback (never hangs)
- `deref-safe!` — deref with timeout, throws on timeout (never hangs)
- `safe-future`  — future with timeout + Result return
- `safe-future!` — future with timeout, throws on timeout

All primitives return within their timeout budget. No exceptions.

ClojureScript note: node is single-threaded, so there is no thread to
interrupt and no blocking timed deref. The :cljs branches run the thunk
to completion and treat the timeout as advisory — `safe-future-call`
never answers :weave/timeout there, it answers the value or
:weave/exception. The cancel token still exists (`*cancel*`, a reified
IDeref over an atom) so cooperative work keeps one shape across hosts,
and `:pool` is accepted but unused until pool.cljc is ported: there is
no executor to submit to.
raw docstring

*cancel*clj/s

Cancel token of the running safe-future-call task, or nil outside one.

An IDeref: @cancel is nil while the caller still waits and true once the caller timed out, was interrupted or otherwise abandoned the call. Cooperative work polls it (see cancelled? and check-cancel!) or hands it to a library that accepts an IDeref cancel flag.

Cancel token of the running safe-future-call task, or nil outside one.

An IDeref: @*cancel* is nil while the caller still waits and true once the
caller timed out, was interrupted or otherwise abandoned the call.
Cooperative work polls it (see `cancelled?` and `check-cancel!`) or hands
it to a library that accepts an IDeref cancel flag.
sourceraw docstring

cancel-tokenclj/s

(cancel-token)

A fresh, un-cancelled token: [token cancel!]. token is an IDeref that answers nil until (cancel!) runs, true after.

A fresh, un-cancelled token: [token cancel!]. `token` is an IDeref that
answers nil until `(cancel!)` runs, true after.
sourceraw docstring

cancelled?clj/s

(cancelled?)

True when the current safe-future-call task was abandoned by its caller, or the current thread is interrupted. On single-threaded node only the token can answer — there is no thread to interrupt.

True when the current safe-future-call task was abandoned by its caller,
or the current thread is interrupted. On single-threaded node only the
token can answer — there is no thread to interrupt.
sourceraw docstring

check-cancel!clj/s

(check-cancel!)

Throw (ex-info ... {:weave/cancelled true}) when cancelled?. A cheap poll for loops inside a safe-future-call task.

Throw (ex-info ... {:weave/cancelled true}) when `cancelled?`. A cheap poll
for loops inside a safe-future-call task.
sourceraw docstring

deref-cancellingclj/s

(deref-cancelling fut timeout-ms cancel!)

Deref fut up to timeout-ms, answering its value or ::timed-out. When the deref does not complete (timeout, caller interrupt, any throw) run (cancel!) and future-cancel fut before returning or rethrowing.

ClojureScript: the deref is synchronous, so nothing can abandon it — the timeout is advisory and ::timed-out is unreachable. A throw still runs (cancel!) before propagating.

Deref `fut` up to `timeout-ms`, answering its value or ::timed-out. When the
deref does not complete (timeout, caller interrupt, any throw) run
`(cancel!)` and future-cancel `fut` before returning or rethrowing.

ClojureScript: the deref is synchronous, so nothing can abandon it — the
timeout is advisory and ::timed-out is unreachable. A throw still runs
`(cancel!)` before propagating.
sourceraw docstring

deref-safeclj/s

(deref-safe ref timeout-ms fallback)

Deref with timeout and fallback. Never hangs.

(deref-safe my-promise 5000 []) ;; => value or [] after 5s (deref-safe my-future 10000 nil) ;; => value or nil after 10s

Deref with timeout and fallback. Never hangs.

(deref-safe my-promise 5000 [])       ;; => value or [] after 5s
(deref-safe my-future 10000 nil)      ;; => value or nil after 10s
sourceraw docstring

deref-safe!clj/s

(deref-safe! ref timeout-ms)

Deref with timeout. Throws on timeout. Never hangs indefinitely.

(deref-safe! my-promise 5000) ;; => value or throws after 5s

Deref with timeout. Throws on timeout. Never hangs indefinitely.

(deref-safe! my-promise 5000)    ;; => value or throws after 5s
sourceraw docstring

safe-futureclj/smacro

(safe-future opts & body)

Execute body in a future with timeout. Returns Result.

(safe-future {:timeout-ms 5000} (expensive-computation)) ;; => (ok result) or (err :weave/timeout {...})

Execute body in a future with timeout. Returns Result.

(safe-future {:timeout-ms 5000}
  (expensive-computation))
;; => (ok result) or (err :weave/timeout {...})
sourceraw docstring

safe-future-callclj/s

(safe-future-call {:keys [timeout-ms name pool] :or {name "anonymous"}} f)

Execute f with a timeout. Returns Result.

(safe-future-call {:timeout-ms 5000} #(expensive-computation)) (safe-future-call {:timeout-ms 5000 :pool io-pool} #(slow-read))

Options: :timeout-ms — max execution time (required) :name — diagnostic label (optional) :pool — ExecutorService to run f on (optional, JVM only)

Cancellation: whenever the deref does not complete — timeout, an interrupt of the calling thread (e.g. the caller's own future-cancel), or any throw — the task's Future is cancelled with interrupt (a queued task never starts) and its cancel token flips. Inside f, *cancel* is that token; poll it with cancelled? / check-cancel! so work that ignores interrupts can stop too. A caller interrupt rethrows the InterruptedException after cancelling.

Without :pool, f runs on clojure.core/future, whose pool is UNBOUNDED. Work that neither observes interruption nor polls the token keeps its thread; N timed-out calls hold N threads. Pass a bounded :pool wherever timeouts are expected, and the ceiling is the pool instead of the heap. A saturated :abort pool returns (err :weave/rejected ...) rather than running the work on the caller.

ClojureScript: f runs synchronously on the caller and the timeout is advisory (see the ns docstring); :pool is accepted but unused.

Execute f with a timeout. Returns Result.

(safe-future-call {:timeout-ms 5000} #(expensive-computation))
(safe-future-call {:timeout-ms 5000 :pool io-pool} #(slow-read))

Options:
  :timeout-ms — max execution time (required)
  :name       — diagnostic label (optional)
  :pool       — ExecutorService to run f on (optional, JVM only)

Cancellation: whenever the deref does not complete — timeout, an interrupt
of the calling thread (e.g. the caller's own future-cancel), or any throw —
the task's Future is cancelled with interrupt (a queued task never starts)
and its cancel token flips. Inside f, `*cancel*` is that token; poll it with
`cancelled?` / `check-cancel!` so work that ignores interrupts can stop too.
A caller interrupt rethrows the InterruptedException after cancelling.

Without :pool, f runs on `clojure.core/future`, whose pool is UNBOUNDED.
Work that neither observes interruption nor polls the token keeps its
thread; N timed-out calls hold N threads. Pass a bounded :pool wherever
timeouts are expected, and the ceiling is the pool instead of the heap.
A saturated :abort pool returns (err :weave/rejected ...) rather than
running the work on the caller.

ClojureScript: f runs synchronously on the caller and the timeout is
advisory (see the ns docstring); :pool is accepted but unused.
sourceraw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close