Skip to content

Only use async: true for ConnCase error tests on Postgres/no-Ecto - #6783

Merged
SteffenDE merged 1 commit into
phoenixframework:mainfrom
praialabs:installer-fix-sqlite3-conncase-async
Aug 2, 2026
Merged

Only use async: true for ConnCase error tests on Postgres/no-Ecto#6783
SteffenDE merged 1 commit into
phoenixframework:mainfrom
praialabs:installer-fix-sqlite3-conncase-async

Conversation

@rhcarvalho

Copy link
Copy Markdown
Contributor

Fixes a long-standing flaky test issue in generated SQLite applications where running mix test would intermittently fail with:

** (Exqlite.Error) Database busy

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.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 (#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 (#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.

… 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
SteffenDE merged commit a6e9e63 into phoenixframework:main Aug 2, 2026
8 checks passed
@SteffenDE

Copy link
Copy Markdown
Member

Let's gooo 🙌🏻

@rhcarvalho
rhcarvalho deleted the installer-fix-sqlite3-conncase-async branch August 2, 2026 21:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants