Welcome! Please see the About page for a little more info on how this works.

0 votes
in core.async by
Current patch file: 0002-ASYNC-126-Add-public-channel-closed-predicate.patch

Add a public function {{closed?}} in clojure.core.async to determine if a channel is closed without putting or taking any values.

This is a trivial wrapper around {{clojure.core.async.impl.protocols/closed?}}

There is still a race between {{closed?}} and {{close!}}. Correct code should never rely on {{closed?}} to check that a channel is *open*. The {{closed?}} predicate should only be used to avoid extra work producing a value for a closed channel.

h2. Example use case

A producer process can check if a channel is {{closed?}} before doing the work to produce the next value.


(thread
  (loop []
    (when-not (closed? ch)
      (>!! ch (do-expensive-work))
      (recur))))

5 Answers

0 votes
by

Comment made by: dialelo

The patch only adds 'closed?' to the ClojureScript version of core.async, is there a reason for not adding it to the Clojure version?

0 votes
by

Comment made by: stuart.sierra

Previous patch was missing the Clojure(JVM) implementation. Fixed in new patch 0002-ASYNC-126-Add-public-channel-closed-predicate.patch.

0 votes
by

Comment made by: bbloom

Please do not add a closed? predicate to the public API. It's a race condition in 99.9% of cases. Go does not provide such a predicate and after growing accustom to CSP via both core.async and Go, I have never felt the need for it. Having the predicate would invite its use. I enjoy clojure.core's preference for omission of questionable functions and expect the same from core.async.

If somebody desperately feels the need for this predicate, ask in IRC or Slack, I'm sure somebody can help you fix the structuring of your channels/processes/etc.

0 votes
by

Comment made by: gshayban

Echoing Brandon, I'm also unconvinced. This is a bug waiting to happen.

0 votes
by
Reference: https://clojure.atlassian.net/browse/ASYNC-126 (reported by stuart.sierra)
...