Error starting child processes: failed to start child

Hello everyone,

I am fairly new to Elixir and so far I’ve had a blast.
As part of an academic assignment I’ve been tasked with implementing a concurrent messaging application using Mutex and Locks.

Before anything, I’ll start by saying that I know this is highly discouraged in Elixir and I am very aware and understand why. Still, for learning’s sake, I must do it with Mutex and Locks in mind.

I am currently using the Mutex Mix module described here. As far as I understand, it’s basically a GenServer with error handling.

My first solution was to follow the documentation on the Mutex Mix module and did everything on top of another GenServer to harbour its utilities to handle state and lock every operation with the Mutex. It works as expected, yet upon asking my TA, they confirmed that was not allowed for this assignment.

Having those two things in mind, so far, I’ve defined a module such that

defmodule MutexChatroom do

def func(state) do
  receive do
    {:join, user} ->
      state = join(state, user)
    {:disconnect, user} ->
      state = disconnect(state, user)
    {:write_message, user, message} ->
      state = write_message(state, user, message)
    {:read_messages, user} ->
      state = read_messages(state, user)
    {:exit} ->
      IO.puts("SERVER: Goodbye!")

the join/2, disconnect/2, write_message/3 and read_messages/2 functions all follow the following structure:

def func(state, x) do
    lock = Mutex.await(ChatLock, state)

        #Some logic checking the state and logging to console.

     Mutex.release(ChatLock, lock)

Inside the main module where I handle user input on the console I am calling on start the following:

    {:ok, pid} = Task.start_link(fn -> MutexChatroom.func(%{users: [], messages: []}) end)

    children = [
      {Mutex, name: ChatLock}

    {:ok, sup_pid} = Supervisor.start_link(children, strategy: :one_for_one)

and using the send/2 function using pid as a parameter to update the state in the module.

Upon running the code, once send/2 is called I get the following error:

** (EXIT from #PID<0.191.0>) shell process exited with reason: shutdown: failed to start child: Mutex
    ** (EXIT) already started: #PID<0.193.0>

So far I’ve had no luck replicating the behaviour of my previous implementation neither managed to run it completely.

Any help is welcome,
Thanks! :slight_smile:

That error message is from Supervisor.start_link - are you sure execution even makes it to the send?

The message tells you something else has already started a process named ChatLock.

1 Like


You are right, I found out the error like 20 minutes after posting and was trying to frantically delete the post once I fixed it. I had the Supervisor.start_link line running recursively by accident.

Thanks anyway :slight_smile: