Mix run versus iex

I have simulated a twitter application which has a simulator to generate tweets and server to process them and store them in the ets. I am using multiple Gen Servers for clients, server and another one to manage the ETS. Although my code runs perfectly fine to generate tweets on iex, I see some issues where the GenServer gets killed before completion when I use mix to run the entire application. The behavior is different on different runs. It does sometimes go on to handle_cast and tweet. I am using handle_cast to simulate the tweeting process once all the clients are registered on ets. Any suggestions/help is appreciated. Please let me know if you need some snapshots to provide any assistance.

1 Like

How do you run your app with mix? Remember to add --no-halt to mix run so that the program keeps running after the start command is completed.

2 Likes

Yes, I just used Yes, I just tried using mix run --no-halt project4.ex 10 10 but still no luck. The args are my number of users and number of messages respectively.

1 Like

Do you see any logged errors?

No errors. Just exits in the middle of execution but it doesn’t on iex.

Are you monitoring the amount of memory? If you’re doing a lot of async stuff with cast in theory you could be letting the process inbox fill up and OOM. Can you post any example logs or code from when it does?

Yes, I am using multiple casts and here is a log of the iex call vs the mix run call
MIX RUN

mix run --no-halt project4.ex 1000 10

Server up!

%{num_clients: 0, num_followers: 0}

Starting the simulator

Start the client processes

Main Server #PID<0.123.0>

Followers updated, ask the simulator to generate Tweets

IEX
[ETSServer]

iex(6)> Runner.main [10,10]

Server up!

%{num_clients: 0, num_followers: 0}

Starting the simulator

Start the client processes

Main Server #PID<0.131.0>

Followers updated, ask the simulator to generate Tweets

Registering in server

“Registering?”

Followers of 1 are [2, 2]

Followers of 2 are [1, 1]

Registering in server

“Registering?”

Registering in server

Followers of 3 are [2, 2]

“Registering?”

Registering in server

Followers of 4 are [1, 2]

“Registering?”

Registering in server

Followers of 5 are [2, 1]

“Registering?”

Followers of 6 are [2, 2]

Registering in server

“Registering?”

Followers of 7 are [1, 1]

Registering in server

“Registering?”

Registering in server

Followers of 8 are [2, 2]

“Registering?”

Registering in server

Followers of 9 are [1, 1]

“Registering?”

Followers of 10 are [2, 2]

Registering in server

Start simulating the Twitter Engine

“Registering?”

{:ok, #PID<0.132.0>}

Client 4 tweeted : aspsav @3

Now start sending the tweet to the mentioned users

Now send my tweet to all the followers

[1, 2]

Client 6 tweeted : ozsyth @2

Now start sending the tweet to the mentioned users

Now send my tweet to all the followers

[2, 2]

Client 1 tweeted : ftnsyf

Now start sending the tweet to the mentioned users

Now send my tweet to all the followers

[2, 2]

Client 2 tweeted : jrbqja

Now start sending the tweet to the mentioned users

Now send my tweet to all the followers

[1, 1]

Client 3 tweeted : fpyjpz

Now start sending the tweet to the mentioned users

Now send my tweet to all the followers

[2, 2]

Client 5 tweeted : hjckvx

Now start sending the tweet to the mentioned users

Now send my tweet to all the followers

[2, 1]

Client 7 tweeted : vqcmlx

Now start sending the tweet to the mentioned users

Now send my tweet to all the followers

[1, 1]

Client 8 tweeted : aitdbp

Now start sending the tweet to the mentioned users

Now send my tweet to all the followers

[2, 2]

Client 9 tweeted : vuhybq

Now start sending the tweet to the mentioned users

Now send my tweet to all the followers

[1, 1]

Client 10 tweeted : wxxnni

Now start sending the tweet to the mentioned users

Now send my tweet to all the followers

[2, 2]

can you show project4.ex ?


project4.ex calls the runner where the execution starts.

Thanks a ton in advance!

How do you run iex -S mix? The fundamental issue though I think is that Elixir applications don’t start the way you think they do.

In your mix.exs file you’ve got https://github.com/kaushikf9t/DistributedSystems/blob/532816d92f1911f7e9f9d529122122323f4a9889/my_twitter/mix.exs#L24 which tells mix that your application will be started by the Twitter.CLI module. Notably, mix expects this module to start and return properly before it does anything else. The thing is, your Twitter.CLI module doesn’t have a valid start function for an application.You need to be returning a root supervisor or something not self(). This may well be creating a deadlock, since self() will be the application starter process, and it’s now waiting to hear a message from itself.

You need to either have a proper supervisor in the start function (which would have been generated for you with mix new --sup) or go the easier route: Remove the mod: option from mix.exs and then just have a simple run.exs file that contains only:

args = System.argv()
param1 = Enum.at(args,0)
param2 = Enum.at(args,1)
mainargs = [param1, param2]
Runner.main(mainargs)

No modules, just that code. Then you would mix run run.exs --no-halt. Not really production worthy, but fine if you’re just trying to get some code working without typing it into iex over and over.

5 Likes

Thanks a ton! Really saved my life, with a project deadline tomorrow! It seems to be working fine now.

1 Like

Glad to hear it! Good luck with your project!

Why not

mainargs = [_, _|_] = System.argv()
Runner.main(mainargs)

or

System.argv()
|> Enum.take(2)
|> Runner.main()