<< Back to previous view

[ASYNC-36] Waiters should unblock on close! Created: 14/Nov/13  Updated: 29/Mar/14  Resolved: 22/Nov/13

Status: Resolved
Project: core.async
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Defect Priority: Major
Reporter: Herwig Hochleitner Assignee: Timothy Baldridge
Resolution: Declined Votes: 1
Labels: None


 Description   

Expected behavior

When a channel is closed, all readers and writers waiting for it, whether on a thread with the !! family of functions or in a go block, should unblock and return nil, as if the channel had been closed in the first place. This is expected since channels are frequently discarded after being closed.

Problem description

The operator >!! (used as example here) immediately returns, when used on a closed channel, however, if >!! is already waiting for a channel, which is then closed, >!! doesn't unblock. , stalling the thread indefinitely.
EDIT: The thread/go block can still be unblocked, by taking from the channel, however since close! signifies the end of the lifetime of a channel, subsequent takes on it cannot be considered idiomatic.

Example REPL Sessions

In this example, the future blocks indefinitely, even though the target channel is closed

This is a problem, since the >!! might be called in a try..catch holding resources and such.

The example also shows, that values can be read from a closed channel (and trigger processing), which might be ok for channel buffers, not so much for pending >!! that have side effects in their continuation.

> (def ch (async/chan))
> (future (>!! ch :cool)
          (println "done"))
> (<!! ch)
"done"
:cool
> (future (>!! ch :cool)
          (println "done"))
> (close! ch)
;; FIXME "done" should be printed here
> (<!! ch)
"done" ;; EDIT pending >!! can be taken from, same as with >!
nil
> (future (>!! ch :cool)
          (println "done"))
;; Same as here
"done"

EDIT Removed example documenting different behavior of threads and go blocks, since subsequent tests turned them out to behave the same.

References

This was first posted to github https://github.com/clojure/core.async/issues/36
Some discussion already happenend there.

http://dev.clojure.org/jira/browse/ASYNC-31 seems related with regard to the close! semantics of a write.



 Comments   
Comment by Leon Grapenthin [ 14/Nov/13 12:52 PM ]

If I try to reproduce your second future, after (<!! ch) "done" is printed correctly. I tried it twice today with "0.1.242.0-44b1e3-alpha".
Probably you didn't see "done" because it was printed in the nrepl-server buffer?

This would be the only proof of infinite blocking. Aside from that this report raises the question whether parked puts should be cancelled when a channel is closed. It would be great if the docstring of close! would state this more clearly (currently it says "Data in the channel remains available" which could also refer to the channels buffer).

Comment by Leon Grapenthin [ 15/Nov/13 11:43 AM ]

I could also not reproduce the second future hanging after the take with "0.1.256.0-1bf8cf-alpha"

Comment by Herwig Hochleitner [ 16/Nov/13 9:22 PM ]

You are right, I couldn't reproduce the infinite stall (and hope I never will).

I would indeed argue that parked gets and puts should be cancelled when a channel is closed. Otherwise the answer to the question "when can I read/write a channel and when should I do it?" gets very non-deterministic. Don't forget, channels are not only about data transmission but also about coordination, (i.e. thread scheduling, thus resource management).

e.g. the current behavior yields the rule: "only write if somebody's listening or if the channel is not going to be closed anytime soon otherwise you might stall forever". Is this intentional? Better than the alternatives?

Comment by Leon Grapenthin [ 17/Nov/13 10:55 AM ]

If you are taking from a channel, you can determine that it's closed by taking nil from it. If you are putting on a channel you can't determine whether it's closed, however the operation will immediately return if the channel is closed and not block.

However in all cases I used async in so far it seemed only natural to handle the closing of a channel on the same site where the puts are made and the channel was created.

Comment by Herwig Hochleitner [ 17/Nov/13 6:52 PM ]

Yes, but normally you wouldn't take/put/do anything to a channel after calling close! on it.
That's what I meant when I said it wouldn't be idiomatic to require taking from a channel after a close! just to make sure that no writers are blocked.

