Skip to content

Instantly share code, notes, and snippets.

@yayitswei
Last active August 29, 2015 14:08
Show Gist options
  • Save yayitswei/d80d51ee29dc5e2c1117 to your computer and use it in GitHub Desktop.
Save yayitswei/d80d51ee29dc5e2c1117 to your computer and use it in GitHub Desktop.
Unexpected behavior using try/catch in a go block
(ns queue-test
(:require [clojure.core.async :as async :refer [>! <! chan go go-loop put!]]))
(defn process! [item]
{:pre [item]}
(println "processing:" item))
(defn init! []
(let [q (chan)]
(go-loop []
(try (process! (<! q))
(catch Exception e
(.printStackTrace e)))
(recur))
q))
(defn test! []
(let [q (init!)]
;; the first queue fails the preconditions for process!
(put! q false)
;; I expect this one to work, but it doesn't in practice
(put! q true)))
(defn test2! []
(let [q (init!)]
;; without the first failing put!, this works as expected
(put! q true)))
@brandonbloom
Copy link

Hm, Gist changed. Was a simpler repo a bit ago.

@yayitswei
Copy link
Author

I've just added two tests and a bit more explanation. test! fails and test2! works as expected.

Also swapped out (go (<! ..)) for put!, per your suggestion.

@laczoka
Copy link

laczoka commented Oct 28, 2014

should catch Throwable instead of Exception

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment