Skip to content

Instantly share code, notes, and snippets.

@paulbellamy
Created June 23, 2014 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulbellamy/54d7d908d7ebce5bd2c0 to your computer and use it in GitHub Desktop.
Save paulbellamy/54d7d908d7ebce5bd2c0 to your computer and use it in GitHub Desktop.
How to lock up core.async
(import '[java.util.concurrent CountDownLatch])
(require '[clojure.core.async :as async])
(defn now []
(System/currentTimeMillis))
(defn waiter [i latch results]
(async/go
(swap! results conj [i :waiting (now)])
(.await latch) ;; Will block the thread
(swap! results conj [i :done (now)])))
(def core-async-thread-count (-> (Runtime/getRuntime) (.availableProcessors) (* 2) (+ 42)))
(def x (+ core-async-thread-count 1))
(defn- parse-results [results]
(->> @results
(group-by second)
(map (fn [[k v]] [k (count v)]))
(into {})))
(defn show-bug []
(let [latch (CountDownLatch. 1)
results (atom [])]
(dotimes [i x] (waiter i latch results))
(let [parsed (parse-results results)]
(if (= x (:waiting parsed))
(println "no lockup!")
(println "Expected" x "to be waiting, but there were" parsed)))))
(show-bug)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment