Only use async: true for ConnCase error tests on Postgres/no-Ecto - #6783
Merged
SteffenDE merged 1 commit intoAug 2, 2026
Merged
Conversation
… apps
Fixes a long-standing flaky test issue in generated SQLite applications where
running `mix test` would intermittently fail with:
** (Exqlite.Error) Database busy
Root Cause
`phx.new` generated `error_html_test.exs` and `error_json_test.exs` with
hardcoded `use ConnCase, async: true`.
In apps with Ecto enabled, `ConnCase` includes a setup block that calls
`DataCase.setup_sandbox(tags)`. When ExUnit runs an `async: true` test module
concurrently with `async: false` test modules (such as tests generated by
`phx.gen.auth`), `setup_sandbox` sees `tags[:async] == true` and checks out
a non-shared secondary connection handle (`shared: false`) from the Repo pool.
Because SQLite uses single-file database locking, having two active database
connections opening sandbox transactions concurrently causes file lock contention
whenever a synchronous test inserts records (e.g. `user_fixture()`). Depending
on ExUnit test seeding and process scheduling, the secondary connection checkout
fails with `(Exqlite.Error) Database busy`.
Background
When `--database sqlite3` support was added in 5143696
(phoenixframework#4268, April 2021), error controller test templates retained hardcoded
`use ConnCase, async: true`. Later, when `phx.gen.auth` introduced support for
`async: true` in 3d609e0 (phoenixframework#5689, January 2024),
it added a helper to conditionally append `async: true` ONLY for PostgreSQL adapters
while leaving non-Postgres adapters as `async: false`. However, `phx.new` was not
updated at the time to use the same logic for its generated error test templates.
Fix
1. Add `test_case_options/1` in `Phx.New.Generator` matching the behavior of `phx.gen.auth`:
- `Ecto.Adapters.Postgres` -> `", async: true"`
- `nil` (no Ecto) -> `", async: true"`
- non-Postgres adapters (SQLite, MySQL, TDS) -> `""`
2. Update `error_html_test.exs.eex` and `error_json_test.exs.eex` templates to use `<%= @test_case_options %>`.
3. Added test coverage across single app and umbrella project generators for Postgres, `--no-ecto`, SQLite3, MySQL, and MSSQL.
SteffenDE
approved these changes
Aug 2, 2026
Member
|
Let's gooo 🙌🏻 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes a long-standing flaky test issue in generated SQLite applications where running
mix testwould intermittently fail with:Recent failure in CI: #6781 (comment), https://github.com/phoenixframework/phoenix/actions/runs/30761860392/job/91533719530?pr=6781 which motivated me to go down the rabbit hole. I've ran into similar flakes in a production app generated years ago, but never bothered to root cause it until now.
Root Cause
phx.newgeneratederror_html_test.exsanderror_json_test.exswith hardcodeduse ConnCase, async: true.In apps with Ecto enabled,
ConnCaseincludes a setup block that callsDataCase.setup_sandbox(tags). When ExUnit runs anasync: truetest module concurrently withasync: falsetest modules (such as tests generated byphx.gen.auth),setup_sandboxseestags[:async] == trueand checks out a non-shared secondary connection handle (shared: false) from the Repo pool.Because SQLite uses single-file database locking, having two active database connections opening sandbox transactions concurrently causes file lock contention whenever a synchronous test inserts records (e.g.
user_fixture()). Depending on ExUnit test seeding and process scheduling, the secondary connection checkout fails with(Exqlite.Error) Database busy.Background
When
--database sqlite3support was added in 5143696 (#4268, April 2021), error controller test templates retained hardcodeduse ConnCase, async: true. Later, whenphx.gen.authintroduced support forasync: truein 3d609e0 (#5689, January 2024), it added a helper to conditionally appendasync: trueONLY for PostgreSQL adapters while leaving non-Postgres adapters asasync: false. However,phx.newwas not updated at the time to use the same logic for its generated error test templates.Fix
Add
test_case_options/1inPhx.New.Generatormatching the behavior ofphx.gen.auth:Ecto.Adapters.Postgres->", async: true"nil(no Ecto) ->", async: true"""Update
error_html_test.exs.eexanderror_json_test.exs.eextemplates to use<%= @test_case_options %>.Added test coverage across single app and umbrella project generators for Postgres,
--no-ecto, SQLite3, MySQL, and MSSQL.