Hi guys I’m having trouble with actors, can’t an a...
# coroutines
Hi guys I’m having trouble with actors, can’t an actor send a message to itself? somehow ,
blocks, what’s the properway to do it?
It doesn't block, it suspends. You can use
returns false.
basically problem persists: actor not being able to send message to itself
@louiscad fixed: default capacity is 0 for some reason. It probably gets changed some place (otherwise one wouldn’t be able to send messages to actor without providing capacity), but using the channel reference inside block, it still uses that argument capacity (which is 0), so
suspends eternally and
@elizarov shouldn’t his behaviour be changed? the the
method arguemtns indeed there is this code:
capacity: Int = 0, // todo: Maybe Channel.DEFAULT here?
I think the only way you can do this safely is to set capacity to UNLIMITED, because with a fixed capacity you run into a danger of deadlock like you’ve mentioned: 1. The actor receives a message 2. While the actor processes, another coroutine pushes something onto the actor’s channel 3. The actor tries to send a message into its own channel, but it suspends because the channel is full 4. The channel now can’t be emptied because the actor needs to pull the next message from the channel, but can’t do that until it completes the send
@tmg Check out the docs regarding capacity, you'll see capacity 0 means something special, the french translation of appointment, and in your case, an appointment between receive and offer is impossible.
The problem with setting to unlimited though is the lack of backpressure, so if the actor processes messages slower than senders are sending the channel will continue to grow in size and your memory usage will balloon
The default behavior is on purpose - everyone suspends until the message can successfully be delivered to the actor, but you can specify a capacity if you need it
@Evan R. yeah, when sending a message to itself it can indeed reach a deadlock without unlimited, but for this use case, there is an actual limit on the messages the actor will receive, so with a proper capacity value, it would never reach it, or if I use unlimited it would never grow too much @louiscad I don’t understood your comment. Is changing from capacity 0 here wrong in my case? (the only thing i get from your comment is that i should read the doc 😅 which I will do
@tmg In the docs of
, which is used and exposed in
, you'll find
, which is equal to
👍 1