[ASYNC-36] Waiters should unblock on close! Created: 14/Nov/13 Updated: 29/Mar/14 Resolved: 22/Nov/13
|Reporter:||Herwig Hochleitner||Assignee:||Timothy Baldridge|
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.
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.
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.
EDIT Removed example documenting different behavior of threads and go blocks, since subsequent tests turned them out to behave the same.
This was first posted to github https://github.com/clojure/core.async/issues/36
http://dev.clojure.org/jira/browse/ASYNC-31 seems related with regard to the close! semantics of a write.
|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".
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.
|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.
> 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
|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.
|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
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).
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
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:
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)
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]
Semantics are now:
user=> (def c (chan))
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 ]|
since I ran into the same problem (see report