#28: a held write lock is {:error, :busy} and the query store lives on #29

Closed
mercurius wants to merge 0 commits from mercurius/28-busy-read into main
Member

Refs macula-services/mcl-bookclub-phoenix#28

Purpose (fix): a read that meets the PRJ writer's lock answers {:error, :busy}, and the query store lives on.

  • dfc2af3, red first: bookclub_query_store_test.exs. A second connection holds BEGIN EXCLUSIVE. On main, the warm store's read meets :"$busy" in esqlite3:fetchall1/2 (esqlite3.erl:236) and the store dies: the stack of CI run 6465.
  • 00ec229, the fix: read/3 keeps :esqlite3.q/3, which finalizes its statement before it returns, and rescues the CaseClauseError on :"$busy" as {:error, :busy}. Any other CaseClauseError is re-raised.
  • Why not my own prepare/step loop: it leaves statements for GC. Their destructor runs on a NORMAL scheduler and waits for the connection mutex, which a busy-waiting step holds for up to 2 s, so a whole scheduler froze. The evidence is gdb and strace traces, and the short-lock test failing 5 of 6 runs under seed 1. The traces are in ~/.claude/sessions/mercurius-busy/investigate/ on host00.
  • The test also checks that after a busy answer the writer can lock again.

Local: format, compile --warnings-as-errors, credo --strict, all 283 tests, and dialyzer are clean. The Erlang twin is mcl-bookclub PR 16 (#15), same shape.

Refs #30: esqlite ~> 0.10 in both apps' mix.exs (the fix relies on q/3 finalizing, which 0.9.0 does not do; mix.lock already 0.10.1).

Refs macula-services/mcl-bookclub-phoenix#28 Purpose (fix): a read that meets the PRJ writer's lock answers {:error, :busy}, and the query store lives on. - dfc2af3, red first: bookclub_query_store_test.exs. A second connection holds BEGIN EXCLUSIVE. On main, the warm store's read meets :"$busy" in esqlite3:fetchall1/2 (esqlite3.erl:236) and the store dies: the stack of CI run 6465. - 00ec229, the fix: read/3 keeps :esqlite3.q/3, which finalizes its statement before it returns, and rescues the CaseClauseError on :"$busy" as {:error, :busy}. Any other CaseClauseError is re-raised. - Why not my own prepare/step loop: it leaves statements for GC. Their destructor runs on a NORMAL scheduler and waits for the connection mutex, which a busy-waiting step holds for up to 2 s, so a whole scheduler froze. The evidence is gdb and strace traces, and the short-lock test failing 5 of 6 runs under seed 1. The traces are in ~/.claude/sessions/mercurius-busy/investigate/ on host00. - The test also checks that after a busy answer the writer can lock again. Local: format, compile --warnings-as-errors, credo --strict, all 283 tests, and dialyzer are clean. The Erlang twin is mcl-bookclub PR 16 (#15), same shape. Refs #30: esqlite ~> 0.10 in both apps' mix.exs (the fix relies on q/3 finalizing, which 0.9.0 does not do; mix.lock already 0.10.1).
A second connection holding BEGIN EXCLUSIVE stands in for the PRJ writer.
On main the warm store's read meets :"$busy" at its first step,
esqlite3's fetchall1/2 has no clause for it, and the store dies: the crash
of CI run 6465. A short lock is still waited out. The Erlang twin's
bookclub_query_store_tests.erl (macula-services/mcl-bookclub#15).

Refs macula-services/mcl-bookclub-phoenix#28
#28: a held write lock is {:error, :busy} and the query store lives on
Some checks failed
c4 model / c4-model-1 (pull_request) Successful in 3s
wall tree / wall-tree-1 (pull_request) Successful in 4s
c4 model / c4-model (pull_request) Successful in 0s
wall tree / wall-tree (pull_request) Successful in 0s
ci / guard-1 (pull_request) Successful in 10s
ci / guard (pull_request) Successful in 0s
ci / gates (OTP 28.4.3 / Elixir 1.18.4) (pull_request) Has been cancelled
ci / Release (forge, v* tag) (pull_request) Has been cancelled
00ec22988f
The store reads through :esqlite3.q/3 as before, which finalizes its
statement before it returns, even on an exception. Its fetch loop has no
clause for :"$busy", so a read that met the PRJ writer's lock past
esqlite3's own 2 s wait raised a CaseClauseError and the store died (CI run
6465). read/3 answers that one case as {:error, :busy}, which the queries
already surface as {:error, {:store_error, :busy}}; any other CaseClauseError
is re-raised.

Not a hand-rolled prepare/bind/step loop: that leaves each statement for
garbage collection, whose destructor runs on a normal scheduler and waits
for the connection's mutex, which a waiting step holds for up to 2 s. One
scheduler froze (gdb and strace traces), and the short-lock test failed
under seed 1.

The test also checks the writer can lock again after a busy answer, and
reads an id with this file's own suffix ("u"), which no other suite seeds.

Refs macula-services/mcl-bookclub-phoenix#28
#30: esqlite ~> 0.10 in project_bookclub and query_bookclub
All checks were successful
c4 model / c4-model-1 (pull_request) Successful in 5s
c4 model / c4-model (pull_request) Successful in 0s
ci / guard-1 (pull_request) Successful in 5s
ci / guard (pull_request) Successful in 0s
wall tree / wall-tree-1 (pull_request) Successful in 6s
wall tree / wall-tree (pull_request) Successful in 0s
ci / gates (OTP 28.4.3 / Elixir 1.18.4) (pull_request) Successful in 6m37s
ci / Release (forge, v* tag) (pull_request) Has been skipped
57cafbf896
The busy fix (#28) relies on esqlite3:q/3 finalizing its statement before it
returns, which esqlite 0.9.0 does not do. mix.lock already holds 0.10.1, so
only the floor moves.

Refs macula-services/mcl-bookclub-phoenix#30
#28 PR 29 review: busy first, then the short lock, in one test
All checks were successful
c4 model / c4-model-1 (pull_request) Successful in 3s
c4 model / c4-model (pull_request) Successful in 0s
wall tree / wall-tree-1 (pull_request) Successful in 5s
ci / guard-1 (pull_request) Successful in 7s
wall tree / wall-tree (pull_request) Successful in 0s
ci / guard (pull_request) Successful in 0s
ci / gates (OTP 28.4.3 / Elixir 1.18.4) (pull_request) Successful in 6m32s
ci / Release (forge, v* tag) (pull_request) Has been skipped
02719e3ce2
ExUnit shuffles tests by seed. The store that read through its own
prepare/step loop, leaving statements to garbage collection, failed only
when the short-lock case ran after the busy one: seeds 1, 2 and 5 caught
it, seeds 3, 4 and 6 did not. One test in that order catches it under every
seed, at the writer's second lock.

Refs macula-services/mcl-bookclub-phoenix#28
#28 PR 29 review r2: every statement a busy read prepares is finalized
All checks were successful
c4 model / c4-model-1 (pull_request) Successful in 3s
c4 model / c4-model (pull_request) Successful in 0s
ci / guard-1 (pull_request) Successful in 5s
ci / guard (pull_request) Successful in 0s
wall tree / wall-tree-1 (pull_request) Successful in 5s
wall tree / wall-tree (pull_request) Successful in 0s
ci / gates (OTP 28.4.3 / Elixir 1.18.4) (pull_request) Successful in 6m10s
ci / Release (forge, v* tag) (pull_request) Has been skipped
c4 model / c4-model-1 (push) Successful in 4s
c4 model / c4-model (push) Successful in 0s
ci / guard-1 (push) Successful in 7s
ci / guard (push) Successful in 0s
wall tree / wall-tree-1 (push) Successful in 5s
wall tree / wall-tree (push) Successful in 0s
ci / gates (OTP 28.4.3 / Elixir 1.18.4) (push) Successful in 6m16s
ci / Release (forge, v* tag) (push) Has been skipped
04efa992f8
The busy-first order caught only a store that kept its read lock. A store
that resets on busy but leaves its statements to garbage collection, the
shape that froze a scheduler, passed every seed. Its freeze depends on when
the runtime runs the destructor, so no wait shows it reliably.

The test now traces the store through the busy read and asserts that each
esqlite3:prepare/3 is matched by esqlite3's finalize_query_statement/1,
which only q/3 calls. The comment says what the test pins and what it does
not.

Refs macula-services/mcl-bookclub-phoenix#28
Member

Landed by fast-forward: main is 04efa992f8 (from 63d4a711). This repository does not allow the manually-merged style, so the PR is closed by hand.

Landed by fast-forward: main is 04efa992f87210a3a92f52e93808b04dee4ca6f2 (from 63d4a711). This repository does not allow the manually-merged style, so the PR is closed by hand.
terra closed this pull request 2026-12-01 20:58:34 +00:00
All checks were successful
c4 model / c4-model-1 (pull_request) Successful in 3s
c4 model / c4-model (pull_request) Successful in 0s
ci / guard-1 (pull_request) Successful in 5s
ci / guard (pull_request) Successful in 0s
wall tree / wall-tree-1 (pull_request) Successful in 5s
wall tree / wall-tree (pull_request) Successful in 0s
ci / gates (OTP 28.4.3 / Elixir 1.18.4) (pull_request) Successful in 6m10s
ci / Release (forge, v* tag) (pull_request) Has been skipped
c4 model / c4-model-1 (push) Successful in 4s
c4 model / c4-model (push) Successful in 0s
ci / guard-1 (push) Successful in 7s
ci / guard (push) Successful in 0s
wall tree / wall-tree-1 (push) Successful in 5s
wall tree / wall-tree (push) Successful in 0s
ci / gates (OTP 28.4.3 / Elixir 1.18.4) (push) Successful in 6m16s
ci / Release (forge, v* tag) (push) Has been skipped

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
3 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
macula-services/mcl-bookclub-phoenix!29
No description provided.