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

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement