Plug.Crypto error in release task: the table identifier does not refer to an existing ETS table

When attempting to seed data in my release task script, I get an error from Plug.Crypto when generating a token to seed data. I ensure my app is loaded, so don’t know what is preventing Plug.Crypto from being started at this point. This seed logic works when the seeder is invoked as a mix task. Just doesn’t work from Elixir releases:

** (ArgumentError) errors were found at the given arguments:

   * 1st argument: the table identifier does not refer to an existing ETS table

     (stdlib 5.2) :ets.lookup/2
     lib/plug/crypto/key_generator.ex:62: Plug.Crypto.KeyGenerator.with_cache/3
    lib/plug/crypto/key_generator.ex:51: Plug.Crypto.KeyGenerator.generate/6
    lib/plug/crypto.ex:179: Plug.Crypto.sign/4
    ...

I ensure my application is loaded and the repo is loaded as well. Here is the call chain from the mix release script

server shell script:

./my_app eval MyApp.ReleaseTasks.seed

release task module

defmodule MyApp.ReleaseTasks do
  def seed do
    Application.load(:my_app)

    for repo <- repos() do
      {:ok, _, _} = Ecto.Migrator.with_repo(repo, &MyApp.Seeder.run(&1))
    end
  end
end

MyApp.Seeder.run(repo) makes call to Plug.Crypto to generate a token resulting in the error:

Plug.Crypto.sign(secret_key_base, session_context(), data,
            signed_at: signed_at,
            max_age: max_age
          )

The issue was that the plug_crypto application was not started. The main application is loaded but not started. To start plug_crypto when Ecto.Migrator.with_repo/2 is invoked, add the following to config.exs:

config :my_app, MyApp.Repo,
  start_apps_before_migration: [:plug_crypto, :logger]
1 Like