Skip to content

Read helper: retry a WAL cohort that closes during the open - #317

Merged
professorpalmer merged 1 commit into
devfrom
fix/readonly-source-changed-flake
Nov 20, 2026
Merged

professorpalmer merged 1 commit into
devfrom
fix/readonly-source-changed-flake

Conversation

@professorpalmer

Copy link
Copy Markdown
Owner

Problem

On macOS CI, tests/test_stop_receipt.py::StopReceiptTests::test_a_cut_running_worker_records_a_stop_receipt_and_settles failed now and then with ReadUnavailable: unable to open database: source changed (run 44764591763, job 136385806279, PR #314; one earlier time near v1.51.4). The call path is store.cut_task -> store.job_ref -> readonly.connect(reuse=True) -> ReadConnection.__init__. A second run of the same tree passed.

Root cause

Each store call opens and closes its own SQLite connection. In WAL mode the last close checkpoints and deletes the -wal/-shm pair.

The read helper (readonly_worker.main) takes its first stamps, opens the file, checks for a lock, takes the lock, and stamps again. When a writer in the test process closes its last connection inside that window, the first stamps include the WAL pair and the second stamps do not. The main file keeps its inode, size, mtime and ctime. The helper then reports source changed with launch_topology_change=True and same_store_write=False.

ReadConnection retried that proof only for attach_binding reads. Ordinary and reuse reads raised it at once, although the same reads retry live sidecars and active reader, which are the same writer cohort seen at a different time.

Probed, with a temporary tag on each helper emit site: all 25 source changed errors in the before runs below came from the post-lock compare (readonly_worker.py, after != before after lockf). In each one, the main stamp was equal and only the sidecars were gone.

Fix

  • readonly._sidecar_contention(exc): live sidecars, active reader, or source changed with launch_topology_change. Ordinary and reuse reads retry it in their own window (100 ms for reuse, 1 s for ordinary). Both retry sites use it.
  • After a write race or a cohort turnover, the retry waits 10 ms, not 50 ms. The writer is done. Under load the first answer can take 60 ms of the 100 ms reuse window, and the 50 ms sleep then used the rest, so no retry happened.

A real source swap still raises. A new inode or changed main metadata does not set launch_topology_change (the helper sets it only when the full main stamp is equal). The retry evaluates the source again from the start through the same fences.

Before and after

Stress script: one thread calls store.get_job in a loop (each call opens and closes a WAL connection), the main thread calls store.job_ref 300 times per process. macOS, this Mac. Counts of source changed that reached the caller:

Writer gap Reads Before After
2 ms 9,000 18 0
10 ms 30,000 4 0

The original test in a loop with 18 CPU burners gave 0 failures in 200 runs before the fix, so CPU load alone does not reproduce it. The stress script does.

Not changed: under the same stress, active reader and database is locked still reach the caller when the 100 ms reuse window ends (for example 116 and 7 in 30,000 reads at 10 ms, before and after alike). That is the designed short window of ordinary reads, not this fault.

Tests

tests/test_readonly_cohort_turnover.py:

  • test_closing_writer_between_stamps_and_lock_is_a_topology_change: forces the race with real files. A read-only WAL writer is open at the first stamps and closed before the lock. The helper emits source changed with launch_topology_change=True, same_store_write=False, and the main stamp is unchanged. The next open reads the source.
  • test_ordinary_and_reuse_reads_retry_a_cohort_turnover: fails before the fix in both modes, passes after, with one helper process.
  • test_slow_turnover_answer_still_retries_in_the_reuse_window: first answer at 60 ms. Fails with the old 50 ms sleep.
  • test_source_change_without_proof_still_raises: source changed without a proof still raises.

Local (macOS, this Mac): related readonly, store and launch tests pass (232 passed). Full python -m unittest discover -s tests: 4521 tests, 2 failures and 3 errors, all timeouts in test_job_scope_launch, test_state_dir_binding and test_daemon_worker_mode_uses_warm_worker. The same 5 fail with the origin/dev readonly.py in place (5 failed, 7 passed both ways), so they are not from this change. CI is the gate.

Changelog text for the next release

  • Read helper, WAL cohort turnover: a writer that closes its last connection while a read opens no longer fails the read with "source changed". Ordinary and reuse reads retry it like "live sidecars", and wait 10 ms, not 50 ms. This was a macOS CI flake in test_stop_receipt.

Each store call closes its connection, and the last close deletes the
-wal/-shm pair. When that close falls between the helper's first stamps
and its lock, the helper reports "source changed" with
launch_topology_change: the main file is unchanged, only the sidecars
are gone. Only attach reads retried it. Ordinary and reuse reads raised
it at once, which made test_stop_receipt flake on macOS CI.

Ordinary and reuse reads now retry it in their own window, like "live
sidecars". After a write race or a turnover the retry waits 10 ms, not
50 ms, so a slow first answer does not use the 100 ms reuse window.

Stress (store.job_ref against a thread that opens and closes store
connections): source changed 18 in 9,000 reads before, 0 after (2 ms
writer gap); 4 in 30,000 before, 0 after (10 ms gap).
@professorpalmer

Copy link
Copy Markdown
Owner Author

First CI run: test (macos-latest, 3.12) failed one test, test_codex_host.HostTurnTests.test_cut_interrupts_only_its_turn ('timeout' != 'cancelled'). That test uses only a fake socket host and a threading.Event. It does not use the store or the read helper. All other legs passed. The failed job is rerun.

@professorpalmer
professorpalmer merged commit 4c3c2c5 into dev Nov 20, 2026
25 of 26 checks passed
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.

1 participant