Run FastSanitizer in a mix task ends with "no process" error

Hi,

I’m trying to use FastSanitizer in a Mix Task, with the following code :

defmodule Mix.Tasks.HelloTask do

  @impl Mix.Task
  def run(_) do
    html = """
<p>blah</p>
"""
    IO.puts FastSanitize.basic_html(html)
  end
end

I end up with the following error :

** (exit) exited in: NimblePool.checkout(FastHtml.Pool)
    ** (EXIT) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started
    (nimble_pool 0.1.0) lib/nimble_pool.ex:261: NimblePool.exit!/3
    (fast_html 2.0.4) lib/fast_html.ex:125: :fast_html.find_and_use_port/3
    (fast_sanitize 0.2.2) lib/fast_sanitize/fragment.ex:8: FastSanitize.Fragment.to_tree/1
    (fast_sanitize 0.2.2) lib/fast_sanitize/sanitizer.ex:30: FastSanitize.Sanitizer.scrub/2
    (testelixir 0.1.0) lib/hellotask.ex:10: Mix.Tasks.HelloTask.run/1
    (mix 1.11.3) lib/mix/task.ex:394: Mix.Task.run_task/3
    (mix 1.11.3) lib/mix/cli.ex:84: Mix.CLI.run_task/2

I tried to “start an application”, as suggested by the error message, but I did not manage to solve the issue that way.
I tried to setup async task, as suggested by some search on the forum, but I did not manage to solve the issue either.

What do I miss ?

Mix tasks, by default, don’t start the application. But FastSanitize.basic_html is meant to be called inside a running app. You can tell the task to start your app like this:

defmodule Mix.Tasks.HelloTask do
  use Mix.Task

  @impl Mix.Task
  def run(_) do
    Mix.Task.run("app.start")

    html = """
    <p>blah</p>
    """

    {:ok, res} = FastSanitize.basic_html(html)

    IO.puts(res)
  end
end

and then it should work OK:

$ mix hello_task
Compiling 1 file (.ex)
<p>blah</p>

Thanks a lots, it works like a charm.

Followup question : what application is exactly started ? Also what is the difference between starting and not starting an application ? After all why this application is not started by Mix each time by default ?

The one you built - in your example it seems to be called testelixir.

Most Elixir applications work by spawning a bunch of processes in the BEAM (Erlang virtual machine). Those processes will keep running (holding state and waiting to receive messages) until the application is shut down. Obviously, if you don’t start the application, then these processes will not exist.

Because most Mix tasks do not require the app to be started. If it was the default, then a lot of unnecessary work would be done in those cases, and the task would take longer to run. Instead, you get to choose whether to start or not based on your needs.

In your case, the library FastSanitize (and its dependencies) spawn BEAM processes and send/receive messages with them. Therefore, if you want to use the functionality of the library in a Mix task, you must start the application, otherwise when it comes time to send a message to the process, you will get an error that the process does not exist.

1 Like