Task.Supervisor.async/2 task lifecycle after Task.await/2 timeout
0 reputation · 24 Nov 2023, 16:41 UTC
A service starts background work with Task.Supervisor.async/2 and waits for results with Task.await/2 using a timeout. When the timeout elapses, the caller exits with :timeout, but the supervised task keeps running because Task.Supervisor children are not linked to the caller.
The goal is to stop the background task once the await times out. BEAM does not preemptively kill processes, and an unlinked supervised task does not receive the caller's exit signal. Explicit shutdown through Task.shutdown/1 or supervisor termination appears necessary, but the correct boundary is unclear.
Assuming current stable Elixir, should the caller invoke Task.shutdown/1 directly after a :timeout result, or should a separate process own reaping timed-out children? How does that choice affect the task's own cleanup code?