Process.send_after and Process.cancel_timer

I’m using Process.send_after to schedule a repeated operation. I’m using a GenServer and receiving the messages in handle_info. All is working fine. To make sure I don’t accidently start multiple timers I am calling Process.cancel_timer in my handle_info function. The strange thing is I am getting a call to handle_info({:cancel_timer, ref, some_bool}, status) which I haven’t implemented so the GenServer crashes. Is this expected? Do I need to do anything in this function or is:

def handle_info({:cancel_timer, _, _}, status) do
    {:noreply, status}


This is as expected and documented if you called like Process.cancel_timer(timer_ref, async: true, info: true.

You can find documentation about the behaviour hidden in the docs for the :info option of Process.cancel_timer/2

1 Like

Thanks. You’re right. Serves me right for not reading the documentation carefully. :slight_smile: