Hi guys I’m having trouble with actors, can’t an a...
# coroutines
t
Hi guys I’m having trouble with actors, can’t an actor send a message to itself? somehow ,
channel.send()
blocks, what’s the properway to do it?
l
It doesn't block, it suspends. You can use
offer
here.
t
offer
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
send
suspends eternally and
offer
returns
false
@elizarov shouldn’t his behaviour be changed? the the
actor
method arguemtns indeed there is this code:
capacity: Int = 0, // todo: Maybe Channel.DEFAULT here?
e
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
l
@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.
e
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
t
@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
l
@tmg In the docs of
Channel
, which is used and exposed in
actor
, you'll find
RENDEZ_VOUS
, which is equal to
0
.
👍 1