{{thread-bound?}} returns true for thread-local bindings that were not bound by the current thread. This is at odds with the docstring, which states that if true is returned, {{set!}} should succeed.
ere is an example REPL session where a thread establishes a binding, those bindings are conveyed to a second thread, the second thread checks thread-bound? to see if it can set the binding, thread-bound? returns true indicating that the binding can be set, the second thread tries to set the binding, and the second thread gets an IllegalStateException:
Clojure 1.5.1
user=> (def ^:dynamic *set-me* nil)
#'user/*set-me*
user=> (defn try-to-set [] (binding [*set-me* 1] (doall (pcalls #(if (thread-bound? #'*set-me*) (set! *set-me* (inc *set-me*)))))))
#'user/try-to-set
user=> (try-to-set)
IllegalStateException Can't set!: *set-me* from non-binding thread clojure.lang.Var.set (Var.java:230)
user=>
*Approach:* thread-bound? should return false in the case where there is a binding *and* that binding was not established by the current thread. The patch adds a new function to Var as core does not have visibility into the non-public Var$TBox.
*Patch:* thread-bound.diff
*Screened by:* Alex Miller