Exceptions even when Phoenix server is not receiving anything from client

Hi there. My Phoenix back end has started emitting error messages that I don’t understand, I’ve reverted to various older versions that I am pretty sure were working OK, but I still get the errors. One commit I am sure of is April 14. It is running on Heroku with no errors but now gives errors locally even when there is no client interrogating it. Bizarre (to me)!

[error] an exception was raised:
    ** (ArgumentError) argument error
        (stdlib) :ets.lookup(nil, :subscribe)
        (phoenix_pubsub) lib/phoenix/pubsub.ex:288: Phoenix.PubSub.call/3
        (phoenix) lib/phoenix/channel/server.ex:254: Phoenix.Channel.Server.init/5
        (stdlib) gen_server.erl:365: :gen_server.init_it/2
        (stdlib) gen_server.erl:333: :gen_server.init_it/6
        (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
       [error] an exception was raised:

Seems like it’s failing at https://github.com/phoenixframework/phoenix_pubsub/blob/16ee3fe013508c3cdbf098c8b85d184aafd25a53/lib/phoenix/pubsub.ex#L288. Is your pubsub server configured?

So sorry, I’m a bit of a noob. I don’ recall ever configuring the pubsub server and wouldn’t know how to do that. Could you offer some guidance on this?

One thing I notice is that while the app is giving error messages locally, the same code deployed on Heroku is not. Both apps function normally from the perspective of the client.

This is potentially because while running the code locally you’ll be running with phoenix live reload, and that uses channels / pubsub to trigger changes.

Can you show your application.ex, endpoint.ex and config.exs files?

There isn’t much configuration, sorry for the bad working on my part. What I wanted to know is what your config :your_app, YourAppWeb.Endpoint, ... (which has some fields for configuring phoenix.pubsub) in config.exs looks like.

1 Like

Thanks! Here come the files

1. application.ex

defmodule LogServer.Application do
  # See https://hexdocs.pm/elixir/Application.html
  # for more information on OTP Applications
  @moduledoc false

  use Application

  def start(_type, _args) do
    # List all child processes to be supervised
    children = [
      # Start the Ecto repository
      LogServer.Repo,
      # Start the endpoint when the application starts
      LogServerWeb.Endpoint
      # Starts a worker by calling: LogServer.Worker.start_link(arg)
      # {LogServer.Worker, arg},
    ]

    # See https://hexdocs.pm/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: LogServer.Supervisor]
    Supervisor.start_link(children, opts)
  end

  # Tell Phoenix to update the endpoint configuration
  # whenever the application is updated.
  def config_change(changed, _new, removed) do
    LogServerWeb.Endpoint.config_change(changed, removed)
    :ok
  end
end

2. endpoint.ex

defmodule LogServerWeb.Endpoint do
  use Phoenix.Endpoint, otp_app: :log_server

  socket "/socket", LogServerWeb.UserSocket,
    websocket: true,
    longpoll: false

  # Serve at "/" the static files from "priv/static" directory.
  #
  # You should set gzip to true if you are running phx.digest
  # when deploying your static files in production.
  plug Plug.Static,
    at: "/",
    from: :log_server,
    gzip: false,
    only: ~w(css fonts images js favicon.ico robots.txt)

  # Code reloading can be explicitly enabled under the
  # :code_reloader configuration of your endpoint.
  if code_reloading? do
    socket "/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket
    plug Phoenix.LiveReloader
    plug Phoenix.CodeReloader
  end

  plug Corsica,
       origins: ["https://knode.io", "https://206.189.184.194", "http://localhost:8175", "http://localhost:8000"],
       log: [rejected: :error, invalid: :warn, accepted: :debug],
       allow_headers: ["content-type", "accept", "authorization"]

##origins: ["https://206.189.184.194", "http:/localhost:8175"],


  plug Plug.RequestId
  plug Plug.Logger

  plug Plug.Parsers,
    parsers: [:urlencoded, :multipart, :json],
    pass: ["*/*"],
    json_decoder: Phoenix.json_library()

  plug Plug.MethodOverride
  plug Plug.Head

  # The session will be stored in the cookie and signed,
  # this means its contents can be read but not tampered with.
  # Set :encryption_salt if you would also like to encrypt it.
  plug Plug.Session,
    store: :cookie,
    key: "_log_server_key",
    signing_salt: "L8GfiIiZ"

  plug LogServerWeb.Router
end

3, config.exs*

use Mix.Config

config :log_server,
  ecto_repos: [LogServer.Repo]

config :logger, :console,
  format: "$time $metadata[$level] $message\n",
  metadata: [:request_id]

config :phoenix, :json_library, Jason

import_config "#{Mix.env()}.exs"

Interesting, your config.exs doesn’t provide any configuration of your endpoint. Can you show your dev.exs ?

Oops, below is dev.exs.

I commented out the line

# ~r{priv/static/.*(js|css|png|jpeg|jpg|gif|svg)$},

in dev.exs .

So far no errors, though I don’t see why that would be the case.

  use Mix.Config


config :log_server, LogServerWeb.Endpoint,
  http: [port: 4000],
  debug_errors: true,
  code_reloader: true,
  check_origin: false

config :log_server, LogServerWeb.Endpoint,
  live_reload: [
    patterns: [
      ~r{priv/static/.*(js|css|png|jpeg|jpg|gif|svg)$},
      ~r{priv/gettext/.*(po)$},
      ~r{lib/log_server_web/views/.*(ex)$},
      ~r{lib/log_server_web/templates/.*(eex)$}
    ]
  ]

# Do not include metadata nor timestamps in development logs
config :logger, :console, format: "[$level] $message\n"

# Set a higher stacktrace during development. Avoid configuring such
# in production as building large stacktraces may be expensive.
config :phoenix, :stacktrace_depth, 20

# Initialize plugs at runtime for faster development compilation
config :phoenix, :plug_init_mode, :runtime

# Configure your database
config :log_server, LogServer.Repo,
  username: "postgres",
  password: "postgres",
  database: "log_server_dev",
  hostname: "localhost",
  pool_size: 10

Right so your issue is that you’re missing the part of the config that usually goes in config.exs, and it sets up the pubsub configuration. In your case you’ll want:

config :log_server, LogServerWeb.Endpoint,
  pubsub: [name: LogServer.PubSub, adapter: Phoenix.PubSub.PG2]
4 Likes

Thanks, that works!!