Comment by Leon Grapenthin [ 18/Nov/13 3:16 PM ]

I would just say that there is no reason to close a channel at all when puts are still pending. You close! the channel only when you are done putting with the reason to communicate to the taking site that nothing more is going to come through the channel.

I find it hard to see the benefit of unblocking parked puts! with close!. They could be in a loop and they don't know that they were unblocked by a close!. If the putting site could determine that the target channel has been closed (by an exception thrown from put! that is unimplemented), it could stop putting permanently - But then it would know what ended up on the channel (and can be taken) and what did not end up on the channel (and can not be taken). Both information would be lost on the putting site if parked puts can be cancelled by a close without notification. You are right that they would be unblocked, but they would probably run infinite loops or simply do more useless puts.

As I see it: Until the put! exception (from the put! docstring) has been implemented one has to avoid putting onto closed channels. If it ever will be implemented it adds another way to determine whether a channel was closed (without taking from it) which I would prefer over cancellation via close!.

EDIT: I forgot to add that you can of course invoke @(.closed ch) to determine whether a channel has been closed but it is questionable whether this is officially supported or an implementation detail that may change.

Comment by Herwig Hochleitner [ 19/Nov/13 2:55 PM ]

> I would just say that there is no reason to close a channel at all when puts are still pending

Channels are designed to be multi-writer and multi-reader, so often you will want to close! a channel from outside. e.g. when the user clicks cancel.
As far as I understand it, close! is the intended interface for such things.

> They could be in a loop and they don't know that they were unblocked by a close!.

Well, that's actually the same when the channel is closed beforehand. And yes, failed puts should be distinguishable from regular puts, but that's ASYNC-31
Should also throw if a pending put gets unblocked by close.

Comment by Leon Grapenthin [ 19/Nov/13 7:15 PM ]

I think I understand your point now. Basically your idea is that channels should be close-able from everywhere, anytime, independently of any puts or takes. However the more I think about it, I can not prefer it over the current implementation:

Being able to close channels from everywhere seems like something that would make it easier to deal with channels and associated processes on the first look. But it would always require a special implementation on the putting sites: Whenever you put on a channel, you'd have to check whether it has been closed and you'd have to implement further decision making based on that information.

The current implementation leads one to do closing in sync with puts: If the user clicks cancel, this is the information the code should process as "Stop putting, Close channel" in that order. If the channel was closed first, that would first produce a new (less valuable) information which would need to be processed (with less information about what happened) which in my view would make things only more complicated.

The user having clicked cancel is specific and valuable information about something that has happened in the past and thus can not be realized in zero time or instantly anyway. Users have to wait as well. The user request can be taken from one channel of user-events. Putting onto the an output channel can properly come to an end first and then the output channel can be closed. Display or other further processing of the last produced output value will happen and the put site can rely on it. With the current constraints one writes code where this information of the user having clicked cancel is directed to the site affected by it, the site were the output values are produced, what is what the user desires to come to an end soon.

This enforcement of handling closing on the calling site is in my view the win for the current implementation: Channels are really like cables were the possibility of them being cut suddenly is not a desired feature but something one would rather avoid.

Comment by Herwig Hochleitner [ 19/Nov/13 10:55 PM ]

> Basically your idea is that channels should be close-able from everywhere, anytime, independently of any puts or takes.

In fact, they are! It's just that the semantics don't seem well-defined to me.

> This enforcement of handling closing on the calling site is in my view the win for the current implementation

There is no such enforcement. Just out of interest: How would you design such enforcement, from an API / Semantics POV? How would you disallow arbitrary closes?

> Channels are really like cables were the possibility of them being cut suddenly is not a desired feature but something one would rather avoid.

We should design not only for desired cases, but also for failure modes. "would rather avoid" isn't sufficient when thinking about the possible combinations of an API.

Also in the real world, cables do get unplugged, without either side getting stuck permanently and plugs/sockets very much are desired features in cable-related technology.

Please do me a favor, play advocatus diaboli and try to argue for a second that channels should be closeable from outside of the writer context.
A good starting point: The writer might already be blocking for an unforseen amount of time when the time to close it has come.

