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

0 votes
in core.async by

It seems to me that alt!! does complete put operations only when the channel is already closed from the beginning, but not when the channel is closed while alt!! is waiting. That is unlike read operations, which complete with a nil value.

Is that an intended behaviour? If so, that is quite 'unfavourable' for obvious reasons. It should at least be marked with a big warning sign in the docs then.

1 Answer

+1 vote

Can you give an example?


  (def ch (chan))
  (go (println "Done" (>! ch 42))
  (Thread/sleep 100)
  (close! ch)

Never prints "Done". I used >! instead of alt! here, because in the mean time I found out that it has the same behaviour.

And from the docs to 'close!':

"Logically closing happens after all puts have been delivered. Therefore, any
blocked or parked puts will remain blocked/parked until a taker releases them."

I conclude this is indeed intended bahaviour (one can/must still read all pending writes after the call to close).

But as I said, the docs for >! or alt! only contain the little word "already [closed]" to indicate that.

As a background: we have our own implementation of a variant of a 'pub sub' channel, and threads that try so send something to subscribers. But using 'close!' on the subscriber side can be a big source of problems then - as it sometimes 'works', but deadlocks the publisher if the channel is closed at the wrong time.
You need to give your channel a buffer size, otherwise it will act as a channel of size zero and yes you can't complete put until someone completes take. https://github.com/clojure/core.async/blob/master/examples/walkthrough.clj#L8