Darkoyd

Darkoyd

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)
      func(state)
    {:disconnect, user} ->
      state = disconnect(state, user)
      func(state)
    {:write_message, user, message} ->
      state = write_message(state, user, message)
      func(state)
    {:read_messages, user} ->
      state = read_messages(state, user)
      func(state)
    {:exit} ->
      IO.puts("SERVER: Goodbye!")
  end
end
...
end

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)
     state
  end

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:

Marked As Solved

al2o3cr

al2o3cr

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.

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement