[swift-evolution] [Concurrency] async/await + actors

Thomas tclementdev at free.fr
Fri Aug 25 11:54:47 CDT 2017


> On 25 Aug 2017, at 18:30, Adam Kemp <adam.kemp at apple.com> wrote:
> 
> 
> 
>> On Aug 25, 2017, at 1:14 AM, Thomas <tclementdev at free.fr <mailto:tclementdev at free.fr>> wrote:
>>> On 25 Aug 2017, at 01:15, Adam Kemp <adam.kemp at apple.com <mailto:adam.kemp at apple.com>> wrote:
>>> I don’t think await should cause the actor’s queue (or any queue) to be suspended. Actor methods should not block waiting for asynchronous things. That’s how you get deadlocks. If an actor method needs to be async then it should work just like any async method on the main queue: it unblocks the queue and allows other messages to be processed until it gets an answer.
>>> 
>>> You do have to be aware of the fact that things can happen in between an await and the next line of code, but conveniently these places are all marked for you. They all say “await”. :)
>> 
>> It is correct that suspending the queue allows for deadlocks, but not doing it means you can receive messages while still in the middle of another message. For the same reason you may need FIFO ordering in a class to guarantee coherency, you will want this to work in an asynchronous world as well. Take for example some storage class:
>> 
>> 1. store(object, key)
>> 2. fetch(key)
>> 
>> If you're doing these operations in order, you want the fetch to return the object you just stored. If the 'store' needs to await something in its implementation and we were to not suspend the queue, the fetch would be processed before the object is actually stored and it would return something unexpected.
> 
> Actors can use other means to serialize operations if they need to, for instance by using an internal queue of pending operations. It’s better for actors that need this kind of serialization to handle it explicitly than for every actor to suffer from potential deadlocks when doing seemingly straightforward things.
> 
> async/await in general is not meant to block anything. It’s explicitly meant to avoid blocking things. That’s what the feature is for. It would be confusing if await did something different for actor methods than it did for every other context.

I'd tend to think non-FIFO actor messaging will cause more trouble than potential deadlocks. I'm re-reading the proposal and it seems to go this way as well:

"An await on an actor method suspends the current task, and since you can get circular waits, you can end up with deadlock. This is because only one message is processed by the actor at a time. The trivial case like this can also be trivially diagnosed by the compiler. The complex case would ideally be diagnosed at runtime with a trap, depending on the runtime implementation model."

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170825/1f48b70f/attachment.html>


More information about the swift-evolution mailing list