# Elixir function doesn't continue if takes too long

**URL:** <https://forum.elixirforum.com/t/elixir-function-doesnt-continue-if-takes-too-long/61457>\
**Category:** Questions\
**Tags:** troubleshooting, learning-elixir, function\
**Created:** [February 5, 2024, 11:47am UTC](https://forum.elixirforum.com/t/elixir-function-doesnt-continue-if-takes-too-long/61457 "2024-02-05T11:47:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Codename-404](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/codename-404/32/32836_2.png) [@Codename-404](https://forum.elixirforum.com/u/Codename-404)\
**Post date:** [February 5, 2024, 11:47am UTC](https://forum.elixirforum.com/t/elixir-function-doesnt-continue-if-takes-too-long/61457/1 "2024-02-05T11:47:36Z")

</div>

def func do  
convert\_video.(1280)  
IO.puts(“Conversion done, starting to upload”)  
end

I have this function, but problem is, if convert\_video.(1280) takes too long, IO.puts(“Conversion done, starting to upload”) line does not execute, infact nothing after executes.  
However if convert\_video.(1280) takes short time, all the codes below executes.

How to make sure function always executes fully?

---

<div class="post-metadata">

**Author:** ![lud](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lud/32/14382_2.png) [@lud](https://forum.elixirforum.com/u/lud)\
**Post date:** [February 5, 2024, 12:46pm UTC](https://forum.elixirforum.com/t/elixir-function-doesnt-continue-if-takes-too-long/61457/2 "2024-02-05T12:46:43Z")

</div>

Obvious question sorry but how are you sure that `convert_video.(...)` has finished?

---

<div class="post-metadata">

**Author:** ![Codename-404](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/codename-404/32/32836_2.png) [@Codename-404](https://forum.elixirforum.com/u/Codename-404)\
**Post date:** [February 5, 2024, 12:53pm UTC](https://forum.elixirforum.com/t/elixir-function-doesnt-continue-if-takes-too-long/61457/3 "2024-02-05T12:53:29Z")

</div>

it’s just calling a system command for ffmpeg, I can monitor the porgress as well as the process in the os, if they are still there. And I can see they are done. I also tried leaving it for more then an hour. It just does nothing.  
If I call other functions (via Phoenix api), it runs, even the same function as well. Just doesn’t execute the rest of the code if the convert\_video() takes long.

---

<div class="post-metadata">

**Author:** ![lud](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lud/32/14382_2.png) [@lud](https://forum.elixirforum.com/u/lud)\
**Post date:** [February 5, 2024, 12:55pm UTC](https://forum.elixirforum.com/t/elixir-function-doesnt-continue-if-takes-too-long/61457/4 "2024-02-05T12:55:27Z")

</div>

I am far from an expert but maybe ffmpeg does not send EOF to stdout (if that is even a thing…). Can you try with the rambo library?

---

<div class="post-metadata">

**Author:** ![Codename-404](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/codename-404/32/32836_2.png) [@Codename-404](https://forum.elixirforum.com/u/Codename-404)\
**Post date:** [February 5, 2024, 12:57pm UTC](https://forum.elixirforum.com/t/elixir-function-doesnt-continue-if-takes-too-long/61457/5 "2024-02-05T12:57:05Z")

</div>

That might be a case, trying that now. thank you ~

---

<div class="post-metadata">

**Author:** ![rvirding](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/rvirding/32/1409_2.png) [@rvirding](https://forum.elixirforum.com/u/rvirding)\
**Post date:** [February 5, 2024, 5:05pm UTC](https://forum.elixirforum.com/t/elixir-function-doesnt-continue-if-takes-too-long/61457/6 "2024-02-05T17:05:43Z")

</div>

A function always executes until it is completed.

---

<div class="post-metadata">

**Author:** ![lud](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lud/32/14382_2.png) [@lud](https://forum.elixirforum.com/u/lud)\
**Post date:** [February 5, 2024, 6:14pm UTC](https://forum.elixirforum.com/t/elixir-function-doesnt-continue-if-takes-too-long/61457/7 "2024-02-05T18:14:59Z")

</div>

Also what you can do is register your process to give it a name:

```elixir
def func do
  Process.register(self(), :hello)
  convert_video.(1280)
  IO.puts("Conversion done, starting to upload")
end

```

Then start your app with `iex` (like `iex -S mix phx.server` if using Phoenix) and run a single long conversion (otherwise process registration will clash), and once you see in the OS that it is finished, you can call this in IEx:

```elixir
pid = Process.whereis(:hello)

```

This will return a pid, or `nil`. If it returns `nil` it means that your function either crashed silently, for some reason, or completed but you could not see the output of `IO.puts` for some reason (but it _was_ executed as Robert says).

If you get a pid, then your process is still awaiting something, or looping forever somewhere. You can then call this in IEx shell:

```elixir
Process.info(pid, :current_stacktrace)

```

This will show what is that process doing at the moment.

For instance if for some reason the process is still awaiting a return from a system command, you may see this:

```elixir
iex(7)> pid = spawn(fn -> System.cmd("watch", ["sleep", "1000"]) end)
#PID<0.116.0>
iex(8)> Process.info(pid, :current_stacktrace)
{:current_stacktrace,
 [
   {System, :do_port_byte, 3, [file: ~c"lib/system.ex", line: 1118]},
   {System, :do_cmd, 3, [file: ~c"lib/system.ex", line: 1107]},
   {:elixir, :eval_external_handler, 3, [file: ~c"src/elixir.erl", line: 396]}
 ]}

```

---

<div class="post-metadata">

**Author:** ![Codename-404](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/codename-404/32/32836_2.png) [@Codename-404](https://forum.elixirforum.com/u/Codename-404)\
**Post date:** [February 6, 2024, 2:45am UTC](https://forum.elixirforum.com/t/elixir-function-doesnt-continue-if-takes-too-long/61457/8 "2024-02-06T02:45:55Z")

</div>

That was really helpful, Thank you very much ~  
So, I found out, the process was returing nil after a while, then I realized it was the phoenix timeout after 60 seconds by default.  
So I increased the default timeout behaviour, and now it works. Alhamdulillah ~  
Thanks again, May Allah return the favour upon you in a greater way ~

---

<div class="post-metadata">

**Author:** ![dimitarvp](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dimitarvp/32/38664_2.png) [@dimitarvp](https://forum.elixirforum.com/u/dimitarvp)\
**Post date:** [February 6, 2024, 12:11pm UTC](https://forum.elixirforum.com/t/elixir-function-doesnt-continue-if-takes-too-long/61457/9 "2024-02-06T12:11:52Z")

</div>

Consider marking @lud’s answer as the solution. It helps future readers.
