Skip to content

PubSub registry fails to start or race condition when testing? #155

Open
@sashaafm

Description

@sashaafm

I'm having this very random issue (it only happens occasionally, let's say 40% of the time) where it looks like my tests fail because they can't publish to my Phoenix.PubSub. The errors are all similar to this one:

121) test run returns an error tuple when it fails to update an account (MyAppServer.Controllers.CreateOrUpdateAccountTest)
     apps/my_app_server/test/my_app_server/controllers/create_or_update_account_test.exs:35
     ** (ArgumentError) unknown registry: MyAppServer.PubSub
     code: {:ok, _account} = Controllers.CreateOrUpdateAccount.run(user)
     stacktrace:
       (elixir 1.11.4) lib/registry.ex:999: Registry.meta/2
       (phoenix_pubsub 2.0.0) lib/phoenix/pubsub.ex:144: Phoenix.PubSub.broadcast/4
       (my_app_server 0.0.1) lib/my_app_server/controllers/create_or_update_account.ex:35: MyAppServer.Controllers.CreateOrUpdateAccount.run/1
       test/my_app_server/controllers/create_or_update_account_test.exs:38: (test)

the stack trace points to

{:ok, {adapter, name}} = Registry.meta(pubsub, :pubsub)
when it attempts to broadcast. I'm using a wrapper module around PubSub but it is quite simple and only calling the PubSub API.

I've also seen this issue #144 where it looks like the author was having a similar problem. However, it seems like he fixed it by having the correct child order in his top-level supervisor.

My top-level supervisor children is the following:

    children = [
      {Phoenix.PubSub, name: MyAppServer.PubSub},
      MyAppServerWeb.Presence,
      MyAppServerWeb.Telemetry,
      MyAppServerWeb.Endpoint,
      {Oban, Application.get_env(:my_app_server, Oban)},
      MyAppServer.Event.Bus.Handler.Logging,
      { DynamicSupervisor,
        strategy: :one_for_one,
        name: MyAppServer.SomeNamespace.Supervisor
      },
      { Registry,
        keys: :unique,
        partitions: System.schedulers_online(),
        name: MyAppServer.SomeNamespace.Registry
      }
    ]

and by the docs of all applications I believe it is correct.

I cannot find a reason for this to be happening. It's completely random and sometimes I can execute my test suite many times and it will never happen. Sometimes it will happen occasionally and other times it will be more frequent.

My test_helper.exs also has nothing out of the ordinary:

ExUnit.start()

Faker.start()

Ecto.Adapters.SQL.Sandbox.mode(MyRepo, :manual)

this issue has never happened in the dev environment, however it has been plaguing my test environment for quite a while.

Any tips on how to solve this issue? Thanks!

EDIT 1: I've found that this issue is reproducible using the seed given at the end of the test suite. I'll try to start from here to understand the problem at hand.

EDIT 2: Adding Application.ensure_all_started(:my_app_server) to the setup_all callback of the test files belonging to the top-level supervisor children seems to have fixed this issue (at least for the seeds I knew could reproduce the problem). However, I'm still unaware of the root cause.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions