Registry with DynamicSupervisor

Taking code from livebook here: It tries to do a Registry.lookup for see if the task has been registered in the registry.

So, the {:error, {:already_started, pid}} branch should never be called. Here is the reason:

  1. Whenever, task starts, it will register itself under Registry with {:via , Registry , {k, fun})
  2. If there is a Task => it is in Registry else, simply start the child via DynamicSupervisor.start_child/2

I think I m missing something.

You’re missing the case, where another task starts up in between the calls to Registry.lookup and DynamicSupervisor.start_child. It won’t be there when you ask the registry if it exists, but it will be there when you try to start another instance with the same name.

2 Likes