Comment by Leon Grapenthin [ 20/Nov/13 5:25 PM ]

I know that there is no real enforcement of handling closing on the putting site. But it's what you are lead to do when you want to avoid unnecessary puts.

If you call your suggestion (closing from anywhere) a failure - it's a failure that can be avoided by simply not doing it.

I tried to imagine cases where channels should be close-able from outside the writer context yesterday a lot before I posted. I simply can't find any. What you are suggesting as a starting point to find such a case is a scenario where you want to cancel a put operation that has already begun. For that scenario we have alts! where you have one put operation that can take a long time and another channel to communicate the cancellation through. If the cancellation comes first, the put operation will not happen even though it was blocking.

Here is a link to an extended example where I have implemented such a scenario: https://www.refheap.com/21103

Comment by Herwig Hochleitner [ 21/Nov/13 10:14 AM ]

I'm tempted to respond with a sole [closed from the reader side] to demonstrate a case where it's valid to close from outside of the writer context, but of course that would be rude. Instead let me try to get an exact grasp on our differing views:

I think, the full API of a library should be usable or produce visible errors immediately. In my view there is no such thing as "avoid by simply not doing it" in API design (except for bad Java APIs
You are certainly free to view a certain legal combination of API calls as failure but I wouldn't be so quick. I think the API of core.async offers close! as a standalone operation for a reason. I think the reason is, that channels are designed for broader applicability than just "dump into channel, then close".

I'd still like to hear a proposal on how to effectively prevent closing from outside of the writer (my guess is: there is no feasible way with the current API).
The reason I insist on this:
There are 2 things, programmers don't like: deadlocks and "don't do that". The combination of those two are what's broken with lock concurrency. Please let's not regress to that "state of the art".

OK here is an example, please don't discard it due to the fact that writers currently can't distinguish between an open channel and a closed one, that's for ASYNC-31:
Take a channel akin to range: producing an infinite number of values. You generate it somewhere in your program, pass it around and then consume it somewhere else.
How should this work?
1) Requiring me to design my program such I have the close condition available at the channel creation site, even though the producer might be completely generic, while only the consumer logically knows about the proper closing time, OR
2) Just let me close the channel from the consumer, when it doesn't want to consume anymore?

Your alts! example is also an excellent point in my case: Even though the put operation will never happen, it's still remaining in a pending state, tying up resources.

Comment by Leon Grapenthin [ 21/Nov/13 8:37 PM ]

Let me get right back to your last point: I have added a little benchmark of the blocking put operation to my example to prove that the put operation is not in pending state and unblocks as soon as :stop is dispatched.

Unfortunately I don't quite get what you mean by a "sole". "Closing from the reader side" is not necessary with the current API.

Now regarding your example:
1) In this point it is not quite clear to me whether you mean to distinguish between "creation site" and putting site. I will assume that you mean both and the same thing and call it production site. My answer would be: Yes. Design your api/program so that you have a close-condition available, even though the producer might be completely generic. One example for such a close-condition is in the example I have linked to: Taking :stop from a seperate channel stops production and closes the channel. The consumer can determine the proper closing time and send :stop, as demonstrated in my example.
2) I know that this is what you want, but you would still need the close condition at the production site to stop putting. Unless you have determined that the channel has been closed, you'd put the next value onto the chan. With the current implementation, such a scenario would heat up the CPU. If you could determine closing on the production site for example by an exception, you would do exactly the same thing as in point 1), but with less information, as I have pointed out in earlier comments.

I believe programmers like "don't do that" much more than "you can't do that". So I think it's fine that you can close! from everywhere. I had some cases myself where very few unnecessary put operations where tolerable as I knew that they would not be possible again after a very short predictable amount of time (In fact when the user clicked cancel: I knew that the interface would be removed from the screen immediately and that a few puts triggered by event listeners wouldn't mind the CPU). So I wouldn't even go so far as to prevent closing. But what I'd really like is that when I develop an API for a library built on top of core.async is this: I don't want to expect channels being closed every time I do a put. I don't want to write an exception-catcher every time. But if what you have proposed would be reality, that would be an unavoidable necessity. Otherwise, I'd never be sure whether values I have put were received, or the channel has been closed. What if I write a function that takes values from one channel and puts them onto another (like core.async/pipe), and suddenly the output-channel was closed? Would I have to close the input channel then as well? Ownership would be totally undefined in such a scenario and lead to all kinds of confusion and most likely lots of boiler-plate code. If I wrote anything that does an unpredictable amount of puts, I'd provide one or multiple facilities to stop the puts.

You may also want to have a look at the C# implementation of the cancellation of tasks: Cancellation tokens are used to determine cancellation on the production site at a specific point in execution order and are passed as an additional parameter: "If library code provides cancellable operations, it should also provide public methods that accept an external cancellation token so that user code can request cancellation." (http://msdn.microsoft.com/en-us/library/vstudio/dd997364.aspx).

Or erlang, where a "finished" message is passed from the production site: http://www.erlang.org/doc/getting_started/conc_prog.html#id67009

Or in go: "A sender can close a channel to indicate that no more values will be sent." and "Note: Only the sender should close a channel, never the receiver." (http://tour.golang.org/#66)

And then there is Haskell, where you can't close channels, but only kill threads...

Comment by Timothy Baldridge [ 22/Nov/13 2:47 PM ]

Copy of my comments to the email in clojure-dev:

Example repl session:

user=> (def c (chan))
#'user/c
user=> (go (>! c 42) (println "done"))
#<ManyToManyChannel clojure.core.async.impl.channels.ManyToManyChannel@29794f5b>
user=> (go (>! c 42) (println "done"))
#<ManyToManyChannel clojure.core.async.impl.channels.ManyToManyChannel@7848089e>
user=> (go (>! c 42) (println "done"))
#<ManyToManyChannel clojure.core.async.impl.channels.ManyToManyChannel@1b6b43ac>
user=> (go (>! c 42) (println "done"))
#<ManyToManyChannel clojure.core.async.impl.channels.ManyToManyChannel@464f0728>
user=> (close! c)
nil
user=> (<!! c)
done
42
user=> (<!! c)
42done

user=> (<!! c)
done
42
user=> (<!! c)
done
42
user=> (<!! c)
nil
user=>

The library works as expected. If you want the pending puts to be dropped, feel free to write something like this:

(defn close-and-flush [c]
(close! c)
(clojure.core.async/reduce (fn [_ _] nil) [] c))

Semantics are now:

user=> (def c (chan))
#'user/c
user=> (go (>! c 42) (println "done"))
#<ManyToManyChannel clojure.core.async.impl.channels.ManyToManyChannel@284b38f9>
user=> (go (>! c 42) (println "done"))
#<ManyToManyChannel clojure.core.async.impl.channels.ManyToManyChannel@43bbaad3>
user=> (go (>! c 42) (println "done"))
#<ManyToManyChannel clojure.core.async.impl.channels.ManyToManyChannel@16cc601f>
user=> (go (>! c 42) (println "done"))
#<ManyToManyChannel clojure.core.async.impl.channels.ManyToManyChannel@630f83c9>
user=> (close-and-flush c)
#<ManyToManyChannel clojure.core.async.impl.channels.ManyToManyChannel@3f1d023f>done
done

done
done
user=>

--------

The semantics are not going to change. Pending puts are flushed. Pending takes are given nil after a closed channel flushes. This is the way the library was designed to operate. By default the library will not discard data unless you configure it to do so, to have pending puts automatically throw away values on a channel would cause many errors, and make libraries harder to reason about.

Leon is right, close! basically means "after all pending values have been written, close the channel".

Comment by Alexander Kauer [ 29/Mar/14 4:36 AM ]

Hi,

since I ran into the same problem (see report ASYNC-60 , somehow I overlooked this report first) I think the documentation should explicitly state that behaviour to avoid further confusion for others.

Generated at Thu Oct 02 05:59:40 CDT 2014 using JIRA 4.4#649-r158309.