IVR
Hi all,
I have a small Phoenix API which you can find here.
I’m running into a bizarre bug: whenever I run mix test I get intermittent failures, which primarily occur in my controllers, the errors say that the passed user IDs are nil – user IDs are pulled from conn.assigns.current_user which is set via Plug.Conn.put_req_header(“authorization”, token), so I guess this part intermittently fails. The strange part is that when I run mix test on individual test files, everything works fine. It appears that the problem is caused by some sort of DB pollution.
Has anyone experienced this? Any ideas on how to fix it?
Many thanks!
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
benwilson512
What database are you using, and are you doing any kind of caching in your application?
al2o3cr
I can’t pinpoint a cause, but I discovered something interesting. There appears to be a race between putting the user’s data in the Pow session cache and reading it out; I verified this by adding this (very much NOT FOR PRODUCTION USE) hack to the authorization plug:
When I run
mix testwith this change applied, even with a fixed--seed, I observe intermittent failures that resolve after retries:IVR
Thanks for looking into this!
I’m using a standard Postgres DB and I’m not caching anything myself, but perhaps some of the libraries that I’m using are doing that. The only one that comes to mind is Pow that is used for authentication.
IVR
Wow
@al2o3cr thank you so much for looking into this and spotting such a weird bug. I probably wouldn’t have picked it up.
After trying out your hack, all my tests are fixed! As you said it though, this is not a production-grade solution. Perhaps @danschultzer might be able to help here?
The fetch method in question comes from this documentation, but I imagine that the actual bug must be somewhere deeper in Pow’s codebase. I’ve created a GitHub issue here, hopefully, we’ll be able to get that fixed, but I feel we’ll need to do a bit more digging first.
What is most concerning to me is that the bug might not actually be limited to tests only. I believe Pow is a battle-tested solution, so I’d be surprised if I’m the first one to run into this.
Sebb
I assume you are using Ecto.Adapters.SQL.Sandbox — Ecto SQL v3.14.0 (just asking because thats where I failed testing an app with DB)
IVR
I’ve tried the following inside my test_helpers.exs:
And I’ve also tried
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Repo)anduse ExUnit.Case, async: truein my tests just like the link you’ve shared suggests. Neither of these helped.Either way, it looks like we’ve narrowed down the problem to Pow’s auth plug, but we still need to figure out where exactly the problem occurs. I’ll try to look at Pow’s source code tomorrow when I should have a bit more time, but I’m an Elixir beginner, so I’d be surprised if I could nail this one.
al2o3cr
Found a useful comment from Pow’s maintainer - note the use of
sleepto ensure the cache has caught up:https://github.com/danschultzer/pow/issues/311#issuecomment-546151186
Using the alternative store in that comment reduces the number of “succeeded after 2 tries” messages in the output, but they don’t go away entirely
EDIT: I did not have the store plugged in correctly when this message was written - see followup belowIVR
Thanks for sharing! I’m now concerned that this problem might affect the production environment too, so the use of sleep is certainly not a solution and since the alternative store doesn’t resolve the problem either, I think the search goes on
I’ve reached out to people on Slack as well, hopefully, this will get some attention because it seems that it’s a Pow issue.
al2o3cr
I did not have the “mock” store plugged in correctly before, so it wasn’t actually affecting the behavior of the system.
Updating the mock to:
and adding:
makes the tests pass consistently!
Unrelated: password hashing is intentionally resource-intensive to prevent attacks, but that’s a problem in tests. Setting
config :pow, Pow.Ecto.Schema.Password, iterations: 1inconfig/test.exstakes the runtime formix testfrom 73s down to 1 second on my machine.IVR
@al2o3cr thank you so much. I really appreciate you looking into this and you did not just find a solution but you’ve also helped me speed up my tests by a massive factor. I’m quite new to Elixir, so I would have struggled with this one. I wish I could buy you a beer!
EDIT: after running tests repeatedly, I noticed that unfortunately it still fails intermittently
The
SleepRescueWeb.EtsCacheMock.init()is supposed to go into test_helpers.exs, right?