Task.async return message

Currently I am reading Elixir In Action and it is mentioned there for Task.async that the return value of the executed lambda is sent back as a message to the starter process. I am a bit confused here or maybe missed some important part of the text, please correct me if I am wrong: it means that even if i dont care for the return value of the lambda i must have something like handle_info to catch the message that is send back or ? Because if i dont handle the message it will just stay in the mail box forever, thanks guys

1 Like

Yes. Exactly.

I’m not aware of a Task like thing that can be run solely for its side effects. But exactly the current behaviour of sending back “unwanted” messages makes it break applications, or at least creates a lot of confusing log messages.

@Fl4m3Ph03n1x created a lot of threads to trace down those messages, and I’m not even sure if they ever found the actual source of the stray messages.

Yes, it’s quite clear in the doc I’d say. See https://hexdocs.pm/elixir/Task.html:

Tasks spawned with async can be awaited on by their caller process (and only their caller) as shown in the example above. They are implemented by spawning a process that sends a message to the caller once the given computation is performed.

And, so Task.await is used to read the message sent by the task.

The documentation also suggests several other ways to run tasks, @Tano, if you don’t want this behavior, eg. Task.start_link/1, Task.start/1, or starting tasks under a Task.Supervisor.

1 Like

Yes, I found the processes sending the messages, but the methodology we used is not fit for production, unless you don’t mind killing a machine with all the logging or slowing it down considerably.

There are many ways of finding out which process is receiving an unhandled message, but there is no straight way. I have compiled a list of all the relevant posts I created regarding this topic for your convenience, should you ever venture this far into message passing:

Still, if you follow the recommendations for Task.async and Task.await, you will (hopefully) never need this information, unless you are using a 3rd party library that doesn’t follow the conventions (as was our case).

2 Likes