How is `:shutdown` option in `Task.Supervisor.html#start_child/3` used?

Can someone help me to understand what is the usefulness of option :shutdown in Task.Supervisor — Elixir v1.13.3 ?
It seems that Task unlike other OTP compliant processes is not based on GenServer. So there is no terminate function that it can call internally to do cleanup.
Since it is not able to do cleanup, how does it matter if supervisor shuts down the task immediately or after some custom timeout value?

I’d want to :brutal_kill or at least have <5s shutdown if the task is resource-intensive but I don’t need it anymore. Let’s say I’m doing optimistic file parsing of a large XML on a form file change event, but the user cancels the upload before the parsing is complete. Then I can free up the resources immediately by sending the Task a shutdown signal.

I believe i have got the answer by watching Graceful shutdown in Elixir - try not to drop the ball | Pawel Szafran | Code BEAM V - YouTube
Default shutdown option value is 5_000. If a Task.Supervisor sends a kill signal to the child it will simply die off. But if in the task we trap the signal, the supervisor will wait for 5_000 before brutal_killing it. Of course if task can finish whatever it was doing before 5_000 it will kill the task there and then.
So this setting is important to set to a timing that is enough for the task to finish whatever it was doing before supervisor kills it.

2 Likes