Halt channel shutdown until async task completes

Hi Elixir friends,

I have a Phoenix.Channel, which may start a task (via Task.async) that can run for up to 60 seconds. The task will send messages back to the channel process (which it handles in handle_info and translates into a push() to the client when appropriate). The channel isn’t directly awaiting the task since it has to be able to handle other messages from the client while the task runs.

When the application stops (e.g. due to a new version being deployed), I’d like the channel to not disconnect the client until the task completes (maybe with a timeout of 60 seconds). I cannot figure out how to do this.

I’ve tried using terminate (with and without trapping exits in the channel), and that seems to be called after the websocket connection has already been disconnected, so this is too late.

I’m guessing I have to catch the channel’s exit signal and choose to “ignore” it, but store in the socket state that exit is pending, such that when the task finishes it can shutdown itself. But it seems that this is handled at some higher level that I don’t have access to.

Any ideas?