krwenholz

krwenholz

UploadWriter fails silently with System.cmd (or I'm holding it wrong)

I’ve written a custom UploadWriter to handle some tasks that can be done during upload and very quickly on close. It passes tests ands leaves behind the correct artifacts (upload to an external store, DB changes, telemetry, and files) on form submission. That’s awesome!

What’s not awesome is that it fails silently (I’m not seeing any logs) such that the upload entry is missing when I get to handle_event("save"...). I’ve narrowed down what’s failing to the use of System.cmd (System.shell also fails in the same way, not surprising) and have created a repro that looks the same in the phoenix_live_view codebase. My code does more than echo, but this is the minimal repro.

https://github.com/phoenixframework/phoenix_live_view/commit/696de9f6cc3b524babd0307a0d1db19f80565694

    @impl true
    def close(test_name, reason) do
      send(test_name, {:close, reason})
      System.cmd("echo", ["hi"])
      {:ok, test_name}
    end

Example failure:

2) test lv with valid token writer can be configured (Phoenix.LiveView.UploadChannelTest)
     test/phoenix_live_view/upload/channel_test.exs:753
     Assertion with =~ failed
     code:  assert render_upload(avatar, "foo.jpeg", 50) =~ "#{@context}:foo.jpeg:100%"
     left:  "<div id=\"phx-F_yS5mRy8ZF98QDk\" data-phx-session=\"SFMyNTY.g2gDaAJhBXQAAAAIZAACaWRtAAAAFHBoeC1GX3lTNW1SeThaRjk4UURrZAAMbGl2ZV9zZXNzaW9uZAADbmlsZAAKcGFyZW50X3BpZGQAA25pbGQACHJvb3RfcGlkZAADbmlsZAAJcm9vdF92aWV3ZAAuRWxpeGlyLlBob2VuaXguTGl2ZVZpZXdUZXN0LlN1cHBvcnQuVXBsb2FkTGl2ZWQABnJvdXRlcmQAA25pbGQAB3Nlc3Npb250AAAAAGQABHZpZXdkAC5FbGl4aXIuUGhvZW5peC5MaXZlVmlld1Rlc3QuU3VwcG9ydC5VcGxvYWRMaXZlbgYAHMW7bZIBYgABUYA.gcGGKYV-ZmdfvdFKFWZ9F9vdxp3mtEjOJIkg3xptyqI\" data-phx-static=\"SFMyNTY.g2gDaAJhBXQAAAADZAAKYXNzaWduX25ld2pkAAVmbGFzaHQAAAAAZAACaWRtAAAAFHBoeC1GX3lTNW1SeThaRjk4UURrbgYAHMW7bZIBYgABUYA.5yoXxILzwUvoaZtt7-eAGLUW9kFRUP3hnI9WDKiScgQ\"><form phx-change=\"validate\" phx-submit=\"save\"><input id=\"phx-F_yS5mSWgbz5wQEE\" type=\"file\" name=\"avatar\" data-phx-hook=\"Phoenix.LiveFileUpload\" data-phx-update=\"ignore\" data-phx-upload-ref=\"phx-F_yS5mSWgbz5wQEE\" data-phx-active-refs=\"\" data-phx-done-refs=\"\" data-phx-preflighted-refs=\"\"/><button type=\"submit\">save</button></form></div>"
     right: "lv:foo.jpeg:100%"
     stacktrace:
       test/phoenix_live_view/upload/channel_test.exs:764: (test)

cmd can be called in either close, meta, or write_chunk and cause the silent failure. meta does actually cause an error about a server PID being missing, but I’m not familiar enough with Elixir to interpret that.

Any suggestions on where to look next or what I might be doing wrong? I didn’t see anything in Phoenix.LiveView.UploadChannel that looked particularly relevant, but my guess is the process is getting closed prematurely somehow.

Marked As Solved

krwenholz

krwenholz

Okay. Finally came up with a (hacky?) solution. System.cmd uses Port, which will, usefully, tell you when it finishes with nice messages (docs). Unfortunately, that message ends up in the UploadChannel inbox. That causes an exit we didn’t want for the writer :grimacing:

If I wrap the System.cmd usage in a GenServer not linked to the writer, then that message isn’t received. Wonderful. In testing, I did this with GenServer.start(SimpleSystemRunner, []). In production, I’m going to write a simple GenServer for my system calls (all to ffmpeg in this case) so it can sit under the app’s supervision tree. I think that’s less hacky.

Where Next?

Popular in Questions Top

mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
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
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
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
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New

We're in Beta

About us Mission Statement