|
| 1 | +# SPDX-FileCopyrightText: Sudo Apt Holdings LLC |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +defmodule Trinity.Repo.SqliteTransactionModeTest do |
| 4 | + @moduledoc """ |
| 5 | + Slice 005 AC1: R26's mechanism, reproduced deterministically, and the fix demonstrated against it. |
| 6 | +
|
| 7 | + R26 has been an intermittent failure for two days and five pull requests. An intermittent fault |
| 8 | + that stops appearing is a fault you got lucky with, so this does not run fifty writers and hope: |
| 9 | + it drives two SQLite connections through the exact sequence the risk register describes and |
| 10 | + asserts the error by name. |
| 11 | +
|
| 12 | + **The mechanism.** A `DEFERRED` transaction takes no lock at `BEGIN` and starts as a reader. At its |
| 13 | + first write it tries to upgrade, and if another connection has written since, SQLite returns |
| 14 | + `SQLITE_BUSY` **without invoking the busy handler**, because blocking at that point risks deadlock. |
| 15 | + No `busy_timeout` affects it, which is why raising it from 5 s to 30 s changed nothing. |
| 16 | +
|
| 17 | + **The fix.** `BEGIN IMMEDIATE` takes the write lock at `BEGIN`, where the busy handler is allowed |
| 18 | + to wait. |
| 19 | +
|
| 20 | + These run against their own database file rather than the sandboxed repo, because the sandbox |
| 21 | + wraps each test in one connection's transaction and cannot express two contending writers. What is |
| 22 | + under test is SQLite's locking, not Ecto's. |
| 23 | + """ |
| 24 | + use ExUnit.Case, async: false |
| 25 | + |
| 26 | + alias Exqlite.Sqlite3 |
| 27 | + |
| 28 | + setup do |
| 29 | + path = Path.join(System.tmp_dir!(), "r26-#{System.unique_integer([:positive])}.db") |
| 30 | + on_exit(fn -> Enum.each([path, path <> "-wal", path <> "-shm"], &File.rm/1) end) |
| 31 | + |
| 32 | + {:ok, setup_conn} = Sqlite3.open(path) |
| 33 | + :ok = Sqlite3.execute(setup_conn, "PRAGMA journal_mode=WAL") |
| 34 | + :ok = Sqlite3.execute(setup_conn, "CREATE TABLE t (id INTEGER PRIMARY KEY, v TEXT)") |
| 35 | + :ok = Sqlite3.close(setup_conn) |
| 36 | + |
| 37 | + {:ok, a} = Sqlite3.open(path) |
| 38 | + {:ok, b} = Sqlite3.open(path) |
| 39 | + # Generous, and deliberately so: the point is that it does not help on the upgrade path. |
| 40 | + :ok = Sqlite3.execute(a, "PRAGMA busy_timeout=5000") |
| 41 | + :ok = Sqlite3.execute(b, "PRAGMA busy_timeout=5000") |
| 42 | + on_exit(fn -> Enum.each([a, b], &Sqlite3.close/1) end) |
| 43 | + |
| 44 | + {:ok, a: a, b: b} |
| 45 | + end |
| 46 | + |
| 47 | + test "DEFERRED: an upgrade under contention is refused at once, whatever busy_timeout says", |
| 48 | + %{a: a, b: b} do |
| 49 | + :ok = Sqlite3.execute(a, "BEGIN DEFERRED") |
| 50 | + :ok = Sqlite3.execute(a, "SELECT count(*) FROM t") |
| 51 | + |
| 52 | + :ok = Sqlite3.execute(b, "BEGIN IMMEDIATE") |
| 53 | + :ok = Sqlite3.execute(b, "INSERT INTO t (v) VALUES ('from b')") |
| 54 | + |
| 55 | + started = System.monotonic_time(:millisecond) |
| 56 | + result = Sqlite3.execute(a, "INSERT INTO t (v) VALUES ('from a')") |
| 57 | + elapsed = System.monotonic_time(:millisecond) - started |
| 58 | + |
| 59 | + assert {:error, reason} = result |
| 60 | + message = to_string(inspect(reason)) |
| 61 | + |
| 62 | + assert message =~ "busy" or message =~ "locked", |
| 63 | + "expected a busy error on the upgrade, got #{message}" |
| 64 | + |
| 65 | + assert elapsed < 1_000, |
| 66 | + "the upgrade waited #{elapsed} ms before failing. It should fail immediately: SQLite " <> |
| 67 | + "does not invoke the busy handler here, which is the whole reason busy_timeout never " <> |
| 68 | + "fixed R26" |
| 69 | + |
| 70 | + Sqlite3.execute(a, "ROLLBACK") |
| 71 | + Sqlite3.execute(b, "COMMIT") |
| 72 | + end |
| 73 | + |
| 74 | + test "IMMEDIATE: the same sequence waits on the busy handler instead of failing", %{a: a, b: b} do |
| 75 | + :ok = Sqlite3.execute(b, "BEGIN IMMEDIATE") |
| 76 | + :ok = Sqlite3.execute(b, "INSERT INTO t (v) VALUES ('from b')") |
| 77 | + |
| 78 | + task = Task.async(fn -> Sqlite3.execute(a, "BEGIN IMMEDIATE") end) |
| 79 | + Process.sleep(100) |
| 80 | + :ok = Sqlite3.execute(b, "COMMIT") |
| 81 | + |
| 82 | + assert :ok = Task.await(task, 5_000), |
| 83 | + "BEGIN IMMEDIATE was refused rather than waiting, so the busy handler is not being " <> |
| 84 | + "consulted even on the path where it should be" |
| 85 | + |
| 86 | + assert :ok = Sqlite3.execute(a, "INSERT INTO t (v) VALUES ('from a')") |
| 87 | + :ok = Sqlite3.execute(a, "COMMIT") |
| 88 | + end |
| 89 | +end |
0 commit comments