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 Switch mode

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.

Last Post!

Darkoyd

Darkoyd

Hi,

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:

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement