The reason for the behavior you are observing is a race condition. The 
effects of close! on the pub aren't synchronous. Even though the channel is 
immediately closed before return, consumers need time to determine that it 
has been closed. At the point in time the pub determines that the source 
channel has been closed, it does indeed close all subscribed channels, as 
your last form demonstrates. However, in your second last form, all 
subscribed channels may have already been unsubscribed, due to unsub-all.

Thus, the solution is to not call unsubs-all if you want all subscribed 
channels to be closed as a consequence of closing the source channel.


On Sunday, March 1, 2015 at 2:17:20 PM UTC+1, Jonas wrote:
>
> Hi all!
>
> I’m working with core.async pub/sub and ran into an issue which I don’t 
> quite understand. According to the clojure.core.async/sub docstring:
>
> > By default the channel will be closed when the source closes
>
> This is not the behaviour I’m seeing when I call 
> clojure.core.async/unsub-all immediately after I close the source channel.
>
> Example:
>
>     (def source (async/chan))
>     (def sub-chan (async/chan))
>     (def pub (async/pub source (constantly "foo")))
>     
>     ;; by default sub-chan should close when source closes
>     (async/sub pub "foo" sub-chan) 
>     
>     ;; This will not close sub-chan
>     (async/thread
>       (async/close! source)
>       (async/unsub-all pub))
>
>     ;; This will close sub-chan
>     (async/thread
>       (async/close! source))
>
> What is the reason for this behaviour? Is it unnecessary to call unsub-all 
> if I close the publications source channel?
>
> Thanks,
> Jonas
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to