Language: Clojure · Versions: Clojure 1.12.0 (org.clojure/core.async 1.6.681)
The problem
Inside a core.async/go block, I refactored a repeated channel read (<! ch) into a local helper function or passed #(<! %) into (mapv #(<! %) chans). At runtime, it immediately fails with AssertionError: Assert failed: <! used not in (go ...) block, and even worse, if someone uses (map #(<! %) chans) inside go without mapv, the go macro DOES rewrite <! inside the lazy map closure if inlined—Wait no: it never crosses (fn ...) boundaries, AND if someone calls blocking (<!! ch) inside a go block as a "workaround", all 8 fixed threads in the core.async dispatch pool can deadlock!
Minimal example
(ns async-trap
(:require [clojure.core.async :as a :refer [go <! >! chan]]))
(defn read-all-broken [chans]
(go
;; Throws AssertionError at runtime because #(...) creates a new `fn*` boundary
;; that the `go` state-machine macro does not transform!
(mapv #(<! %) chans)))
Expected
Read a value from each channel in chans sequentially (or concurrently) inside a go block without blocking the fixed-size core.async dispatch thread pool.
What happens instead
- The
gomacro only rewrites the AST of its lexical body and stops at any nestedfn*(fn,#(...),defn). Inside the innerfn,<!is left un-transformed and throwsAssertionError: <! used not in (go ...) block. - Replacing
<!with<!!inside agoblock parks the underlyingcore.asyncthread-pool carrier thread; if 8goblocks do<!!waiting on a 9thgoblock to>!, the entirecore.asyncruntime deadlocks.
What are the cleanest ways in core.async to abstract reusable parking logic across helper functions (macros vs channel-returning functions vs a/map / a/into)?
Your diagnosis is mostly right. One thing needs correcting: the go macro never rewrites <! inside a nested fn*, and inlining doesn't change that. Lazy map, for and #(...) all expand to fn*, so they all fail the same way. doseq and loop work, because they expand to loop*/recur with no fn.
1. Inline the loop (sequential, keeps order)
(defn read-all [chans]
(go
(loop [cs (seq chans), acc []]
(if cs
(recur (next cs) (conj acc (<! (first cs))))
acc))))
2. Helpers that return channels (the normal way to reuse logic)
The helper runs its own go block. The caller parks on the channel it returns, so parking stays inside a go body at every level.
(defn read-with-default [c default]
(go (if-some [v (<! c)] v default)))
(go (let [x (<! (read-with-default ch :none))] ...))
3. Macros, when you want the code to sit inline in the caller's go block
(defmacro <!-or [c default]
`(if-some [v# (a/<! ~c)] v# ~default))
(go (<!-or ch :none)) ; expands before go analyses it, so it's rewritten
Use this for small control-flow snippets. A channel-returning function (option 2) is easier to test.
4. Combinators (concurrent)
;; one value from each, in order; closes when any input closes
(go (<! (a/map vector chans))) ; guard against empty chans yourself
;; one value from each, order NOT preserved
(a/into [] (a/merge (map #(a/take 1 %) chans)))
Why <!! deadlocks
<!! doesn't park. It blocks the real thread. In 1.6.681, go blocks run on a fixed pool (8 threads by default, set by the clojure.core.async.pool-size property). If all 8 threads block waiting on a go block that can't get a thread, everything stops, exactly as you describe. Use <!! and >!! only off the go pool: on the REPL or main thread, or inside a/thread, which returns a channel you can <! from a go block. During development, starting the JVM with -Dclojure.core.async.go-checking=true makes blocking operations inside a go thread throw instead of hanging.
What I checked and what I'm inferring
- Checked against the 1.6.x source: the
<!you call outside agobody is a plain function whose body is(assert nil "<! used not in (go ...) block"). Under(set! *assert* false)it returnsnilsilently instead of throwing. - Not checked: I haven't run any of the snippets above.
- Not checked: how
a/mapbehaves with an emptychans. I didn't check it, so test it. - Not checked: newer core.async releases (1.8+) changed how blocking threads work on modern JDKs, and I haven't confirmed the details. Read the changelog before you rely on pool behaviour after upgrading.